rsyncを使う

rsyncを使う機会があったので、使い方をメモしておくことにします。
server1(192.168.56.10)とserver2(192.168.56.11)を用意し、動作を確認します。


今回はCentOS6.5の64bit版を使っています。

ssh を使った rsync


sshを使うとお手軽でした。パスフレーズ無しの秘密鍵を用意して、
公開鍵認証方式でログインできるようにしておくとシンプルに使えます。

$ rsync -av -e ssh /home/myuser/test myuser@192.168.56.11:/home/myuser
sending incremental file list
test/
test/test.txt

sent 115 bytes  received 35 bytes  100.00 bytes/sec
total size is 0  speedup is 0.00

rsync プロトコルを使った rsync


ローカル環境のコピーなら直接rsyncでも良いですね。

$ rsync -av -e ssh /home/myuser/test /home/myuser/backup/
sending incremental file list
created directory /home/myuser/backup
test/
test/test.txt

sent 115 bytes  received 35 bytes  300.00 bytes/sec
total size is 0  speedup is 0.00

rsyncをデーモンとして立ち上げる(rsyncd)


rsyncをデーモンとして常駐させたい場合、「--daemon」オプションを付けて起動します。
CentOSの場合、RPMパッケージでインストールすると、後述するxinetd経由で起動する想定のようで、
自動起動させたい場合は起動スクリプトを作る必要があります。
また、「/etc/rsyncd.conf」を事前に用意する必要があります。

# /usr/bin/rsyncd --daemon


今回用意した「/etc/rsyncd.conf」は以下の通りです。
同期元のIPアドレスのみ許可し、認証設定をしています。

uid = root
gid = root
use chroot = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
read only = yes
hosts allow = 192.168.56.10
hosts deny = *
auth users = apache,myuser
secrets file = /etc/passwd-rsyncd

[web]
path = /var/www/html
read only = no
uid = apache
gid = apache

[home]
path = /home/myuser
read only = no
uid = myuser
gid = myuser


認証用の「/etc/passwd-rsyncd」は以下の通りです。

apache:apache
myuser:myuser


問題なければ下記のように起動します。

# netstat -at | grep rsync
tcp        0      0 *:rsync                     *:*                         LISTEN      
tcp        0      0 *:rsync                     *:*                         LISTEN 


同期してみた結果です。

$ rsync -av /home/myuser/test rsync://apache@192.168.56.11/web
Password: 
sending incremental file list
test/
test/test.txt

sent 111 bytes  received 31 bytes  56.80 bytes/sec
total size is 0  speedup is 0.00

$ rsync -av /home/myuser/test rsync://myuser@192.168.56.11/home/
Password: 
sending incremental file list
test/
test/test.txt

sent 111 bytes  received 31 bytes  56.80 bytes/sec
total size is 0  speedup is 0.00

rsyncをデーモンとして立ち上げる(xinetd)


rsyncをデーモンとして立ち上げたい場合、CentOSではxined経由で立ち上げることを想定しているようです。
ということでxinetdをインストールします。

# yum install xinetd


「/etc/xinetd.d/rsync」を編集して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
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}


「/etc/rsyncd.conf」をrsyncdをデーモンとして起動した場合と同様、用意します。
xinetdを使っているため、「hosts allow」「hosts deny」オプションは除きます。

uid = root
gid = root
use chroot = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
read only = yes
auth users = apache,myuser
secrets file = /etc/passwd-rsyncd

[web]
path = /var/www/html
read only = no
uid = apache
gid = apache

[home]
path = /home/myuser
read only = no
uid = myuser
gid = myuser


アクセス制御を行う場合は、「/etc/hosts.deny」「/etc/hosts.allow」に以下のような設定を記述します。



/etc/hosts.deny の設定例は以下の通りです。

rsync:ALL


/etc/hosts.allow の設定例は以下の通りです。

rsync:192.168.56.10


同期してみた結果です。

$ rsync -av /home/myuser/test rsync://apache@192.168.56.11/web
Password: 
sending incremental file list
test/
test/test.txt

sent 111 bytes  received 31 bytes  56.80 bytes/sec
total size is 0  speedup is 0.00

$ rsync -av /home/myuser/test rsync://myuser@192.168.56.11/home/
Password: 
sending incremental file list
test/
test/test.txt

sent 111 bytes  received 31 bytes  56.80 bytes/sec
total size is 0  speedup is 0.00


今日はこんなところで。