通过rsync+inotify-tools+ssh实现触发式远程实时同步

以下是参考原文配置的全过程

环境: CentOS 6.8 x64
55服务器: 192.168.0.55
66服务器: 192.168.0.661. 安装几个必须的软件
安装rsync
# yum -y install wget gcc rsync
安装不成功inotify-tools
# cd 使回到当前根目录
# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar xzvf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure
# make && make install打开端口:
# cd /etc/sysconfig
# vi iptables
增加

#service iptables restart

2. 配置ssh key信任
$ cd
$ ssh-keygen -t rsa

55主机执行:
ssh-copy-id root@192.168.0.66
66主机执行:
ssh-copy-id root@192.168.0.55

测试:
在55服务器执行: ssh root@192.168.0.66 如果需要输入密码,则上面配置错误
在66服务器执行: ssh root@192.168.0.55 如果需要输入密码,则上面配置错误

重启SSH服务
$ cd
# service sshd restart

3. 创建inotify_rsync.sh脚本
$ cd
# vi inotify_rsync.sh

55主机创建:
———————————-
#!/bin/sh
SRC=/home/rsync/test
DST=root@192.168.0.66:/home/rsync/
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F
do
/usr/bin/rsync -a -h -q -z -t –delete $SRC $DST
done

66主机创建: 跟上面不一样的,仅仅是IP
———————————-
#!/bin/sh
SRC=/home/rsync/test
DST=root@192.168.0.55:/home/rsync/
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F
do
/usr/bin/rsync -a -h -q -z -t –delete $SRC $DST
done

4. 创建需要同步的文件夹
mkdir /home/rsync
mkdir /home/rsync/test

5. 执行脚本
$ cd
# chmod +x inotify_rsync.sh
# /root/inotify_rsync.sh & 执行方便测试
#设置脚本开机自启动
vi /etc/rc.local
———————–最下面加入
/root/inotify_rsync.sh &

6. 测试
在55服务器执行:
$ cd /home/rsync/test
$ touch a.txt

在66服务器执行:
$ cd /home/rsync/test
$ ls
a.txt

看到了a.txt文件,说明文件同步已经成功!

测试同步是否存在问题:
/usr/bin/rsync -a -h -q -z -t –delete /home/rsync/test root@192.168.0.66:/home/rsync/
/usr/bin/rsync -a -h -q -z -t –delete /home/rsync/test root@192.168.0.55:/home/rsync/