加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

rsync+inotify实时同步环境部署记录

发布时间:2020-12-14 01:42:44 所属栏目:Linux 来源:网络整理
导读:rsync+inotify实时同步环境部署记录 在使用rsync首次全量同步后,结合inotify对源目录进行实时监控,只有有文件变动或新文件产生,就会立刻同步到目标目录下,非常高效使用! 一)单向同步 需求:10.60.123.133同步/usr/local/tomcat-api/webapps/upload数据
rsync+inotify实时同步环境部署记录
在使用rsync首次全量同步后,结合inotify对源目录进行实时监控,只有有文件变动或新文件产生,就会立刻同步到目标目录下,非常高效使用!
一)单向同步
需求:10.60.123.133同步/usr/local/tomcat-api/webapps/upload数据到10.60.123.134对应目录
(1)10.60.123.133是源服务器,作为rsync的客户端,部署rsync+inotify。
(2)10.60.123.134是目标服务器,作为rsync的服务端。只需要安装配置rsync即可,不需要安装inotify。

第一部分:在目标服务器10.60.123.134上部署rsync服务端:
1)关闭selinux
[[email?protected] ~]# vim /etc/selinux/config
SELINUX=disabled
[[email?protected] ~]# setenforce 0

2)防火墙上允许以上三台源服务器访问它的22端口和873端口
[[email?protected] ~]# vim /etc/sysconfig/iptables
.......
.......
-A INPUT -s 192.168.1.106 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.57 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.139 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.106 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
-A INPUT -s 192.168.1.57 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
-A INPUT -s 192.168.1.139 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

[[email?protected] ~]# /etc/init.d/iptables restart

如若hosts.allow里面做了限制,同样也要开放以上三台源服务器的权限
[[email?protected] ~]# vim /etc/hosts.allow
#

hosts.allow This file contains access rules which are used to

allow or deny connections to network services that

either use the tcp_wrappers library or that have been

started through a tcp_wrappers-enabled xinetd.

#

See ‘man 5 hosts_options‘ and ‘man 5 hosts_access‘

for information on rule syntax.

See ‘man tcpd‘ for information on tcp_wrappers

#
sshd:192.168.1.106,192.168.1.57,192.168.1.139:allow
sshd:all:deny
[[email?protected] ~]# cat /etc/hosts.deny
#

hosts.deny This file contains access rules which are used to

deny connections to network services that either use

the tcp_wrappers library or that have been

started through a tcp_wrappers-enabled xinetd.

#

The rules in this file can also be set up in

/etc/hosts.allow with a ‘deny‘ option instead.

#

See ‘man 5 hosts_options‘ and ‘man 5 hosts_access‘

for information on rule syntax.

See ‘man tcpd‘ for information on tcp_wrappers

#

3)安装部署rsync服务
注意:CentOS中是以xinetd来管理Rsync服务的
[[email?protected] ~]# yum install rsync xinetd
[[email?protected] ~]# vim /etc/xinetd.d/rsync
.....
disable = no #由默认的yes改为no,设置开机启动rsync

启动xineted服务
[[email?protected] ~]# /etc/init.d/xinetd start

