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/目录中检视是否和服务端资料实时保持一致。