1 安装

[root@server1 soft]# wget http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz

[root@server1 soft]# tar zxvf rsync-3.0.9.tar.gz

[root@server1 soft]# cd rsync-3.0.9

[root@server1 rsync-3.0.9]# ./configure

[root@server1 rsync-3.0.9]# make && make install

 
[root@server soft]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@server1 soft]# tar zxvf inotify-tools-3.14.tar.gz

[root@server1 soft]# cd inotify-tools-3.14

[root@server1 inotify-tools-3.14]# ./configure

[root@server1 inotify-tools-3.14]# make && make install

 
[root@server1 ~]# yum -y install rsync

[root@server2 ~]# yum -y install rsync

 
2. 配置 ssh key 信任

server1

[root@server1 ~]# useradd -m rsync

[root@server1 ~]# passwd rsync

 
server2

[root@server2 ~]# useradd -m rsync

[root@server2 ~]# passwd rsync

 
server1 (创建 key 并配置 server2 的信任)

[root@server1 ~]# su - rsync

[rsync@server1 ~]$ ssh-keygen -t rsa (所有选项回车,使用默认设置)

[rsync@server1 ~]$ scp ~/.ssh/id_rsa.pub rsync@172.16.19.89:/home/rsync/

 

server2 (将公钥添加到authorized_keys文件中)

[root@server2 ~]# su - rsync

[rsync@server2 ~]$ mkdir .ssh

[rsync@server2 ~]$ chmod 700 .ssh/

[rsync@server2 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys

server2 (创建key并配置对server1的信任)

[rsync@server2 ~]$ ssh-keygen -t rsa

[rsync@server2 ~]$ scp ~/.ssh/id_rsa.pub rsync@172.16.19.118:/home/rsync/

 
server1 (将公钥添加到authorized_keys文件中)

[rsync@server1 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys

 
分别重启 sshd 服务

[root@server1 ~]# service sshd restart

[root@server2 ~]# service sshd restart 

 
3. 添加同步脚本

[root@server1 ~]# vi inotify_rsync.sh

#!/bin/sh

SRC=/home/rsync/test/

DST=rsync@172.16.19.89:/home/rsync/test

INWT=/usr/local/bin/inotifywait

RSYNC=/usr/local/bin/rsync

$INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do

            rsync -aHqzt --delete $SRC $DST

        done

[root@server1 ~]# chmod +x inotify_rsync.sh

[root@server1 ~]# cp inotify_rsync.sh /home/rsync/

[root@server1 ~]# su - rsync

[rsync@server1 ~]$ ./inotify_rsync.sh &

[1] 15305

[rsync@server1 ~]$ Couldn't watch /home/rsync/test/: No such file or directory

 
[1]+  Done                    ./inotify_rsync.sh

[rsync@server1 ~]$ mkdir test

[rsync@server2 ~]$ mkdir test

测试

[rsync@server1 ~]$ cd test/

[rsync@server1 test]$ touch yhl.txt

[rsync@server1 test]$ Address 172.16.19.89 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

 
 
验证

[rsync@server2 ~]$ cd test/

[rsync@server2 test]$ ls