Lsyncdの使い方

Lsyncdって何?

Lsycd - Live Syncing (Mirror) Daemon は、inotify or fsevents を利用して、監視対象のディレクトリに変更が加えられた事を検出し、rsync などを利用し、変更されたファイルの同期を行う為のツールです。

rsyncの代わりに別のツールを呼び出すように設定する事も出来ます。

同期はファイル単位で行われるため、データベースなどの複製には向きません。

入手とインストール

手元に有った、CentOS6 にインストールしてみました。

まず、lsyncdに要求されるモジュールをインストールします。

[root@cent6 ~]# yum -y install lua-devel

[root@cent6 ~]# yum -y install asciidoc

[root@cent6 ~]# yum -y install "Development tools"

で、Lsyncdの入手とコンパイルを行います。

[root@cent6 ~]# wget http://lsyncd.googlecode.com/files/lsyncd-2.1.4.tar.gz

[root@cent6 ~]# tar zxf lsyncd-2.1.4.tar.gz

[root@cent6 lsyncd-2.1.4]# ./configure

[root@cent6 lsyncd-2.1.4]# make

簡単な動作確認

[root@cent6 lsyncd-2.1.4]# ./lsyncd --help

USAGE:

runs a config file:

lsyncd [OPTIONS] [CONFIG-FILE]

default rsync behaviour:

lsyncd [OPTIONS] -rsync [SOURCE] [TARGET]

default rsync with mv's through ssh:

lsyncd [OPTIONS] -rsyncssh [SOURCE] [HOST] [TARGETDIR]

default local copying mechanisms (cp|mv|rm):

lsyncd [OPTIONS] -direct [SOURCE] [TARGETDIR]

OPTIONS:

-delay SECS Overrides default delay times

-help Shows this

-insist Continues startup even if it cannot connect

-log all Logs everything (debug)

-log scarce Logs errors only

-log [Category] Turns on logging for a debug category

-logfile FILE Writes log to FILE (DEFAULT: uses syslog)

-nodaemon Does not detach and logs to stdout/stderr

-pidfile FILE Writes Lsyncds PID into FILE

-runner FILE Loads Lsyncds lua part from FILE

-version Prints versions and exits

LICENSE:

GPLv2 or any later version.

SEE:

`man lsyncd` for further information.

[root@cent6 lsyncd-2.1.4]#

でインストール

[root@cent6 lsyncd-2.1.4]# make install

Lsyncdの動作確認

とりあえず、ローカルシステム内でテストしてみます。

(古いバージョンの時の記事だけど、そのまま使えます。)

同期元 : /test1

同期先 : /test2

・ テスト用のディレクトリとファイルを作成

[root@cent6 lsyncd-2.0.5]# mkdir /test1 /test2

[root@cent6 lsyncd-2.0.5]# touch /test1/testfile

・lsyncd を起動

[root@cent6 lsyncd-2.0.5]# lsyncd -rsync /test1/ /test2/

・同期されている事の確認

[root@cent6 lsyncd-2.0.5]# ls -l /test2/

合計 0

-rw-r--r-- 1 root root 0 9月 1 22:11 2011 testfile

[root@cent6 lsyncd-2.0.5]#

・この時lsyncdは起動したままです

[root@cent6 lsyncd-2.0.5]# ps -ef |grep lsyncd

root 6300 1 0 22:11 ? 00:00:00 lsyncd -rsync /test1/ /test2/

root 6356 3687 0 22:13 pts/1 00:00:00 grep lsyncd

[root@cent6 lsyncd-2.0.5]#

・ 同期元ディレクトリにファイルを追加

[root@cent6 lsyncd-2.0.5]# cp /var/log/httpd/* /test1/

・ 同期先ディレクトリを確認

[root@cent6 lsyncd-2.0.5]# ls -l /test2

合計 14764

-rw-r--r-- 1 root root 4144434 9月 1 22:15 2011 access_log

-rw-r--r-- 1 root root 5031884 9月 1 22:15 2011 access_log-20110821

-rw-r--r-- 1 root root 2642926 9月 1 22:15 2011 error_log

-rw-r--r-- 1 root root 3292470 9月 1 22:15 2011 error_log-20110821

-rw-r--r-- 1 root root 0 9月 1 22:11 2011 testfile

[root@cent6 lsyncd-2.0.5]#

設定ファイルの利用

/etc/lsyncd.conf などのファイルを作成して記述します。

詳細はこちら Lsyncd 2.1.x manual

[root@cent6 ~]# vi /etc/lsyncd.conf

settings {

logfile = "/var/log/lsyncd.log",

statsfile = "/var/run/lsyncd.status",

delay = "3", ## 標準の遅延が 15秒と、ちょっと長いので 3秒に変更します。

}

sync {

default.rsyncssh,

source = "/test1/",

host = "localhost",

targetdir = "/test2/",

rsync = {

archive = true.

}

}

[root@cent6 ~]#

設定ファイルの利用方法

設定ファイルを指定して、lsyncdを起動します。

[root@cent6 ~]# /usr/local/bin/lsyncd /etc/lsyncd.conf

リモートでの同期や、起動ファイルの記述方法はまた後日。。。