您的位置:首页 > 其它

rsync+inotify实现文件自动同步

2015-06-23 17:35 411 查看
单一的rsync工具仅可以进行数据同步,单一的Inotify仅可以实现实时文件监控,而两者的结合能满足企业对数据中心实时数据同步的要求。接下来我们用案例说明两者结合部署流程,例子中需要部署一套web服务,然后随着用户访问量的增加,单台服务器已经满足不了大量的并发访问。因此,采用集群技术,整合多台服务器处理能力实现负载均衡,从而满足并发访问量。由于web服务器所提供的网站数据需要保持一致,但当服务器越来越多时,公司发现在这些主机之间同步那些随时可能发生改变的网站数据非常困难。解决方案是在后端建立一个数据发布服务器,该服务器作为rsync客户端,通过Inotify机制实时监控网站的数据,当数据发生变化后调用rsync命令上传数据至多个rsync服务器,这里rsync服务器就是提供web服务的web服务器。首先需要在多台web服务器上部署rsync服务器,这些rsync服务器要能够提供客户端上传功能,以实现客户端主机将数据推送至rsync服务器,实现数据的实时同步功能。如图所示:

配置如下:Web1[root@localhost ~] yum -y install rsync[root@localhost ~] mkdir -p /var/www/001[root@localhost ~] chmod 660 /var/www/001[root@localhost ~] chown nobody:nobody /var/www/001[root@localhost ~]vim /etc/rsync.conf#!/etc/rsyncd.conftransfer logging = yeslog file=/var/log/rsync.logpid file=/var/run/rsyncd.pidlock file=/var/run/rsync.lockuid=nobodygid=nobodyuse chroot=noignore errorsread only=no[web1]comment = web contentlist=falsepath=/var/www/001auth users=tomsecrets file=/etc/rsyncd.secretshosts allow=10.10.10.132hosts deny=* [root@localhost ~] echo “tom:pass” > /ect/rsyncd.secrets[root@localhost ~] chmod 600 /etc/rsyncd.secrets[root@localhost ~] rsync --daemon[root@localhost ~] echo “rsync --daemon” >> /etc/rc.local如果有防火墙,注意防火墙端口开启873端口 Web2[root@localhost ~] yum -y install rsync[root@localhost ~] mkdir -p /var/www/002[root@localhost ~] chmod 660 /var/www/002[root@localhost ~] chown nobody:nobody /var/www/002[root@localhost ~]vim /etc/rsync.conf#!/etc/rsyncd.conftransfer logging = yeslog file=/var/log/rsync.logpid file=/var/run/rsyncd.pidlock file=/var/run/rsync.lockuid=nobodygid=nobodyuse chroot=noignore errorsread only=no[web2]comment = web contentlist=falsepath=/var/www/002auth users=tomsecrets file=/etc/rsyncd.secretshosts allow=10.10.10.132hosts deny=* [root@localhost ~] echo “tom:pass” > /ect/rsyncd.secrets[root@localhost ~] chmod 600 /etc/rsyncd.secrets[root@localhost ~] rsync --daemon[root@localhost ~] echo “rsync --daemon” >> /etc/rc.local如果有防火墙,注意防火墙端口开启873端口 在数据发布服务器(10.10.10.132)上需要下载inotify软件包,并编写监控脚本,这里的脚本名称为notify_rsync.sh。当监控到数据发生改变时,自动进行数据同步操作,将数据推送至web服务器[root@localhost ~] yum -y install rsync[root@localhost ~] yum -y install automake libtool[root@localhost ~] unzip inotify-tools-master.zip[root@localhost ~] Cd inotify-tools[root@localhost ~] ./autogen.sh configure.ac[root@localhost ~] ./configure [root@localhost ~] make && make install[root@localhost ~] echo "pass" > /etc/rsyncd.secrets[root@localhost ~] chmod 600 /etc/rsyncd.secrets[root@localhost ~] vim notify_rsync.sh#!/bin/bash#this rsync scripts based on inotify#date:2015-6-23#version:1.0export PATH=/bin:/usr/bin:/usr/local/binsrc=/tmp/dest1=web1dest2=web2client1=10.10.10.136client2=10.10.10.133user=tom#password file must not be other-accessiblepassfile=/etc/rsyncd.secrets[ ! -e $passfile ] && exit 2#wait for changeinotifywait -mrq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' --event modify,create,move,delete,attrib $src |while read filedoecho "$file" > /var/log/inotify_web 2>&1/usr/bin/rsync -avz --delete --progress --password-file=$passfile $src ${user}@$client1::$dest1 >> /var/logsync_web1 2>&1/usr/bin/rsync -avz --delete --progress --password-file=$passfile $src ${user}@$client2::$dest2 >> /var/logsync_web2 2>&1done[root@localhost ~] chmod a+x notify_rsync.sh[root@localhost ~] /root/notify_rsync.sh[root@localhost ~] Echo “/root/notify_rsync.sh” /etc/rc.local 向/tmp/下添加内容,去web1和web2上查看是否同步# cat /tmp/123Hello,world

参考资料:http://lxw66.blog.51cto.com/5547576/1331048

http://segmentfault.com/a/1190000002427568
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息