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

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

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

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

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

相关内容