sersync+rsync进行数据同步
一:环境
操作系统环境:redhat6.6
内核版本:2.6.32-358.el6.x86_64
rsync server:192.168.2.3(部署rsync server)
rsync client:192.168.2.4(部署sersync)
特别说明:
我之前用rsync进行生产环境数据的同步,由于目录下每日会有上千个文件生成,所以目录本身就比较大,用rsync的进行无差异同步的时候发现就扫描目录的文件就需要很长时间,同步效率有些低,后面用inotify+rsync进行数据同步,有个问题是:inotify监控目录文件变化的时候,在我们要对生产系统进行开启或关闭的时候,由于目录占用问题导致开启或关闭生产组件会出现进程没关闭的显示,但是实际上是关闭了的,由于检查进程的时候发现目录被占用了;所以今天用sersync进行数据同步的时候发现速度快了好多,所以留个笔记以后方便查看。
二:sersync部署
1、下载sersync安装包
链接:https://pan.baidu.com/s/1kHQSVEODxbtXpxxMnT1S2A
提取码:9q4m
2、解压安装包
[root@rsync_client tools]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@rsync_client tools]# mv GNU-Linux-x86 /usr/local/sersync
3、备份原配置文件
[root@rsync_client ~]# cd /usr/local/sersync/ [root@rsync_client sersync]# cp confxml.xml confxml.xml.ori
4、部署rsync server详细参数解释请看https://www.cnblogs.com/zhangweiyi/p/10571152.html
1 [root@rsync_server ~]# cat /etc/rsyncd.conf 2 uid = rsync 3 gid = rsync 4 use chroot = no 5 max connections = 200 6 timeout = 300 7 pid file = /var/run/rsyncd.pid 8 lock file = /var/run/rsync.lock 9 log file = /var/log/rsyncd.log 10 ignore errors 11 read only = false 12 list = false 13 hosts allow = 192.168.2.0/24 14 hosts deny = 0.0.0.0/32 15 auth users = sam 16 secrets file = /etc/rsync.password 17 [test] 18 path = /tmp/
5、配置rsync密码文件
[root@rsync_server ~]# cat /etc/rsync.password sam:test123 [root@rsync_server ~]# [root@rsync_server ~]# ll /etc/rsync* # 查看文件权限是否为600 -rw------- 1 root root 364 Nov 30 08:29 /etc/rsyncd.conf-rw------- 1 root root 13 Nov 21 09:59 /etc/rsync.password
6、启动rsync
[root@rsync_server ~]# rsync --daemon [root@rsync_server ~]# lsof -i :873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 1206 root 3u IPv4 11197 0t0 TCP *:rsync (LISTEN) rsync 1206 root 5u IPv6 11198 0t0 TCP *:rsync (LISTEN)
三:部署sersync
1、首先创建rsync server端的密码验证文件
[root@rsync_client ~]# cat /etc/rsync.password test123 [root@rsync_client ~]# ll /etc/rsync.password # 确保文件权限为600 -rw-------. 1 root root 7 11月 26 19:53 /etc/rsync.password
2、部署sersync
[root@rsync_client tools]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz [root@rsync_client tools]# mv GNU-Linux-x86 /usr/local/sersync [root@rsync_client sersync]# cp confxml.xml confxml.xml.ori [root@rsync_client sersync]#
3、修改config.xml文件只修改我注释了的即可
1 [root@rsync_client sersync]# cat confxml.xml 2 <?xml version="1.0" encoding="ISO-8859-1"?> 3 <head version="2.5"> 4 <host hostip="localhost" port="8008"></host> 5 <debug start="false"/> 6 <fileSystem xfs="false"/> 7 <filter start="false"> 8 <exclude expression="(.*)\.svn"></exclude> 9 <exclude expression="(.*)\.gz"></exclude> 10 <exclude expression="^info/*"></exclude> 11 <exclude expression="^static/*"></exclude> 12 </filter> 13 <inotify> 14 <delete start="true"/> 15 <createFolder start="true"/> 16 <createFile start="true"/> # 修改此项为true 17 <closeWrite start="true"/> 18 <moveFrom start="true"/> 19 <moveTo start="true"/> 20 <attrib start="false"/> 21 <modify start="false"/> 22 </inotify> 23 24 <sersync> 25 <localpath watch="/tmp/"> # 监控的目录 26 <remote ip="192.168.2.4" name="test"/> # 需要把监控的目录数据同步到rsync server指定的模块下,前面写了server的模块名是test对应的目录是:/tmp/ 27 <!--<remote ip="192.168.8.39" name="tongbu"/>--> 28 <!--<remote ip="192.168.8.40" name="tongbu"/>--> 29 </localpath> 30 <rsync> 31 <commonParams params="-artuz"/> 32 <auth start="true" users="sam" passwordfile="/etc/rsync.password"/> # 指定rsync的虚拟用户和密码文件路径 33 <userDefinedPort start="false" port="874"/><!-- port=874 --> 34 <timeout start="false" time="100"/><!-- timeout=100 --> 35 <ssh start="false"/> 36 </rsync> 37 <failLog path="/var/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> # 把日志文件的路径改到/var/log/下 38 <crontab start="false" schedule="600"><!--600mins--> 39 <crontabfilter start="false"> 40 <exclude expression="*.php"></exclude> 41 <exclude expression="info/*"></exclude> 42 </crontabfilter> 43 </crontab> 44 <plugin start="false" name="command"/> 45 </sersync> 46 47 <plugin name="command"> 48 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> 49 <filter start="false"> 50 <include expression="(.*)\.php"/> 51 <include expression="(.*)\.sh"/> 52 </filter> 53 </plugin> 54 55 <plugin name="socket"> 56 <localpath watch="/opt/tongbu"> 57 <deshost ip="192.168.138.20" port="8009"/> 58 </localpath> 59 </plugin> 60 <plugin name="refreshCDN"> 61 <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 62 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 63 <sendurl base="http://pic.xoyo.com/cms"/> 64 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 65 </localpath> 66 </plugin> 67 </head>
4、启动sersync
[root@rsync_client ~]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
[root@rsync_client ~]# ps -ef | grep -v "grep" | grep sersync
root 24590 1 0 19:46 ? 00:00:00 /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
5、测试是否正常
在/tmp目录下创建几个文件看看目标端192.168.2.3(rsync server)是否已经收到
在/tmp目录下删除几个文件看看目标端192.168.2.3(rsync server)是否也不存在
以上两者满足说明部署成功!