参考文章:
https://blog.51cto.com/liubao0312/1677586
https://github.com/wsgzao/sersync
一、介绍
1.1、Sersync介绍
(1)sersync是基于inotify开发的,类似于inotify-tools的工具
(2)sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录
1.2、rsync+inotify-tools与rsync+sersync架构的区别
(1)rsync+inotify-tools
a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体的文件或者目录发生了变化记录下来;
b、rsync在同步的时候,因为并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低。
(2)rsync+sersync
a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;
b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高。
总结:
当同步的目录数据量不大时,建议使用rsync+inotify
当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync
二、原理说明
原理步骤:
-
在主服务器(Master)上开启sersync服务,sersync负责监控配置路径中的文件实时变化(用户就是在这个服务器里创建、修改、删除文件的);
-
在Master调用rsync命令把更新的文件推送(Push)到目标服务器(Slave1 和 Slave2);
-
需要在主服务器配置sersync,在目标服务器配置rsync
原理如图:
三、配置
环境说明:
系统:Ubuntu 18.04
Master:192.168.43.166
Slave:192.168.43.97
3.1、配置目标服务器Slave的Rsync
3.1.1、开机启动rsync服务
Ubuntu 18.04 是默认安装了rsync的,但rsync服务默认不是启动的,我们要修改文件sudo vi /etc/default/rsync
。
RSYNC_ENABLE=true #将false改true
3.1.2、修改配置文件
先复制一下配置文件到etc目录下,方便修改
sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
修改配置文件
sudo vi /etc/rsyncd.conf
修改成如下信息:
注意:这个path同步目录如果没有的话,需要自己创建
3.1.3、创建密码文件
创建密码文件:sudo vi /etc/rsyncd.secrets
。内容如下:
hadoop:123456 #格式必须要为 用户名:密码
说明:这里的用户名与密码是Master的用户名与密码,别搞错了
给密码文件赋予0600权限:sudo chmod 0600 /etc/rsyncd.secrets
3.1.4、启动resync
sudo /etc/init.d/rsync start
至此,Slave的Rsync配置完成
3.1.5、测试
我们到Master上测试Slave的Rsync:
在Master上创建密码文件:sudo vi /etc/rsyncd.secrets
。内容如下:
123456 #写入客户端密码即可
给密码文件赋予0600权限:sudo chmod 0600 /etc/rsyncd.secrets
cd ~ #返回到桌面
sudo vi hello.txt #创建一个hello.txt,里面随便写一些内容
rsync -avzP hello.txt [email protected]::data --password-file=/etc/rsyncd.secrets
解释:将本机的hello.txt文件上传到Slave的data模块指定的path中,这里用了密码文件,能够进行免密上传。类似于git的push
特别注意:这里一定要成功,不然下面配置了也无法成功
3.2、配置主服务器Master的Sersync
3.2.1、下载Sersync
下载地址:https://github.com/wsgzao/sersync
解压:
tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
cd /usr/local/
mv GNU-Linux-x86 sersync
3.2.2、配置Sersync
cd /usr/local/sersync
cp confxml.xml confxml.xml-bak
sudo vi confxml.xml
修改为如下内容:
3.2.3、开启sersync守护进程同步数据
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
返回如下结果,则成功:
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
sh: 1: cannot create /proc/sys/fs/inotify/max_user_watches: Permission denied
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
sh: 1: cannot create /proc/sys/fs/inotify/max_queued_events: Permission denied
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is hadoop
passwordfile is /etc/rsyncd.secrets
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /home/hadoop/hub && rsync -artuz -R --delete ./ --timeout=100 [email protected]::data --password-file=/etc/rsyncd.secrets >/dev/null 2>&1
run the sersync:
watch path is: /home/hadoop/hub
至此,sersync+rsync已经配置完成,只要在同步目录里面创建、删除、修改文件,就能实时同步到Slave里面了
你也可以将/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
写入到rc.local中,让Master开机就启动文件同步
附录:
1、参数说明
参数 | 说明 |
---|---|
./sersync -r | 开启实时监控的之前对主服务器目录与远程目标机器的目录进行一次整体同步。 |
./sersync -o xx.xml | 不指定 -o参数: sersync使用sersync可执行文件目录下的默认配置文件confxml.xml 指定 -o 参数:可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步 |
./sersync -n num | 指定默认的线程池的线程总数。 例如: ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过该参数调低,如果机器配置较高,可以调高默认的线程总数,提升同步效率; |
./sersync -d | 后台服务,通常情况下使用 -r参数对本地到远端整体同步一遍后,在后台运行此参数启动守护进程实时同步。在第一次整体同步时,-d 和 -r参数经常会联合使用; |
./sersync -m | 不进行同步,只运行插件。 例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件 |
2、配置文件说明
默认的配置文件如下:
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <head version="2.5">
3 <host hostip="localhost" port="8008"></host>
4 <debug start="false"/>
5 <fileSystem xfs="false"/>
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
22
23 <sersync>
24 <localpath watch="/opt/tongbu">
25 <remoteip="127.0.0.1" name="tongbu1"/>
26 <!--<remoteip="192.168.8.39" name="tongbu"/>-->
27 <!--<remoteip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
29 <rsync>
30 <commonParamsparams="-artuz"/>
31 <auth start="false"users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPortstart="false" port="874"/><!-- port=874 -->
33 <timeoutstart="false" time="100"/><!-- timeout=100 -->
34 <sshstart="false"/>
35 </rsync>
36 <failLog path="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->
37 <crontab start="false"schedule="600"><!--600mins-->
38 <crontabfilterstart="false">
39 <excludeexpression="*.php"></exclude>
40 <excludeexpression="info/*"></exclude>
41 </crontabfilter>
42 </crontab>
43 <plugin start="false" name="command"/>
44 </sersync>
45
46 <plugin name="command">
47 <param prefix="/bin/sh" suffix=""ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
48 <filter start="false">
49 <includeexpression="(.*)\.php"/>
50 <includeexpression="(.*)\.sh"/>
51 </filter>
52 </plugin>
53
54 <plugin name="socket">
55 <localpath watch="/opt/tongbu">
56 <deshostip="192.168.138.20" port="8009"/>
57 </localpath>
58 </plugin>
59 <plugin name="refreshCDN">
60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
61 <cdninfodomainname="ccms.chinacache.com" port="80"username="xxxx" passwd="xxxx"/>
62 <sendurlbase="http://pic.xoyo.com/cms"/>
63 <regexurlregex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/p_w_picpaths"/>
64 </localpath>
65 </plugin>
66 </head>
2.1、Debug开启
4 <debug start="false"/>
设置为true,表示开启debug模式,会在sersync正在运行的控制台打印inotify时间与rsync同步命令;
2.2、XFS文件系统开关
5 <fileSystem xfs="false"/>
对于xfs文件系统的用户,需要将这个选项开启,才能使用sersync正常工作;
2.3、filter文件过滤功能
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为false来提高性能,减少rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的时间close_write,同样可以实现文件完整同步;
注意:强将creatFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控;所以除非特殊需要,请必须开启; 默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete参数设置为false,则不对删除事件进行监控;