soft:rsync-3.0.8.tar.gz inotify-tools-3.14.tar.gz

wget http://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
server:192.168.2.211
bakserver:192.168.2.67 
bakserver(被同步端):

 
  
  1. rsync安装 
  2. tar xzvf rsync-3.0.8.tar.gz 
  3. cd rsync-3.0.8 
  4. ./configure 
  5. make 
  6. make install 
  7.  
  8. mkdir /etc/rsyncd 
  9. vim rsyncd.conf 
  10.  
  11. uid = root 
  12. gid = root 
  13. user chroot = no 
  14. max connections = 50 
  15. timeout = 180 
  16. pid file = /etc/rsyncd/rsyncd.pid 
  17. lock file= /etc/rsyncd/rsyncd.lock 
  18. log file = /var/log/rsyncd.log 
  19. transfer logging = yes 
  20. log format = %t %a %m %f %b 
  21. syslog facility = local3 
  22. secrets file = /etc/rsyncd/rsyncd.password 
  23.  
  24. [nagios] 
  25. path=/usr/local/nagios/ 
  26. ignore errors 
  27. hosts allow = 192.168.0.0/22 
  28. hosts deny = 0.0.0.0/32 
  29. secrets file = /etc/rsyncd/rsyncd.password 
  30. read only = no 
  31. list = no 
  32. auth users = yunwei 
    保存退出。
    echo "yunwei:123456" >/etc/rsyncd/rsyncd.password #yunwei:123456,前者为认证用户名,后者为认证密码。
    chmod 600 /etc/rsyncd/rsyncd.password #安全考虑

    启动服务:rsync --daemon --config=/etc/rsyncd/rsyncd.conf
    加入自启动:ehoc "rsync --daemon --config=/etc/rsyncd/rsyncd.conf" >/etc/rc.d/rc.local

server:
 

 
  
  1. rsync安装 
  2. tar xzvf rsync-3.0.8.tar.gz 
  3. cd rsync-3.0.8 
  4. ./configure 
  5. make 
  6. make install 
  7.  
  8. tar xvzf inotify-tools-3.14.tar.gz 
  9. cd inotify-tools-3.14 
  10. ./configure 
  11. make 
  12. make install 
  13.  
  14. ll /usr/local/bin/inotifywa* 
  15. -rwxr-xr-x 1 root root 37264 04-14 13:42 /usr/local/bin/inotifywait 
  16. -rwxr-xr-x 1 root root 35438 04-14 13:42 /usr/local/bin/inotifywatch 
  17. inotify-tools安装完成后,会生成inotifywait和inotifywatch两个指令,其中, 
  18. inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。 
  19. inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。 
  20.  
  21. 创建认证密码: 
  22. mkdir /etc/rsyncd 
  23. echo "123456" >/etc/rsyncd/rsyncd.password     #此处只需密码即可。 
  24. chmod 600 /etc/rsyncd/rsyncd.password 

客户端运行脚本:

 
  
  1. vim nagios_rsync.sh 
  2. #!/bin/bash 
  3. src=/usr/local/nagios/ 
  4. des=nagios           #此处为模块名称。 
  5. ip=192.168.2.67 
  6. /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read file 
  7. do 
  8. rsync -vzrtopg --delete --progress $src yunwei@$ip::$des --password-file=/etc/rsyncd/rsyncd.password >/dev/null 2>&1 
  9. done 
  10.  
  11. 保存,赋予执行权限。 
  12.  
  13. 后台运行: 
  14. nohup ./nagios_rsync.sh & 
  15.  
  16. 加入自启动: 
  17. echo "nohup ./nagios_rsync.sh &" >>/etc/rc.d/rc.local  

测试:
在客户端nagios目录下创建,删除文件,在服务端立即可以看到,证明实施同步正常。