勉強会夏祭り2010でlsyncdのデモをしてきた #tobesetu

投稿日:

三連休初日の17日に開催された「勉強会夏祭り」に行ってきました。場所は毎度恒例の日本オラクル本社(オラクル青山センター)。いつもお世話になってます。

そして主催の@kwappaさん、@ece_mさん、スタッフの皆様どうもありがとうございました。楽しいイベントに呼んでいただけてとてもうれしかったです。

懇親会のLTでlsyncdのデモをやってきました。LT動画でさくっとアップできたのでまずはこの話題から片付けるw

せっかくなのでlsyncdのインストール方法も書いてみるので、何かの参考になれば幸いです。

同期元にlsyncdをインストール

まずはlsyncdのプロジェクトサイトからtar.gz形式のファイルをダウンロード。プロジェクトはgoogle codeのサイトでホストされてる。

サーバーにインストールするのであれば、URLを直接wgetで叩いちゃった方が早い。こんな感じで。


$ wget http://lsyncd.googlecode.com/files/lsyncd-1.34.tar.gz
--2010-07-20 19:09:48-- http://lsyncd.googlecode.com/files/lsyncd-1.34.tar.gz
lsyncd.googlecode.com をDNSに問いあわせています... 64.233.183.82
lsyncd.googlecode.com|64.233.183.82|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 169174 (165K) [application/x-gzip]
`lsyncd-1.34.tar.gz' に保存中

100%[==========================================================================================================================================>] 169,174 814K/s 時間 0.2s

2010-07-20 19:09:49 (814 KB/s) - `lsyncd-1.34.tar.gz' へ保存完了 [169174/169174]

アーカイブを展開して中に移動し、./configure && make && sudo make install でOKなはず。


$ tar zxf ./lsyncd-1.34.tar.gz
$ cd ./lsyncd-1.34
$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for a BSD-compatible install... /usr/bin/install -c
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking sys/inotify.h usability... yes
checking sys/inotify.h presence... yes
checking for sys/inotify.h... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

手元のCentOS5.4でmakeしようとしたら、エラーが出た。lsyncdは設定ファイルをxmlで記述するので、libxmlのヘッダファイルが無いとコンパイルできない。


lsyncd.c:38:27: error: libxml/parser.h: そのようなファイルやディレクトリはありません
lsyncd.c:39:25: error: libxml/tree.h: そのようなファイルやディレクトリはありません

この場合はlibxml2-develをyumでインストールすればOK。


$ sudo yum install libxml2-devel
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* addons: ftp.riken.jp
* base: ftp.riken.jp
* extras: ftp.riken.jp
* rpmforge: fr2.rpmfind.net
* updates: ftp.riken.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package libxml2-devel.i386 0:2.6.26-2.1.2.8 set to be updated
--> Processing Dependency: pkgconfig for package: libxml2-devel
--> Processing Dependency: zlib-devel for package: libxml2-devel
--> Running transaction check
---> Package pkgconfig.i386 1:0.21-2.el5 set to be updated
---> Package zlib-devel.i386 0:1.2.3-3 set to be updated
--> Finished Dependency Resolution
(中略)
Complete!

コンパイルできたら sudo make install でインストール。


$ sudo make install
make[1]: ディレクトリ `/home/***/src/lsyncd-1.34' に入ります
/bin/sh ./mkinstalldirs /usr/local/bin
/usr/bin/install -c lsyncd /usr/local/bin/lsyncd
/bin/sh ./mkinstalldirs /usr/local/share/man/man1
/usr/bin/install -c -m 644 ./doc/lsyncd.1 /usr/local/share/man/man1/lsyncd.1
/bin/sh ./mkinstalldirs /usr/local/share/man/man5
/usr/bin/install -c -m 644 ./doc/lsyncd.conf.xml.5 /usr/local/share/man/man5/lsyncd.conf.xml.5
make[1]: ディレクトリ `/home/ysato/src/lsyncd-1.34' から出ます
$ which lsyncd
/usr/local/bin/lsyncd

これで /usr/local/bin/lsyncdにインストールされる。


同期先にrsyncを設定する

