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

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

随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足。
#由默认的yes改为no,设置开机启动rsync

启动xineted服务
[root@bastion-IDC ~]# /etc/init.d/xinetd start

创建/etc/rsyncd.conf文件,并配置同步策略。添加如下代码:
[root@bastion-IDC ~]# 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.pass         #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd          #rsync启动时欢迎信息页面文件位置(自己创建这个文件,内容随便自定义)

[fangfull_upload]                            #自定义名称
path = /home/backup/image-back/fangfull_upload           #rsync服务端数据目录路径,即同步到目标目录后的存放路径
comment = fangfull_upload                                             #模块名称与[fangfull_upload]自定义名称相同
uid = 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 = 192.168.1.106        #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.194       #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开#由默认的yes改为no,设置开机启动rsync

启动rsync服务
[root@static-img ~]# /etc/init.d/xinetd start
[root@static-img ~]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xinetd 3268 root 5u IPv4 4139812 0t0 TCP *:rsync (LISTEN)

创建同步的密码文件,这个文件名可以跟服务端的认证文件不一样,但是里面的密码必须一致!用于rsync同步命令中。不过,最好两边的文件设置成一样,便于管理
[root@static-img ~]# vim /etc/rsync.pass
123456@rsync

设置rsync.pass密码文件为600权限
[root@static-img ~]# chmod 600 /etc/rsync.pass

#安装编译工具
[root@static-img ~]# cd /use/local/src
[root@static-img src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@static-img src]# tar zxvf inotify-tools-3.14.tar.gz
[root@static-img src]# cd inotify-tools-3.14
[root@static-img inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@static-img inotify-tools-3.14]# make && make install

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

设置系统环境变量
[root@static-img ~]# vim /etc/profile
......
export PATH=$PATH:/usr/local/inotify/bin
[root@static-img ~]# source /etc/profile

添加库文件
[root@static-img ~]# vim /etc/ld.so.conf
/usr/local/inotify/lib
[root@static-img ~]# ldconfig

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

修改参数:
[root@static-img ~]# sysctl -w fs.inotify.max_queued_events="99999999"
[root@static-img ~]# sysctl -w fs.inotify.max_user_watches="99999999"
[root@static-img ~]# sysctl -w fs.inotify.max_user_instances="65535"

max_user_instances:
每个用户创建inotify实例最大值


4)接着执行同步操作:
分别在三台源服务器上执行rsync首次全量同步的操作(加--delete参数,保持目标目录和源目录下文件绝对一致)
---------------------------------------------
在192.168.1.106服务器上
第一次全量同步:
[root@fangfull_web ~]# rsync -avH --port=873 --progress --delete /Data/fangfull_upload/ RSYNC_USER@192.168.1.5::fangfull_upload --password-file=/etc/rsync.pass
[root@fangfull_web ~]# rsync -avH --port=873 --progress --delete /Data/erp_upload/ RSYNC_USER@192.168.1.5::erp_upload --password-file=/etc/rsync.pass

待第一次rsync全量同步完成后,就进行rsync+inotify实时同步脚本操作。
实时同步脚本里添加的是--delete-before参数,而不是--delete参数(第一次全量同步时rsync用的参数),二者区别:
--delete参数:表示rsync同步前,暴力删除目标目录中的所有文件,然后再执行同步操作。
--delete-before参数:表示rsync同步前,会先对目标目录进行一次扫描检索,删除目标目录中对比源目录的多余文件,然后再执行同步操作。显然比--delete参数安全些。
[root@fangfull_web1 ~]# cd /home/rsync/
[root@fangfull_web1 rsync]# cat rsync_fangfull_upload_inotify.sh
#!/bin/bash
SRCDIR=/Data/fangfull_upload/
USER=RSYNC_USER
IP=192.168.1.5
DESTDIR=fangfull_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 $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done
[root@fangfull_web1 rsync]# cat rsync_erp_upload_inotify.sh
#!/bin/bash
SRCDIR=/Data/erp_upload/
USER=RSYNC_USER
IP=192.168.1.5
DESTDIR=erp_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 $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

然后启动同步脚本,放在后台执行!
[root@fangfull_web1 rsync]# nohup sh rsync_fangfull_upload_inotify.sh &
[root@fangfull_web1 rsync]# nohup sh rsync_erp_upload_inotify.sh &

