1、两台服务器IP地址分别为:
  源服务器:192.168.1.2
  目标服务器:192.168.1.3
  @todo:从源服务器(192.168.1.2)的/www/目录下的所有的文件实时同步到目标服务器(192.168.1.3)的/www_bak/目录下/test_bak/下
  从源的/test/目录下的所有文件实时同步到目标服务器(192.168.1.3)的
  源服务器下需要安装rsync和inotify,源服务器做为server端,实时的向目标服务器client端发送数据
  2、安装 rsync
  一般centos6.5下都已经安装了rsync,所以就不必安装了,可以用以下命令检查一下是否已安装:
  rpm -qa |grep rsync
  
  上图显示了我的机器上安装的是rsync-3.0.6-12。
  如果没有安装请往下看,如果已安装,那就跳过下面的部分:
  cd /usr/local/src
  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 --prefix=/usr/local/rsync
  make
  make install
  rsync已安装完毕
  3、创建同步文件所需要的密码文件,这样做是为了安全
  touch /etc/rsyncd.secrets
  echo 'newpassword' > /etc/rsyncd.secrets
  注:这里的newpassword可以是任意字符
  出于安全考虑要把此文件的权限改成600:
  chmod 600 /etc/rsyncd.secrets
  4、安装inotify
  先查看服务器是否支持inotify
  ll /proc/sys/fs/inotify
  
  会有三个文件,这说明此服务器是支持 inotify的。
  下面安装inotify:
  wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 
 tar zxvf inotify-tools-3.14.tar.gz 
 cd inotify-tools-3.14 
 ./configure --prefix=/usr/local/inotify 
 make
 make install
  5、创建rsync复制脚本,用户shell来实现,其功能就是:从源服务器(192.168.1.2)的/www/目录下的所有的文件无论是添加、修改、删除文件,能够通过inotify监控到,并通过rsync实时同步到目标服务器(192.168.1.3)的/www_bak/目录下
  vim /usr/bin/rsync.sh
  #!/bin/bash
 host=192.168.1.3
 src=/www/
 des=web
 user=webuser
 /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
 | while read files
 do
 /usr/bin/rsync -zrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src $user@$host::$des
 echo "${files} was rsynced" > /var/log/rsyncd.log 2>&1
 done
  6、创建rsync复制脚本,用户shell来实现,其功能就是:从源服务器(192.168.1.2)的/test/目录下的所有的文件无论是添加、修改、删除文件,能够通过inotify监控到,并通过rsync实时同步到目标服务器(192.168.1.3)的/www_bak/目录下
  vim /usr/bin/rsync-b.sh
  #!/bin/bash
 host=192.168.1.3
 src=/test/
 des=weba
 user=webuser
 /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
 | while read files
 do
 /usr/bin/rsync -zrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src $user@$host::$des
 echo "${files} was rsynced" > /var/log/rsyncd.log 2>&1
 done
  其中host是目标服务器的ip,src是源服务器要同步的目录,des是认证模块名,需要与目标服务器一致,user是建立密码文件里的认证用户。
  修改rsync.sh的权限
  chmod +x /usr/bin/rsync-b.sh
  到此为止,源服务器的所有操作就完成了。下面配置目标服务器。
  1、目标服务器也要安装 rsync,安装方式跟源服务器一样,这里就不在赘述了。
  2、建立密码文件:
  touch /etc/rsyncd.secrets
  echo "webuser:newpassword" > /etc/rsyncd.secrets
  同样要给此文件一个600的权限
  chmod 600 /etc/rsyncd.secrets
  注:在源服务器建立的密码文件,只有密码,没有用户名;而在目标服务器里建立的密码文件,用户名与密码都有。
  3、写rsync的配置文件:
  vim /etc/rsyncd.conf
  1、目标服务器也要安装 rsync,安装方式跟源服务器一样,这里就不在赘述了。
  2、建立密码文件:
  touch /etc/rsyncd.secrets
  echo "webuser:newpassword" > /etc/rsyncd.secrets
  同样要给此文件一个600的权限
  chmod 600 /etc/rsyncd.secrets
  注:在源服务器建立的密码文件,只有密码,没有用户名;而在目标服务器里建立的密码文件,用户名与密码都有。
  3、写rsync的配置文件:
  vim /etc/rsyncd.conf
  uid = root
 gid = root
 use chroot = no
 max connections = 10
 strict modes = yes
 pid file = /var/run/rsyncd.pid
 lock file = /var/run/rsync.lock
 log file = /var/log/rsyncd.log
 #log format = %t %a %m %f %b # 日志记录格式
 [web]
 path = /www_bak/
 comment = web file
 ignore errors
 read only = no
 write only = no
 hosts allow = 192.168.1.2
 hosts deny = *
 list = false
 uid = root
 gid = root
 auth users = webuser
 secrets file = /etc/rsyncd.secrets
  [weba]
 path = /test_bak/
 comment = web file
 ignore errors
 read only = no
 write only = no
 hosts allow = 192.168.1.2
 hosts deny = *
 list = false
 uid = root
 gid = root
 auth users = webuser
 secrets file = /etc/rsyncd.secrets
  目标服务器启动 rsync
  /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
  5、源服务器启动同步:
  /usr/bin/rsync.sh &
  /usr/bin/rsync-b.sh &
  到这里,所有的都已完成。可以到源服务器下的/www目录下建一个文件,然后再看一下目标服务器下的/www_bak/下是否有?