centos6配置rsync+inotify实现实时同步

rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

一、基本环境

软件包版本:

01.
rsync-3.0.6-12.el6.x86_64 
02.
inotify-tools-3.14

服务器信息

01.
服务端(server):172.16.1.1
02.
客服端(client1):172.16.1.2
03.
客服端(client2):172.16.1.3

二、客户端配置

1. client1 172.16.1.2配置

安装rsync

01.
#yum install -y rsync xinetd

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

01.
#vim /etc/rsyncd.conf 
02.
uid = nobody         //rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,
03.
gid = nobody         //rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,
04.
use chroot = no    //允许 chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下 #chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
05.
max connections = 10  //最大连接数
06.
strict modes = yes   //是否检查口令文件的权限
07.
pid file = /var/run/rsyncd.pid  //pid文件的存放位置
08.
lock file = /var/run/rsync.lock  //支持max connections参数的锁文件
09.
log file = /var/log/rsyncd.log   //日志文件位置,启动rsync后自动产生这个文件,无需提前创建
10.
[lixuan]   //自定义名称
11.
path = /data/lixuan/          #本地自定义路径
12.
comment = client file   //模块名称与自定义名称相同
13.
ignore errors             //忽略错误
14.
read only = no         //设置rsync服务端文件为读写权限
15.
write only = no        //设置rsync服务端文件为读写权限
16.
hosts allow = 172.16.1.1       #服务端IP地址
17.
hosts deny = *                    //止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
18.
list = false                           //不显示rsync服务端资源列表
19.
uid = root                           //设置rsync运行权限为root
20.
gid = root                          //设置rsync运行权限为root
21.
auth users = root               //模块验证用户名称,可使用空格或者逗号隔开多个用户名
22.
secrets file = /etc/client1.pass     #自动同步密码文件

新建/etc/client1.pass  文件

01.
#echo "root:123456" >>/etc/client1.pass
02.
#chmod -R 600 /etc/client1.pass   #这个 必须是 600权限  要不就会提示 找不到文件

启动rsync服务

01.
#/etc/init.d/xinetd start
02.
#rsync --daemon --config=/etc/rsync.conf

2. client2配置

安装rsync

01.
#yum install -y rsync xinetd

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

01.
#vim /etc/rsyncd.conf 
02.
uid = nobody
03.
gid = nobody
04.
use chroot = no
05.
max connections = 10
06.
strict modes = yes
07.
pid file = /var/run/rsyncd.pid
08.
lock file = /var/run/rsync.lock
09.
log file = /var/log/rsyncd.log
10.
[lixuan]
11.
path = /data/lixuan/          #本地自定义路径
12.
comment = client file
13.
ignore errors
14.
read only = no
15.
write only = no
16.
hosts allow = 172.16.32.204      #服务端IP地址
17.
hosts deny = *
18.
list = false
19.
uid = root
20.
gid = root
21.
auth users = root
22.
secrets file = /etc/client2.pass     #自动同步密码文件

保存退出!

新建/etc/client2.pass  文件

01.
#echo "root:123456" >>/etc/client2.pass
02.
chmod -R 600 /etc/client2.pass #这个 必须是 600权限  要不就会提示 找不到文件

启动rsync服务

01.
#/etc/init.d/xinetd start
02.
#rsync --daemon --config=/etc/rsync.conf

三、服务端配置(172.16.1.1)

1.安装rsync 

01.
#yum install -y rsync xinetd

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

01.
#vim /etc/rsyncd.conf 
02.
uid = root
03.
gid = root
04.
use chroot = no
05.
max connections = 100
06.
log file = /var/log/rsyncd.log
07.
pid file = /var/run/rsyncd.pid
08.
lock file = /var/run/rsync.lock
09.
secrets file = /etc/server.pass
10.
[lixuan]
11.
path = /data/lixuan/
12.
auth users = root
13.
list = no
14.
read only = no
15.
secrets file = /etc/servers.pass
16.
comment = server directory

保存退出!

新建/etc/server.pass  文件

01.
#echo "root:123456" >>/etc/server.pass
02.
#chmod -R 600 /etc/server.pass #这个 必须是 600权限  要不就会提示 找不到文件

启动rsync服务

01.
#/etc/init.d/xinetd start

2. 安装inotify

验证内核是否支持inotify

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

配置服务端内容发布脚本

01.
#vim /data/sh/inotifyrsync.sh
02.
#!/bin/bash
03.
client1=172.16.1.2
04.
client2=172.16.1.3
05.
src=/data/lixuan/
06.
dst=lixuan
07.
user=root
08.
/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
09.
        do
10.
          /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client1::$dst
11.
          /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client2::$dst
12.
          echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
13.
        done

保存退出!

新建/etc/client.pass  文件

01.
#echo "123456" >>/etc/client.pass
02.
#chmod -R 600 /etc/client.pass #这个 必须是 600权限  要不就会提示 找不到文件

 赋予脚本执行权限

01.
#chmod 755 /data/sh/inotifyrsync.sh

后台执行脚本

01.
#sh /data/sh/inotifyrsync.sh &

将此脚本加入开机自启动文件中

01.
#echo "/data/sh/inotifyrsync.sh &"  >> /etc/rc.local
02.
#rsync --daemon --config=/etc/rsync.conf

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

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


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/380

评论列表