使用rsync+inotify同步两台服务器文件

目标功能:将B服务器文件同步到A服务器

A服务器rsyncd.conf配置 权限600

A服务器rsyncd.pas文件配置  权限600

同步文件路径 /data/wwwroot/shen/  权限755

打开873端口:iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT

保存设置:service iptables save

重启:service iptables restart

启动服务 rsync --daemon --config=/etc/rsyncd.conf

删除文件即可重启rsync rm -rf /var/run/rsyncd.pid

kill cat /var/run/rsyncd.pid

 

B服务器

rsyncd.pas  权限600

将B服务器中/data/wwwroot/shen/内的所有文件同步到A服务器中

rsync -avH --progress --delete --password-file=/etc/rsyncd.pas /data/wwwroot/shen/ root@112.74.203.95::web

 

常见错误:http://www.jb51.net/article/31920.htm

 

查看是否支持inotify    ls -lsart /proc/sys/fs/inotify

下载 yum install inotify-tools --enablerepo=epel

检测是否成功安装 inotifywait -h

 

编写shell脚本

#etc/rc.d/inotify.sh

inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib /data/wwwroot/shen/ | while read file
do
rsync -avH --progress --delete --password-file=/etc/rsyncd.pas /data/wwwroot/shen root@112.74.203.95::web
done

 

chmod +x /etc/rc.d/inotify.sh

执行脚本:/etc/rc.d/inotify.sh

 

posted @ 2016-12-27 14:21  MauriceChans  阅读(361)  评论(0编辑  收藏  举报