リアルタイムミラーリングツール導入(lsyncd+rsyncd)

最終更新日: 2014.02.19

<<トップページ <<新着情報 <<サイト内検索 <<CentOSで自宅サーバー構築 <<Scientific Linuxで自宅サーバー構築

■概要

lsyncdを使用してマシン間でリアルタイムにディレクトリのミラーリングを行う。
lsyncdはLinuxカーネルのinotify機能を利用して、ファイルの更新時にミラー先のrsyncサーバーへrsyncを実行することにより、リアルタイムにディレクトリのミラーリングを行う。

ここでは、例として/rootディレクトリをミラー先の/tmp/rootディレクトリへミラーリングする。


■rsyncサーバー設定(ミラー先)

[root@to ~]# mkdir /tmp/root ← ミラー先ディレクトリ作成

[root@to ~]# chown nobody:nobody /tmp/root/ ← ミラー先ディレクトリ所有者をnobodyに変更

[root@to ~]# vi /etc/rsyncd.conf ← rsyncサーバー設定ファイル作成
[root] ← 任意のモジュール名(ミラー元から接続時の名前)
    path = /tmp/root ← ミラー先ディレクトリ
    hosts allow = 192.168.1.0/24 ← 接続許可ホスト
    read only = false ← 更新許可

※rsyncd.confの詳細はこちらを参照

■rsyncサーバー起動(ミラー先)

[root@to ~]# yum -y install xinetd ← xinetdインストール

[root@to ~]# /etc/rc.d/init.d/xinetd start ← xinetd再起動
xinetd を停止中:                                           [  OK  ]
xinetd を起動中:                                           [  OK  ]

[root@to ~]# chkconfig rsync on ← rsyncサーバー起動&自動起動設定

■lsyncdインストール(ミラー元)

[root@from ~]# yum -y install libxml2-devel lua-devel lua-static ← lsyncdインストールに必要なパッケージをインストール

[root@from ~]# export LUA_CFLAGS='-I/usr/include -lm -ldl' ← Luaの環境変数設定

[root@from ~]# export LUA_LIBS='/usr/lib/liblua.a' ← Luaの環境変数設定

[root@from ~]# wget http://lsyncd.googlecode.com/files/lsyncd-2.0.2.tar.gz ← lsyncdダウンロード

※最新版のURLはダウンロードページで確認

[root@from ~]# tar zxvf lsyncd-2.0.2.tar.gz ← lsyncd展開

[root@from ~]# cd lsyncd-2.0.2 ← lsyncd展開先ディレクトリへ移動

[root@from lsyncd-2.0.2]# ./configure && make && make install ← lsyncdインストール

[root@from lsyncd-2.0.2]# cp examples/lrsync.lua /etc/lsyncd.conf ← lsyncd設定ファイルコピー

[root@from lsyncd-2.0.2]# cd ← lsyncd展開先ディレクトリを抜ける

[root@from ~]# rm -rf lsyncd-2.0.2 ← lsyncd展開先ディレクトリを削除

[root@from ~]# rm -rf lsyncd-2.0.2.tar.gz ← ダウンロードしたファイルを削除

■lsyncd設定(ミラー元)

[root@from ~]# vi /etc/lsyncd.conf ← lsyncd設定ファイル編集
settings = {
        statusFile = "/tmp/lsyncd.stat",
        statusInterval = 1, ← statusIntervalに変更
}

sync{
        default.rsync,
        source="/root/", ← ミラー元ディレクトリ
        target="xxx.xxx.xxx.xxx:/tmp/root/", ← ミラー先(rsyncサーバー)IPアドレス:ミラー先ディレクトリ
        rsyncOps="-avlt", ← rsyncオプション追加
}

[root@from ~]# ssh-keygen -t rsa -N "" ← RSA鍵ペア(公開鍵/秘密鍵)作成
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): ← 空ENTER
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
09:6c:eb:5a:f7:01:fa:32:1b:8c:b2:82:48:b7:c1:c3 root@from.fedorasrv.com

[root@from ~]# scp .ssh/id_rsa.pub ミラー先IPアドレス:/root/.ssh/authorized_keys2 ← RSA鍵(公開鍵)をミラー先へコピー

■lsyncd起動

[root@master ~]# vi /etc/rc.d/init.d/lsyncd ← lsyncd起動スクリプト作成
#!/bin/bash
#
# lsyncd
#
# chkconfig: - 99 20
# description: lsyncd auto start script

start() {
    /usr/local/bin/lsyncd /etc/lsyncd.conf
}

stop() {
    /bin/kill -9 `/sbin/pidof lsyncd`
    until [ -z $(/sbin/pidof lsyncd) ]; do :; done
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        pid=`pidof lsyncd`
        if [ $? -eq 0 ]; then
            echo "lsyncd (pid $pid) is running..."
        else
            echo "lsyncd is not running"
        fi
        ;;
    *)
        echo "Usage: lsyncd {start|stop|restart|status}"
        exit 1
esac

exit $?

[root@master ~]# chmod +x /etc/rc.d/init.d/lsyncd ← lsyncd起動スクリプトへ実行権限付加

[root@master ~]# /etc/rc.d/init.d/lsyncd start ← lsyncd起動

[root@master ~]# chkconfig --add lsyncd ← lsyncd起動スクリプトをchkconfigへ登録

[root@master ~]# chkconfig lsyncd on ← lsyncd自動起動設定

■lsyncd確認

ミラー元の/rootディレクトリの内容がミラー先の/tmp/rootディレクトリにコピーされていることを確認。
ミラー元の/rootディレクトリでファイルの作成・更新・削除等を行い、その都度、ミラー先に変更が反映されていることを確認。

※ミラーはディレクトリ単位で行われるため、万が一ミラー先が停止してしまって、その間にミラー元に変更があった場合でも、ミラー先起動後の最初のミラー元変更時に全てミラーリングされる


■関連コンテンツ




▲このページのトップへ戻る

プライバシーポリシー