rsync+sersync

一、为什么要用rsync+sersync架构?
1、sersync是基于inotify开发的,类似于inotify-tools的工具
2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录
 
二、rsync+inotify-tools与rsync+sersync架构的区别?
1、rsync+inotify-tools
 a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
 b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低
 
2、rsync+sersync
 a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;
 b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高。
三、前期准备
1.系统资源列表
角色
服务器配置
操作系统版本
IP
机器名
sersync服务(M)
VM
CentOS7.5
192.168.0.114
sersync(推送端)
rsync服务(S1)
VM
CentOS7.5
192.168.0.55
WEB1(接收端)
rsync服务(S2)
VM
CentOS7.5
192.168.0.252
WEB2(接收端)
 
需求分析:在使用sersync+rsync进行实时同步时,用于推送端的服务器上跑sersync,接受文件端服务器上跑rsync守护进程。重点:当然sersync也是利用rsync命令将本服务器上的文件传输推送到接收端中,所以推送端也需要安装rsync服务。
四、部署服务
  接收端部署Rsync服务(s1\s2同时部署服务)
1、安装
 tar zxvf rsync-3.1.3.tar.gz
cd rsync-3.1.3
./configure --prefix=/usr/local/rsync/
make && make install

2、编写配置文件

######### 全局配置参数 ##########
port=888
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300 
motd file = /var/rsyncd/rsync.motd
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
write only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsyncd.passwd
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
###########下面指定模块,并设定模块配置参数,可以创建多个模块###########
[www]
path = /data/www/
###################################
[bbs]
path=/data/bbs/
###################################
[blog]
path=/data/blog/

3、创建同步密码并设置权限

echo "user:password" > /etc/rsync.password
chmod 600 /etc/rsync.password

4、启动

rsync --daemon
netstat -lnpt |grep 888

5、创建同步相关目录并授权rsync账户

mkdir -p /data/{www,bbs,blog}
chown -R rsync: /data
tree

7、设置开机启动项

vim /etc/rc.local

# rsync server
/usr/bin/rsync --daemon

    推送端部署Sersync服务(master)

1、安装rsync(同上)

2、设置rsync密码

echo "password" > /etc/rsync.password # 只放密码即可
chmod 600 /etc/rsync.password

3、创建同步相关目录并授权rsync账户

mkdir -p /data/{www,bbs,blog}
chown -R rsync: /data
tree

4、在master端手动同步服务查看是否可以通信

rsync -avz --port 888 /data/www/ rsync_backup@192.168.0.55::www --password-file=/etc/rsyncd.passwd
rsync -avz --port 888 /data/www/ rsync_backup@192.168.0.252::www --password-file=/etc/rsyncd.passwd

5、部署sersync服务

wget http://sersync.googlecode.com/files/sersync2.1_64bit.tar.gz  # 访问不了用下边的
wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar xzf sersync2.5.4_64bit_binary_stable_final.tar.gz
压缩包里面有两个文件confxml.xml sersync2
    sersync2 这个是sersync主程序(可执行文件,直接执行就可以)
    confxml.xml 这个是sersync配置文件
rm -rf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86/ /opt/sersync
ln -s /opt/sersync /usr/bin/sersync

6、配置sersync

cp /opt/sersync/confxml.xml ./confxml.xml_bak
vi confxml.xml

a)修改24-28行
  24         <localpathwatch= "/opt/tongbu" >      # 定义本地要同步的目录
  25             <remote ip= "127.0.0.1" name= "tongbu1" />
  26             <!--<remoteip= "192.168.8.39"  name= "tongbu" />-->         # 同步到哪台机器上 tongbu模块rsync端模块名字
  27             <!--<remoteip= "192.168.8.40"  name= "tongbu" />-->         # 同步到哪台机器上 tongbu模块
  28         < /localpath >

改为:
 24         <localpath watch="/data/www">
 25             <remote ip="192.168.0.55" name="www"/>
 26             <remote ip="192.168.0.252" name="www"/>
 27             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 28             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 29         </localpath>
