rsync+inotify實現實時同步

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(被同步端):ios

  
  
           
  
  
  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:
 git

  
  
           
  
  
  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 

客戶端運行腳本:github

  
  
           
  
  
  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目錄下建立,刪除文件,在服務端當即能夠看到,證實實施同步正常。vim

相關文章
相關標籤/搜索