rsync+inotify实现文件时实同步

由192.168.0.5上inotify服务监测文件是否有更新,如果有更新(修改,删除,新建)inotify就会通过rsync命令将更新的文件推向三台web服务器
1、在三台web上配置rsync服务
#mkdir -p /data/httpd/wwwroot #创建web目录
#vim /etc/rsyncd.conf #配置文件
-
1 uid = root 2 gid = root 3 use chroot = no 4 max connections =5 5 pid file =/var/run/rsyncd.pid 6 lock file =/var/run/rsync.lock 7 log file =/var/log/rsyncd.log 8 [www] 9 path=/data/httpd/wwwroot/ 10 comment = update 11 ignore errors 12 read only = no 13 list = no 14 hosts allow =192.168.0.0/255.255.255.0 15 auth users = root 16 uid = root 17 gid = root 18 secrets file =/etc/rsyncd.secrets
#vim /etc/rsyncd.secrets #创建rsync证文件
123456
root:123456
#chmod 0600 /etc/rsyncd.secrets #设置权限
#rsync --daemon #启动服务
#echo "rsync --daemon" >> /etc/rc.local #开机自启动
2、配置服务端(rsync+inotify)
#mkdir -p /data/httpd/wwwroot #创建目录存放代码
(1)、安装inotify
#cd /usr/local/src/
#wget http://cloud.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
(2)、编写同步更新脚本
-
1 #!/bin/bash 2 src=/data/httpd/wwwroot/ 3 des=www 4 host="192.168.0.6 192.168.0.7 192.168.0.8" 5 /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M'--format '%T %w%f%e'-e close_write,delete,create,attrib $src |while read files 6 do 7 for hostip in $host 8 do 9 rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src root@$hostip::$des 10 done 11 echo "${files} was rsynced">>/tmp/rsync.log 2>&1 12 done
(3)、创建rsync认证文件
#scp from rsync server
(4)、启动inotify监控脚本
#nohup /bin/bash /root/bin/rsync.sh &
#echo "nohup /bin/bash /root/bin/rsync.sh &" >> /etc/rc.local
(5)、测试
在192.168.0.5上进入目录/data/httpd/wwwroot/
#touch a.txt
在三台web机上/data/httpd/wwwroot/目录可以立马看a.txt。