同期先のサーバーもCentOSなのだが、rsyncdを稼動させるためにxinetdがインストールされている必要がある。標準でインストールするとこれも入っていないようなので、yumでインストールしてやる。


$ sudo yum install xinetd
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* addons: rsync.atworks.co.jp
* base: rsync.atworks.co.jp
* extras: rsync.atworks.co.jp
* updates: www.ftp.ne.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package xinetd.i386 2:2.3.14-10.el5 set to be updated
--> Finished Dependency Resolution
(中略)

xinetdが制御するrsyncdの設定ファイルを編集し、xinetd経由でrsyncdが動くようにする。


$ sudo vi /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no # <- yes から no に変更
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}

rsyncd.confというファイルを/etc以下に作成し、設定を記述する。


$ sudo touch /etc/rsyncd.conf
$ sudo vi /etc/rsyncd.conf
#
# global
#
uid = ******
gid = ******
use chroot = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.xxx.xxx
hosts deny = *
dont compress = *.gz *.tgz *.zip *.bz2 *.jpg *.gif *.png

#
# module
#
[douki_test]
comment = コメントをかけばいいサ
path = /home/yz/public_html
readonly=no

rsyncd.confの設定ファイルの記述方法はrsync でディレクトリの同期(バックアップ) - maruko2 Note.のサイトを参考にしました。

ここまでいったらrsyncdの設定を有効にするため、xinetdを再起動する。(インストールしたばかりで起動していない場合は restart でなく start で普通に起動すればいい)


$ sudo /etc/init.d/xinetd restart
xinetd を停止中: [ OK ]
xinetd を起動中: [ OK ]


rsyncできることを確認する

同期元のサーバーにログインし、適当なフォルダに入って以下のようなコマンドを叩いてみる。


rsync -avz ./test.txt 192.168.xxx.yyy::douki_test

ここで192.168.xxx.yyyの部分はファイルを同期する相手サーバーのIPアドレス。/etc/hostsとかDNSで名前解決できているのであればホスト名でも構わない。
:: の後ろにある「douki_test」はrsyncd.confに記述した「設定の名前」を書く。
ここまでで設定が正しくできていれば、


$ rsync -avz ./test.txt 192.168.xxx.yyy::douki_test
building file list ... done
test.txt

sent 35153 bytes received 38 bytes 70382.00 bytes/sec
total size is 306714 speedup is 8.72

というようなメッセージが出てくるはず。僕が設定した時に漏れていたのはiptablesの設定。rsyncのポートは873なので、同期先のサーバーでアクセスを許容する必要がある。
# iptablesが原因でrsyncに失敗すると rsync: failed to connect to ipp3w-w2: No route to host (113) 「No route to host」と言われるので、すぐ気付くはず。その場合は


-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 873 -j ACCEPT

のようなエントリを /etc/sysconfig/iptables に追記して /etc/init.d/iptables restart で設定を反映させる。


上記のrsync設定を使って、lsyncdを動かす

lsyncdは --no-daemon オプションを使って実行すると、普通に標準出力に色々吐き出しながら実行してくれるので、最初はこれで動作確認をすれば楽ちん。

同期元のサーバーで、lsyncdコマンドを叩くと監視対象のファイルリストがずらずらと出力され、lsyncdが起動する。


/usr/local/bin/lsyncd --no-daemon 192.168.xxx.yyy::douki_test
(中略)
--- Entering normal operation with [***] monitored directories ---

これでlsyncdが問題なく起動してしまえば、後は同期元のサーバーでファイルが作成されたり削除されたりすると、自動的にrsyncが叩かれて同期先のサーバーが同じ状態になります。


設定できたら


lsyncdの起動スクリプトを書いておくといいと思います。
このあたりを参考に・・・

サーバー間ミラーリング(lsyncd+rsync) - 備忘録

lsyncdが「(28:No space left on device)」とかいうエラーを吐いた場合は、inotifyが監視できるファイル数の上限を超えているので、
lsyncdで上限ファイル数を超えた時の対処策 - Unix的なアレ
ここに書いてある通りに対応すればOK。

それでは楽しい同期生活を!