rsync+inotify一键安装脚本

一、环境描述

server:192.168.122.54

client:192.168.122.55,192.168.122.56

同步目录:/data/html

server端有任何数据更新,即将同步到client端,实时同步

二、采用方法:rsync+inotify

三、关于inotify原理,请参考https://www.ibm.com/developerworks/cn/linux/l-inotify/

四、操作过程

  4.1服务端脚本

 
 
  1. #!/bin/bash 
  2.  
  3. yum install rsync -y 
  4. mkdir -p /data/html #如果要同步的不是此目录,可以根据实际需要添加目录
  5. #wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 
  6. tar xzvf inotify-tools-3.14.tar.gz 
  7. cd inotify-tools-3.14 
  8. ./configure 
  9. make 
  10. make install 
  11.  
  12. #cponfigure inotify 
  13.  
  14. cat >>/home/rsync.sh << EOF 
  15. #!/bin/bash 
  16. src=/data/html/   #同步的源目录
  17. des=www 
  18. host="192.168.122.55 192.168.122.56" 
  19. /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib \$src | while read files 
  20. do 
  21. for hostip in \$host 
  22. do 
  23. rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets \$src root@\$hostip::\$des 
  24. done 
  25. echo "\${files} was rsynced" >>/tmp/rsync.log 2>&1 
  26. done 
  27. EOF 
  28.  
  29. #confiugre secret 
  30. cat >> /etc/rsyncd.secrets << EOF 
  31. 123456 
  32. root:123456 
  33. EOF 
  34. chmod 0600 /etc/rsyncd.secrets 
  35.  
  36. #setting running onboot 
  37. echo "nohup /bin/bash /home/rsync.sh &" >> /etc/rc.local 
  38. nohup /bin/bash /home/rsync.sh & 

4.2 客户端脚本

 
 
  1. #!/bin/bash 
  2.  
  3.  
  4. yum install rsync -y 
  5. mkdir -p /data/html 
  6.  
  7. #configure rsyncd daemon 
  8. cat >> /etc/rsyncd.conf  << EOF 
  9. uid = root 
  10. gid = root 
  11. use chroot = no 
  12. max connections = 5 
  13. pid file = /var/run/rsyncd.pid 
  14. lock file = /var/run/rsync.lock 
  15. log file = /var/log/rsyncd.log 
  16.  
  17. [www] 
  18. path=/data/html/ 
  19. comment = update 
  20. ignore errors 
  21. read only = no 
  22. list = no 
  23. hosts allow = 192.168.122.0/24 
  24. auth users = root 
  25. uid = root 
  26. gid = root 
  27. secrets file = /etc/rsyncd.secrets 
  28. EOF 
  29.  
  30. #configure secret 
  31. cat >> /etc/rsyncd.secrets << EOF 
  32. 123456 
  33. root:123456 
  34. EOF 
  35. chmod 0600 /etc/rsyncd.secrets 
  36. echo "rsync --daemon" >> /etc/rc.local 
  37. rsync --daemon 

五、测试过程。

略过测试过程,大家可以自己测试同步效果。

六、附一键安装包

 

 
 
  1. svn钩子自动更新post-commit  
  2. for dir in $(svnlook dirs-changed /var/svn/web/) 
  3. do 
  4.     svn update -N /opt/nginx/web/$dir --username "admin" --password "admin" 
  5. done 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值