检查下是否脚本启动成功了
[root@fangfull_web1 rsync]# ps -ef|grep inotify
root 11390 1 0 13:41 ? 00:00:00 sh rsync_erp_upload_inotify.sh
root 11392 11390 0 13:41 ? 00:00:00 sh rsync_erp_upload_inotify.sh
root 11397 1 0 13:41 ? 00:00:00 sh rsync_fangfull_upload_inotify.sh
root 11399 11397 0 13:41 ? 00:00:00 sh rsync_fangfull_upload_inotify.sh
root 21842 11702 0 17:22 pts/0 00:00:00 grep --color=auto inotify

最后进行测试:
比如在源目录/Data/fangfull_upload中创建一个文件或目录,会自动实时同步到目标机器192.168.1.5的目标目录/home/backup/image-back/fangfull_upload中。


---------------------------------------------
在192.168.1.57服务器上
第一次全量同步:
[root@xqsj_web ~]# rsync -avH --port=873 --progress --delete /Data/xqsj_upload/ RSYNC_USER@192.168.1.5::xqsj_upload --password-file=/etc/rsync.pass
[root@xqsj_web ~]# rsync -avH --port=873 --progress --delete /Data/fanghu_upload_src/ RSYNC_USER@192.168.1.5::fanghu_upload_src --password-file=/etc/rsync.pass

rsync+inotify实时同步:
[root@xqsj_web1 ~]# cd /home/rsync/
[root@xqsj_web1 rsync]# cat rsync_xqsj_upload_inotify.sh
#!/bin/bash
SRCDIR=/Data/xqsj_upload/
USER=RSYNC_USER
IP=192.168.1.5
DESTDIR=xqsj_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 $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done
[root@xqsj_web1 rsync]# cat rsync_fanghu_upload_src_inotify.sh
#!/bin/bash
SRCDIR=/Data/fanghu_upload_src/
USER=RSYNC_USER
IP=192.168.1.5
DESTDIR=fanghu_upload_src
/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 $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

然后启动同步脚本,放在后台执行!
[root@fangfull_web1 rsync]# nohup sh rsync_xqsj_upload_inotify.sh &
[root@fangfull_web1 rsync]# nohup rsync_fanghu_upload_src_inotify.sh &

最后进行测试:
比如在源目录/Data/xqsj_upload中创建一个文件或目录,会自动实时同步到目标机器192.168.1.5的目标目录/home/backup/image-back/xqsj_upload中。

--------------------------------------------
在192.168.1.139服务器上
第一次全量同步:
[root@static-img ~]# rsync -avH --port=873 --progress --delete /Data/Static_img/webroot/ssapp-prod/ RSYNC_USER@192.168.1.5::ssapp-prod --password-file=/etc/rsync.pass
[root@static-img ~]# rsync -avH --port=873 --progress --delete /usr/local/nginx/html/ssapp.prod/ RSYNC_USER@192.168.1.5::ssapp.prod --password-file=/etc/rsync.pass

rsync+inotify实时同步:
[root@static-img ~]# cd /home/rsync/
[root@static-img rsync]# cat rsync_ssapp-prod_inotify.sh
#!/bin/bash
SRCDIR=/Data/Static_img/webroot/ssapp-prod/
USER=RSYNC_USER
IP=192.168.1.5
DESTDIR=ssapp-prod
/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 $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

[root@static-img rsync]# cat rsync_ssapp.prod_inotify.sh
#!/bin/bash
SRCDIR=/usr/local/nginx/html/ssapp.prod/
USER=RSYNC_USER
IP=192.168.1.5
DESTDIR=ssapp.prod
/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 $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

然后启动同步脚本,放在后台执行!
[root@fangfull_web1 rsync]# nohup sh rsync_ssapp-prod_inotify.sh &
[root@fangfull_web1 rsync]# nohup rsync_ssapp.prod_inotify.sh &

最后进行测试:
比如在源目录/Data/Static_img/webroot/ssapp-prod中创建一个文件或目录,会自动实时同步到目标机器192.168.1.5的目标目录/home/backup/image-back/ssapp-prod中。

如果在同步过程中,发现中途报错!重复执行同步命令一直是报这个错误:
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:同步目录时的参数

原文地址:https://www.cnblogs.com/fyy-hhzzj/p/7845449.html