linux rsync +inotify 实现实时同步

本文发布时间: 2019-Mar-22
前言: rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。一、基本环境系统:CentOS2.6.32-220.el6.x86_64软件包版本:rsync-3.0.6-12.el6.x86_64 inotify-tools-3.14下载链接:百度 inotify-tools-3.14 服务端(server):172.16.1.1客服端(client1):172.16.1.2 (client2):172.16.1.3二、客户端配置(172.16.1.2、172.16.1.3)1. client1 172.16.1.2配置安装rsync#yum install -y rsync xinetd在/etc/目录下建立rsyncd.conf配置文件进行编辑#vim /etc/rsyncd.confuid = nobody //rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,gid = nobody //rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,use chroot = no //允许 chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下 #chroot为yes时必须使用root权限,且不能备份path路径外的链接文件max connections = 10 //最大连接数strict modes = yes //是否检查口令文件的权限pid file = /var/run/rsyncd.pid //pid文件的存放位置lock file = /var/run/rsync.lock //支持max connections参数的锁文件log file = /var/log/rsyncd.log //日志文件位置,启动rsync后自动产生这个文件,无需提前创建[lixuan] //自定义名称path = /data/lixuan/ #本地自定义路径comment = client file //模块名称与自定义名称相同ignore errors //忽略错误read only = no //设置rsync服务端文件为读写权限write only = no //设置rsync服务端文件为读写权限hosts allow = 172.16.1.1 #服务端IP地址hosts deny = * //止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开list = false //不显示rsync服务端资源列表uid = root //设置rsync运行权限为rootgid = root //设置rsync运行权限为rootauth users = root //模块验证用户名称,可使用空格或者逗号隔开多个用户名secrets file = /etc/client1.pass #自动同步密码文件新建/etc/client1.pass 文件#echo "root:123456" >>/etc/client1.pass#chmod -R 600 /etc/client1.pass #这个 必须是 600权限 要不就会提示 找不到文件启动rsync服务#/etc/init.d/xinetd start#rsync --daemon --config=/etc/rsync.conf2. client2 172.16.1.3配置安装rsync#yum install -y rsync xinetd在/etc/目录下建立rsyncd.conf配置文件进行编辑#vim /etc/rsyncd.confuid = nobodygid = nobodyuse chroot = nomax connections = 10strict modes = yespid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log[lixuan]path = /data/lixuan/ #本地自定义路径comment = client fileignore errorsread only = nowrite only = nohosts allow = 172.16.32.204 #服务端IP地址hosts deny = *list = falseuid = rootgid = rootauth users = rootsecrets file = /etc/client2.pass #自动同步密码文件保存退出!新建/etc/client2.pass 文件#echo "root:123456" >>/etc/client2.passchmod -R 600 /etc/client2.pass#这个 必须是 600权限 要不就会提示 找不到文件启动rsync服务#/etc/init.d/xinetd start#rsync --daemon --config=/etc/rsync.conf三、服务端配置(172.16.1.1)1.安装rsync#yum install -y rsync xinetd在/etc/目录下建立rsyncd.conf配置文件进行编辑#vim /etc/rsyncd.confuid = rootgid = rootuse chroot = nomax connections = 100log file = /var/log/rsyncd.logpid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locksecrets file = /etc/server.pass[lixuan]path = /data/lixuan/auth users = rootlist = noread only = nosecrets file = /etc/servers.passcomment = serverdirectory保存退出!新建/etc/server.pass 文件#echo "root:123456" >>/etc/server.pass#chmod -R 600 /etc/server.pass#这个 必须是 600权限 要不就会提示 找不到文件启动rsync服务#/etc/init.d/xinetd start2. 安装inotify验证内核是否支持inotify#uname -r2.6.32-220.el6.x86_64#ll /proc/sys/fs/inotifytotal 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/bashclient1=172.16.1.2client2=172.16.1.3src=/data/lixuan/dst=lixuanuser=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 -R 600 /etc/client.pass#这个 必须是 600权限 要不就会提示 找不到文件 赋予脚本执行权限#chmod 755 /data/sh/inotifyrsync.sh后台执行脚本#sh /data/sh/inotifyrsync.sh &将此脚本加入开机自启动文件中#echo "/data/sh/inotifyrsync.sh &" >>/etc/rc.local#rsync --daemon --config=/etc/rsync.conf四、测试rsync+inotify数据实时同步 在服务端(172.16.1.1)的/data/lixuan/目录中添加删除目录或文件,然后进入客户端(172.16.1.1、172.16.1.2)的/data/lixuan/目录中查看是否和服务端数据实时保持一致。参考文章http://www.tuicool.com/articles/IB3I7rm


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:09pm
栏目列表