rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
一、基本环境
软件包版本:
01.rsync-3.0.6-12.el6.x86_64 02.inotify-tools-3.14
服务器信息
01.服务端(server):172.16.1.102.客服端(client1):172.16.1.203.客服端(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运行权限为root20.gid = root //设置rsync运行权限为root21.auth users = root //模块验证用户名称,可使用空格或者逗号隔开多个用户名22.secrets file = /etc/client1.pass #自动同步密码文件
新建/etc/client1.pass 文件
01.#echo "root:123456" >>/etc/client1.pass02.#chmod -R 600 /etc/client1.pass #这个 必须是 600权限 要不就会提示 找不到文件
启动rsync服务
01.#/etc/init.d/xinetd start02.#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 = nobody03.gid = nobody04.use chroot = no05.max connections = 1006.strict modes = yes07.pid file = /var/run/rsyncd.pid08.lock file = /var/run/rsync.lock09.log file = /var/log/rsyncd.log10.[lixuan]11.path = /data/lixuan/ #本地自定义路径12.comment = client file13.ignore errors14.read only = no15.write only = no16.hosts allow = 172.16.32.204 #服务端IP地址17.hosts deny = *18.list = false19.uid = root20.gid = root21.auth users = root22.secrets file = /etc/client2.pass #自动同步密码文件
保存退出!
新建/etc/client2.pass 文件
01.#echo "root:123456" >>/etc/client2.pass02.chmod -R 600 /etc/client2.pass #这个 必须是 600权限 要不就会提示 找不到文件
启动rsync服务
01.#/etc/init.d/xinetd start02.#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 = root03.gid = root04.use chroot = no05.max connections = 10006.log file = /var/log/rsyncd.log07.pid file = /var/run/rsyncd.pid08.lock file = /var/run/rsync.lock09.secrets file = /etc/server.pass10.[lixuan]11.path = /data/lixuan/12.auth users = root13.list = no14.read only = no15.secrets file = /etc/servers.pass16.comment = server directory
保存退出!
新建/etc/server.pass 文件
01.#echo "root:123456" >>/etc/server.pass02.#chmod -R 600 /etc/server.pass #这个 必须是 600权限 要不就会提示 找不到文件
启动rsync服务
01.#/etc/init.d/xinetd start
2. 安装inotify
验证内核是否支持inotify
01.#uname -r02.2.6.32-220.el6.x86_6403.#ll /proc/sys/fs/inotify04.total 005.-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.sh02.#!/bin/bash03.client1=172.16.1.204.client2=172.16.1.305.src=/data/lixuan/06.dst=lixuan07.user=root08./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 files09. do10. /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client1::$dst11. /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client2::$dst12. echo "${files} was rsynced" >>/tmp/rsync.log 2>&113. done
保存退出!
新建/etc/client.pass 文件
01.#echo "123456" >>/etc/client.pass02.#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.local02.#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
相关推荐
- centos7安装CUDA Tookit+CUDA Samples+NCCL+OpenMPI
- centos7添加交换分区swap
- centos7升级systemd并切换cgroup v1到cgroup v2
- centos7下程序运行提示报错version `GLIBCXX_3.4.20` not found
- centos7二进制部署prometheus+alertmanager+grafana
- centos7使用chrony配置时间同步
- centos7使用二进制部署TLS加密etcd集群
- centos7二进制部署etcd集群
- centos7备份和恢复单机etcd数据
- centos7二进制部署单机etcd
评论列表