说明:使用服务器版本CentOS5.5_x64,Web1-2是服务端安装rsync,server是客户端安装 inotify-tools,规划如下:
1. 安装rsync与inotify-tools
[root@web1~]# rpm -q rsync
rsync-2.6.8-3.1
[root@web2~]# rpm -q rsync
rsync-2.6.8-3.1
[root@server~]# tar zxf inotify-tools-3.14.tar.gz
[root@server~]# cd inotify-tools-3.14
[root@serverinotify-tools-3.14]# ./configure ;make ;make install
2. 在2个服务节点配置rsync(在web1/web2的操作)
[root@web1~]# vi /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
pid file =/var/run/rsyncd.pid
lock file =/var/run/rsync.lock
log file =/var/log/rsyncd.log
[www]
path = /var/www/html/
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = 192.168.1.0/24
list = false
uid = root
gid = root
auth users = webuser
secrets file = /etc/rsyncd_users.db
[root@web1~]# echo "123" >> /etc/rsyncd_users.db
[root@web1~]# echo "webuesr:123" >> /etc/rsyncd_users.db
[root@web1~]# chmod 600 /etc/rsyncd_users.db
[root@web1~]# /usr/bin/rsync --daemon
[root@web1~]# netstat -anpt |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 9182/rsync
[root@web1~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local
3 配置内容发布节点(在Server上的操作)
配置内容发布节点的主要工作是将生成的静态网页实时的同步到二个服务节点,这个过程可以通过一个shell脚本来完成
[root@server~]# vi /usr/local/inotify/inotify.sh
#!/bin/bash
host="192.168.1.200192.168.1.201"
src=/web/data/
dst=www
user=webuser
/usr/local/bin/inotifywait-mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -emodify,delete,create,attrib $src \
| while read files
do
for hostip in $host
do
/usr/bin/rsync -vzrtopg --delete--progress --password-file=/etc/rsyncd_users.db $src $user@$hostip::$dst&> /dev/null
echo "${files} wasrsynced" >>/tmp/rsync.log 2>&1
done
done
[root@server~]# echo "webuser:123" > /etc/rsyncd_users.db
[root@server~]# chmod 600 /etc/rsyncd_users.db
[root@server~]# /usr/local/inotify/inotify.sh &
[root@server~]# echo "/usr/local/inotify/inotify.sh &" >> /etc/rc.local
4.测试是否同步
[root@server~]# touch /web/data/index.html
[root@web1~]# ls /var/www/html/
index.html
[root@web2~]# ls /var/www/html/
index.html
以上可以看到同步成功了!
有时会遇到这样的情况:向inotify监控的目录(这里是/web/data/)写入一个很大文件时,由于写入这个大文件需要一段时间,此时inotify就会持续不停的输出该文件被更新的信息,这样就会持续不停的触发rsync去执行同步操作,占用了大量系统资源,那么针对这种情况,最理想的做法是等待文件写完后再去触发rsync同步。 在这种情况下,可以修改inotify的监控事件,即:“-e close_write,delete,create,attrib”。