提示:  此步 watch = "/data/blog" 就是定义服务端待同步的目录,和目标服务器的模块name= "blog"

b)修改31--34行,认证部分【rsync密码认证】
         < rsync >
             <commonParamsparams= "-artuz" />
             <auth start= "false" users = "root"  passwordfile= "/etc/rsync.pas" />
              <userDefinedPortstart= "false"  port= "874" /><!-- port=874 -->
              <timeoutstart= "false"  time = "100" /><!-- timeout=100 -->
              <sshstart= "false" />
          < /rsync >
改为:
 38         <rsync>
 39             <commonParams params="-artuz"/>
 40             <auth start="true" users="rsync_backup" passwordfile="/etc/rsyncd.passwd"/>
 41             <userDefinedPort start="true" port="888"/><!-- port=874 -->
 42             <timeout start="true" time="100"/><!-- timeout=100 -->
 43             <ssh start="false"/>
 44         </rsync>

c)修改37行
<failLogpath= "/tmp/rsync_fail_log.sh" timeToExecute= "60" /><!--default every 60mins execute once-->
# 当同步失败后,日志记录到/usr/local/sersync/logs/rsync_fail_log.sh文件中,并且每 60 分钟对失败的log进行重新同步
改为:
 45         <failLog path="/opt/sersync/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

修改后的完整配置文件为:
[root@master sersync]# cat confxml.xml 
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
	<delete start="true"/>
	<createFolder start="true"/>
	<createFile start="false"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="false"/>
	<modify start="false"/>
    </inotify>

    <sersync>
	<localpath watch="/data/www">
	    <remote ip="192.168.0.55" name="www"/>
	    <remote ip="192.168.0.252" name="www"/>
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
        <localpath watch="/data/bbs">
            <remote ip="192.168.0.55" name="bbs"/>
            <remote ip="192.168.0.252" name="bbs"/>
	</localpath>
        <localpath watch="/data/blog">
            <remote ip="192.168.0.55" name="blog"/>
            <remote ip="192.168.0.252" name="blog"/>
	</localpath>
	<rsync>
	    <commonParams params="-artuz"/>
	    <auth start="true" users="rsync_backup" passwordfile="/etc/rsyncd.passwd"/>
	    <userDefinedPort start="true" port="888"/><!-- port=874 -->
	    <timeout start="true" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	<failLog path="/opt/sersync/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	<crontab start="false" schedule="600"><!--600mins-->
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/>
    </sersync>

    <plugin name="command">
	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>

    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    <plugin name="refreshCDN">
	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
	    <sendurl base="http://pic.xoyo.com/cms"/>
	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
	</localpath>
    </plugin>
</head>

7、开启sersync守护进程同步数据

sersync2 -n 10 -d -o /usr/local/sersync/confxml.xml
-n 启用线程数量
-d daemon方式启动
-o 指定配置文件

五、服务验证

1、sersync启动正常返回结果

