服务器端(代码更新服务器):CentOS release 5.8 IP地址:192.168.1.12

客户端(web服务器):Oracle Linux Server release 5.8;IP地址:192.168.1.13

       由于没有共享存储,现在要的是对它们的根目录/data/httpd/www实现即时同步更新。

       根目录均为/data/httpd/www/,自动同步顺序为192.168.1.12--->192.168.1.13;我们只要把192.168.1.12设置成为rsyncd服务器端就可以

①配置客户端192.168.2.13(只要安装rsync)

wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz

tar zxvf rsync-3.0.9.tar.gz

cd rsync-3.0.9

./configure && make && make install

配置rsyncd.conf的详细内容,Server端和client端都要配置vim /etc/rsyncd.conf

 

uid = root
gid = root
use chroot = no
max connections = 5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[www]
path=/data/httpd/www/
comment = update
ignore errors
read only = no
list = no
hosts allow = 192.168.1.0/255.255.255.0
auth users = root
uid = root
gid = root
secrets file = /etc/rsyncd.secrets

②创建rsync证文件;Server端和client端都要配置
vim /etc/rsyncd.secrets
123456
root:123456

rsyncd.conf以及rsyncd.secrets的内容同Server端一样

rsync --daemon      启动服务

 echo "rsync --daemon"  >> /etc/rc.d/rc.local   #开机自启动

③服务端192.168.2.12(rsync+inotify)

wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz

tar zxvf rsync-3.0.9.tar.gz

cd rsync-3.0.9

./configure && make && make install

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

④编写同步更新脚本
vim /root/rsync.sh

_____________________________________________________________________

#!/bin/bash
   src=/data/httpd/www/
   des=www
   host="192.168.1.13"
   /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read files
   do
     for hostip in $host
     do
        rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src
root@$hostip::$des
     done
     echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
   done
_________________________________________________________________________

最后,我们将此脚本放入后台运行,输入如下命令就可以

nohup sh /root/rsync.sh &

开机自动启动

echo "nohup sh /root/rsync.sh &" >> /etc/rc.d/rc.local

 

 

 



            





 

 

注意;为了保证/data/httpd/www这个目录可以自动同步,此目录建议分配777权限;/etc/rsyncd.secrets分配600

关闭防火墙 /etc/init.d/iptables stop