rsync+inotify一键安装脚本

字体大小: 中小 标准 ->行高大小: 标准

一、环境描述

server:192.168.122.54

client:192.168.122.55,192.168.122.56

同步目录:/data/html

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

二、采用方法:rsync+inotify

三、关于inotify原理(略)

四、操作过程

4.1服务端脚本

  1. #!/bin/bash
  2. yum install rsync -y mkdir -p /data/html #如果要同步的不是此目录,可以根据实际需要添加目录
  3. #wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar xzvf inotify-tools-3.14.tar.gz
  4. cd inotify-tools-3.14 ./configure
  5. make make install
  6. #cponfigure inotify
  7. cat >>/home/rsync.sh <<EOF
  8. #!/bin/bash src=/data/html/ #同步的源目录
  9. des=www host="192.168.122.55 192.168.122.56"
  10. /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
  11. for hostip in \$host do
  12. rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets \$src root@\$hostip::\$des done
  13. echo "\${files} was rsynced" >>/tmp/rsync.log 2>&1 done
  14. EOF
  15. #confiugre secret cat >> /etc/rsyncd.secrets <<EOF
  16. 123456 root:123456
  17. EOF chmod 0600 /etc/rsyncd.secrets
  18. #setting running onboot
  19. echo "nohup /bin/bash /home/rsync.sh &" >> /etc/rc.local nohup /bin/bash /home/rsync.sh &

4.2 客户端脚本

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

五、测试过程。
 
略过测试过程,大家可以自己测试同步效果。
 
六、附一键安装包

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/11月/21日/rsync+inotify一键安装脚本

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/64847.html