创建/etc/rsyncd.conf文件,并配置同步策略。添加如下代码:
[[email?protected] ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log
#日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid
#pid文件的存放位置
lock file = /var/run/rsync.lock
#支持max connections参数的锁文件
secrets file = /etc/rsync.passwd
#用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd
#rsync启动时欢迎信息页面文件位置(自己创建这个文件,内容随便自定义)

[pic_upload]
#自定义名称
path = /usr/local/tomcat-api/webapps
#rsync服务端数据目录路径,即同步到目标目录后的存放路径
comment = pic_upload
#模块名称与[pic_upload]自定义名称相同
uid = nobody
#设置rsync运行的uid权限。这个要保证同步到目标目录后的权限和源目录一致,即都是nobody!
gid = nobody
#设置rsync运行的gid权限。
port=873
#默认的rsync端口
use chroot = no
#默认为true,修改为no或false,增加对目录文件软连接的备份
read only = no
#设置rsync服务端文件为读写权限
list = no
#不显示rsync服务端资源列表
max connections = 200
#最大连接数
timeout = 600
#设置超时时间
auth users = RSYNC_USER
#执行数据同步的用户名,需要后面手动设置。可以设置多个,用英文状态下逗号隔开
hosts allow = 10.60.123.133
#允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#hosts deny = 192.168.1.194
#禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开(如果没有禁止,就不用设置这一行)

创建用户认证文件 (多个模块用的是这同一个认证文件)
[[email?protected] ~]# vim /etc/rsync.passwd
RSYNC_USER:[email?protected]

设置文件权限,即rsyncd.conf和rsync.pass认证文件都是600权限!
[[email?protected] ~]# chmod 600 /etc/rsyncd.conf
[[email?protected] ~]# chmod 600 /etc/rsync.passwd

重启rsync服务
[[email?protected] ~]# /etc/init.d/xinetd restart
[[email?protected] ~]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xinetd 22041 root 5u IPv6 3336440 0t0 TCP *:rsync (LISTEN)

第二部分:在源服务器10.60.123.133部署rsync客户端和inotify监控
关闭selinux,做为客户端的rsync可以不用在iptables里开放873端口
安装rsync,启动rsync服务。
创建同步的密码文件,这个文件名可以跟服务端的认证文件不一样,但是里面的密码必须一致!用于rsync同步命令中。不过,最好两边的文件设置成一样,便于管理
[[email?protected] ~]# vim /etc/rsync.passbr/>[email?protected]
设置rsync.pass密码文件为600权限
[[email?protected] ~]# chmod 600 /etc/rsync.pass
查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify
[[email?protected] ~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Oct 26 12:03 max_queued_events
-rw-r--r-- 1 root root 0 Oct 26 12:03 max_user_instances
-rw-r--r-- 1 root root 0 Oct 26 12:03 max_user_watches

注意:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
CentOS 5.X 内核为2.6.18,默认已经支持inotify
[[email?protected] ~]# uname -a
Linux static-img 2.6.32-573.22.1.el6.x86_64 #1 SMP Wed Mar 23 03:35:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

下面开始安装inotify-tools
[[email?protected] ~]# yum install make gcc gcc-c++ #安装编译工具
[[email?protected] ~]# cd /usr/local/src
[[email?protected] src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[[email?protected] src]# tar zxvf inotify-tools-3.14.tar.gz
[[email?protected] src]# cd inotify-tools-3.14
[[email?protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[[email?protected] inotify-tools-3.14]# make && make install

发现已经成功安装inotify-tools了
[[email?protected] inotify-tools-3.14]# ll -d /usr/local/inotify/
drwxr-xr-x 6 root root 4096 Oct 26 12:01 /usr/local/inotify/

设置系统环境变量
[[email?protected] ~]# vim /etc/profile
......
export PATH=$PATH:/usr/local/inotify/bin
[[email?protected] ~]# source /etc/profile

添加库文件
[[email?protected] ~]# vim /etc/ld.so.conf
/usr/local/inotify/lib
[[email?protected] ~]# ldconfig

修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值
[[email?protected] ~]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 16384
[[email?protected] ~]# sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 8192
[[email?protected] ~]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128

修改参数:
[[email?protected] ~]# sysctl -w fs.inotify.max_queued_events="99999999"
[[email?protected] ~]# sysctl -w fs.inotify.max_user_watches="99999999"
[[email?protected] ~]# sysctl -w fs.inotify.max_user_instances="65535"

参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现" Event Queue Overflow "错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /Data/xqsj_upload -type d | wc -l 统计这些源目录下的目录数,必须保证max_user_watches值大于统计结果(这里/Data/xqsj_upload为同步的源文件目录)
max_user_instances:
每个用户创建inotify实例最大值

4)接着执行同步操作:
分别在三台源服务器上执行rsync首次全量同步的操作(加--delete参数,保持目标目录和源目录下文件绝对一致)

在10.60.123.133服务器上
第一次全量同步:
rsync -avH --port=873 --progress --delete /usr/local/tomcat-api/webapps/upload [email?protected]::pic_upload --password-file=/etc/rsync.pass
待第一次rsync全量同步完成后,就进行rsync+inotify实时同步脚本操作。
实时同步脚本里添加的是--delete-before参数,而不是--delete参数(第一次全量同步时rsync用的参数),二者区别:
--delete参数:表示rsync同步前,暴力删除目标目录中的所有文件,然后再执行同步操作。
--delete-before参数:表示rsync同步前,会先对目标目录进行一次扫描检索,删除目标目录中对比源目录的多余文件,然后再执行同步操作。显然比--delete参数安全些。

vim /etc/rsync_pic_upload_inotify.sh
#!/bin/bash
SRCDIR=/usr/local/tomcat-api/webapps/upload
USER=RSYNC_USER
IP=10.60.123.134
DESTDIR=pic_upload
/usr/local/inotify/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f%e‘ -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file
do
/usr/bin/rsync -avH --port=873 --progress --delete-before $SRCDIR [email?protected]$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

然后启动同步脚本,放在后台执行!
[[email?protected]_web1 rsync]# nohup sh rsync_pic_upload_inotify.sh &
最后进行测试:
比如在源目录/usr/local/tomcat-api/webapps/upload中创建一个文件或目录,会自动实时同步到目标机器10.60.123.134的目标目录/usr/local/tomcat-api/webapps/upload中。

果在同步过程中,发现中途报错!重复执行同步命令一直是报这个错误:
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at
main.c(1505)

最后发现原因:
是因为在同步的时候,源目录下有软链接文件!
rsync同步软链接文件,应该加参数-l

所以,最好在使用rsync同步命令的时候,后面跟-avpgolr参数组合(将上面的-avH改成-avpgolr)
-a:递归
-v:打印详细过程
-p:保持文件属性
-g:文件所属组不变
-o:文件所属者不变
-l:软连接属性
-r:同步目录时的参数

二)双向同步需求:10.60.123.133同步/usr/local/tomcat-api/webapps/upload数据到10.60.123.134对应目录,反向也可。双向主要差异就是二台均要安装rsync+inotify 之间873端口互通,二台都要有/etc/rsyncd.conf ip配置对端 uid = root才行, /etc/rsync.passwd 配置用户加密码,/etc/rsync.pass配置密码文件,/etc/rsync_pic_upload_inotify.sh配置对端地址便可。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读