首页 > 其他 > 详细

rsync+inotify 实现数据实时同步

时间:2015-06-20 02:20:50      阅读:284      评论:0      收藏:0      [点我收藏+]
前言:
     rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

一、基本环境

系统:CentOS 2.6.32-220.el6.x86_64
软件包版本:rsync-3.0.6-12.el6.x86_64
                   inotify-tools-3.14 
下载链接:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

服务端(server):172.16.32.204
客服端(client1):172.16.32.205
          (client2):172.16.32.206

二、客户端配置(172.16.32.205、172.163.32.206)

1. client1 172.16.32.205配置

安装rsync
#yum install -y rsync xinetd

在/etc/目录下建立rsyncd.conf配置文件进行编辑
#vim /etc/rsyncd.conf 

uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[lixuan]
path = /data/lixuan/          #本地自定义路径
comment = client file
ignore errors
read only = no
write only = no
hosts allow = 172.16.32.204       #服务端IP地址
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /etc/client1.pass     #自动同步密码文件

新建/etc/client1.pass  文件
#echo "root:123456" >>/etc/client1.pass
#chmod 600 /etc/client1.pass


启动rsync服务
#/etc/init.d/xinetd start

2. client2 172.16.32.206配置

安装rsync
#yum install -y rsync xinetd

在/etc/目录下建立rsyncd.conf配置文件进行编辑
#vim /etc/rsyncd.conf 

uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[lixuan]
path = /data/lixuan/          #本地自定义路径
comment = client file
ignore errors
read only = no
write only = no
hosts allow = 172.16.32.204      #服务端IP地址
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /etc/client2.pass     #自动同步密码文件

保存退出!

新建/etc/client2.pass  文件
#echo "root:123456" >>/etc/client2.pass
chmod 600 /etc/client2.pass


启动rsync服务
#/etc/init.d/xinetd start

三、服务端配置(172.16.32.204)

1.安装rsync 

#yum install -y rsync xinetd


在/etc/目录下建立rsyncd.conf配置文件进行编辑
#vim /etc/rsyncd.conf 

uid = root
gid = root
use chroot = no
max connections = 100
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/server.pass
[lixuan]
path = /data/lixuan/
auth users = root
list = no
read only = no
secrets file = /etc/servers.pass
comment = server directory

保存退出!

新建/etc/server.pass  文件
#echo "root:123456" >>/etc/server.pass
#chmod 600 /etc/server.pass


启动rsync服务
#/etc/init.d/xinetd start

2. 安装inotify

验证内核是否支持inotify
#uname -r
2.6.32-220.el6.x86_64

#ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jun 11 10:15 max_queued_events    #表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值
-rw-r--r-- 1 root root 0 Jun 11 10:15 max_user_instances     #表示每一个real user ID可创建的inotify instatnces的数量上限
-rw-r--r-- 1 root root 0 Jun 11 10:15 max_user_watches      #表示每个inotify instatnces可监控的最大目录数量

配置服务端内容发布脚本
#vim /data/sh/inotifyrsync.sh

#!/bin/bash
client1=172.16.32.205
client2=172.16.32.206
src=/data/lixuan/
dst=lixuan
user=root

/usr/local/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f%e‘ -e close_write,modify,delete,create,attrib $src | while read files
        do
  /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client1::$dst
  /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client2::$dst
                echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
         done

保存退出!

新建/etc/client.pass  文件
#echo "123456" >>/etc/client.pass
#chmod 600 /etc/client.pass

                                                                
赋予脚本执行权限
#chmod 755 /data/sh/inotifyrsync.sh

后台执行脚本
#sh /data/sh/inotifyrsync.sh &

将此脚本加入开机自启动文件中
#echo "/data/sh/inotifyrsync.sh &"  >> /etc/rc.local

四、测试rsync+inotify数据实时同步

      在服务端(172.16.32.204)的/data/lixuan/目录中添加删除目录或文件,然后进入客户端(172.16.32.205、172.16.32.206)的/data/lixuan/目录中查看是否和服务端数据实时保持一致。












rsync+inotify 实现数据实时同步

原文:http://blog.chinaunix.net/uid-26999549-id-5080212.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!