set  the system param
execute:echo50000000 >  /proc/sys/fs/inotify/max_user_watches
execute: echo  327679>  /proc/sys/fs/inotify/max_queued_events
parse the  command  param
option: -d      run as a daemon
option: -r       rsync  all the  local  files to the remoteservers 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 runbehind the console 
use  rsync  password- file  :
user is rsync_backup
passwordfile is          /etc/rsync .password
config xml parse success
please  set  /etc/rsyncd .confmax connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) +20(Sub threads)
please according your cpu ,use -n paramto adjust the cpu rate
chmod : cannot access` /usr/local/sersync/logs/rsync_fail_log .sh': No such  file  or directory
------------------------------------------
rsync  the directory recursivlyto the remote servers once
working please wait...
execute  command :  cd  /data/www &&  rsync  -artuz -R --delete ./ --timeout=100 rsync_backup@172.16.1.25::www--password- file = /etc/rsync .password > /dev/null  2>&1 
run the sersync: 
watch  path is:  /data/www

2、压力测试

写入10K个文件批量同步测试结果:
for n in `seq 10000`;do echodddd>www/$n.txt;done
ps -ef |greprsync

六、参数讲解

1、sersync参数说明

Sersync参数
说明
./sersync -r
-r参数作用是:开启实时监控的之前对主服务器目录与远程目标机器的目录进行一次整体同步;如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则要以 -r参数运行sersync,将本地与远程整体同步一次;
提别说明:如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步;
./sersync -o xx.xml
不指定 -o参数: sersync使用sersync可执行文件目录下的默认配置文件confxml.xml
指定 -o 参数:可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步
./sersync -n num
-n参数为:指定默认的线程池的线程总数;
例如: ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过该参数调低,如果机器配置较高,可以调高默认的线程总数,提升同步效率;
./sersync -d
-d参数为:后台服务,通常情况下使用 -r参数对本地到远端整体同步一遍后,在后台运行此参数启动守护进程实时同步;在第一次整体同步时,-d 和 -r参数经常会联合使用;
./sersync -m
pluginName
-m参数:不进行同步,只运行插件 ./sersync -m pluginName
例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件
组合命令使用说明:
-n 8 -o liubl.xml -r -d
多个参数可以配合使用,例如:./sersync -n 16 -o config.xml -r -d 表示设置线程池工作线程为16个,指定liubl.xml作为配置文件,在实时监控前 做一次整体同步,以守护进程方式在后台运行;
./sersync --help
很遗憾,它没有查看帮助(需要的话2条路,要么看源代码,要么自测求验证)

 

2、初始化配置文件说明

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host> #保留字段,默认即可
<debug start="false"/> #是否开启调试模式,默认false即可
<fileSystem xfs="true"/> #是否开启支持xfs文件系统,Centos7默认都是xfs的了,所以建议true开启
<filter start="false"> #是否开启过滤模式,根据需求开启,例:过滤以.php结尾的文件(.*)\.php
<exclude expression="(.*)\.svn"></exclude> #过滤以.svn结尾的文件
<exclude expression="(.*)\.gz"></exclude> #过滤以.gz结尾的文件
<exclude expression="^info/*"></exclude> #过滤监控目录下的info路径
<exclude expression="^static/*"></exclude> #过滤监控目录下的static路径
<exclude expression="wwwroot/blogwhsir/*"></exclude> #过滤wwwroot/blogwhsir/目录下所有文件
</filter>
<inotify> #inotify监控事件
<delete start="true"/> #如果不开启此项,在删除监控目录下的文件时,目标服务器的文件则不会同时删除,根据需求开启
<createFolder start="true"/> #不开启不能监控子目录,建议true
<createFile start="false"/> #关闭提高通讯性能,默认就好
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
 
<sersync>
<localpath watch="/opt/tongbu"> #指定要监控的本地目录
<remote ip="127.0.0.1" name="tongbu1"/> #指定要同步的目标服务器的IP地址,及目标服务器rsync的[模块]
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync> #配置rsync
<commonParams params="-artuz"/> #rsync的参数
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/> #是否开启rsync的认证模式,需要配置users及passwordfile,根据情况开启(如果开启,注意密码文件权限一定要是600)
<userDefinedPort start="false" port="874"/><!-- port=874 --> #远程目标服务器的端口不是默认端口时使用
<timeout start="false" time="100"/><!-- timeout=100 --> #是否开启rsync的超时时间
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #目标服务器传输失败时会重新传输,再次失败会写入rsync_fail_log中,每隔一段时间(timeToExecute)执行脚本再次传输
<crontab start="false" schedule="600"><!--600mins--> #对监控目录与目标服务器每隔一段时间进行一次整体同步,默认600分钟,根据个人情况是否开启
<crontabfilter start="false"> #如果之前开启了文件过滤,这里也要设置过滤
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
 
<plugin name="command"> #下面就是插件的设置(不做过多说明)
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
 
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="https://blog.whsir.com"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>

  

 
 
 
 
posted @ 2022-03-10 15:07  总要做些什么  阅读(243)  评论(0编辑  收藏  举报