Rsync介绍与详解http://fdgui.blog.51cto.com/3484207/1408780

什么是sersync?

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是: 
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。

单纯使用rsync和使用serync+rsync的区别:

使用rsync+crontab做定时同步时,主服务器端开启rsync守护进程,而镜像服务器是运行rsync客户端,平时一般会利用crontab定时获取rsync服务器上的数据。 
但使用rsync+sersync做实时同步时,用于推送文件的服务器运行sersync服务,用于接收文件的服务器则运行rsync守护进程,简单来说就是sersync会利用rsync命令将文件推送到rsync服务器,实际线上使用一般会把sersync作为主服务器,rsync作为镜像服务器,实现数据同步备份,web镜像等功能。

前提条件:
在使用sersync之前,我们必须安装配置好rsync服务器。这里我们需要注意的是,纯粹的使用rsync做单向同步时,rsync的守护进程是运行在文件推送的服务器上,而接收的服务器是运行rsync客户端。使用sersync做文件实时同步刚好相反,用于接收文件的服务器运行rsync守护进程。

一、安装sersync:

下载地址:https://code.google.com/p/sersync/downloads/list


[root@master src]#ls 
  
sersync2.5.4_64bit_binary_stable_final.tar.gz 
  
 
  [root@master src]#tar xzf sersync2.5.4_64bit_binary_stable_final.tar.gz  
   
  
 
  [root@master src]#ls 
  
GNU-Linux-x86 sersync2.5.4_64bit_binary_stable_final.tar.gz 
  
[root@master src]#cd GNU-Linux-x86/ 
  
 
  [root@master GNU-Linux-x86]#ll 
  
total 1776 
  
-rwxr-xr-x 1 root root    2214 Oct 26  2011 confxml.xml 
  
-rwxr-xr-x 1 root root 1810128 Oct 26  2011 sersync2


为了方面使用,我们把它COPY到:

[root@master GNU-Linux-x86]#mkdir /usr/local/sersync
[root@master GNU-Linux-x86]#mv * /usr/local/sersync/


二、基本配置:

2.1 创建密码文件:

[root@master GNU-Linux-x86]#cd /usr/local/sersync/
[root@master sersync]#echo "password123" > rsync.passwd
[root@master sersync]#chmod 600 rsync.passwd

2.2 编辑主配置文件:

以下红色部分为我修改的

[root@master sersync]#vi 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="true"> 
        <exclude expression="(.*)\.html"></exclude> 
        <exclude expression="(.*)\.php"></exclude>        <exclude expression="(.*)\.jpg"></exclude>
        <exclude expression="(.*)\.gif"></exclude>
        <exclude expression="(.*)\.png"></exclude>
        <exclude expression="(.*)\.css"></exclude>
        <exclude expression="(.*)\.js"></exclude> 
        <exclude expression="^tmp/*"></exclude> 
    </filter><!--设置要监控的事件 --> 
    <inotify> 
        <delete start="true"/> 
        <createFolder start="true"/> 
<createFile start="true"/>
        <closeWrite start="true"/> 
        <moveFrom start="true"/> 
        <moveTo start="true"/> 
        <attrib start="false"/> 
<modify start="true"/>
    </inotify>    <sersync>
  <!--设定监控目录--> 
<localpath watch="/var/www/html">  <!--指定远端rsync的地址和模块名-->
<remote ip="192.168.204.129" name="webhome"/> 
            <!--<remote ip="192.168.204.128" name="tongbu"/>--> 
            <!--<remote ip="192.168.8.40" name="tongbu"/>--> 
        </localpath>    <rsync> 
        <commonParams params="-artuz"/>      <!--是否启用验证,并指定密码存放文件 --> 
<auth start="true" users="apache" passwordfile="/usr/local/sersync/rsync.passwd" /> 
        <userDefinedPort start="false" port="874"/><!-- port=874 --> 
        <timeout start="false" time="100"/><!-- timeout=100 --> 
        <ssh start="false"/> 
    </rsync> 
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once—>  <!--是否启用执行完整rsync,并指定执行周期 -->     
<crontab start="true" schedule="1200"><!--600mins--> 
        <crontabfilter start="true">         <exclude expression="(.*)\.html"></exclude> 
        <exclude expression="(.*)\.php"></exclude>        <exclude expression="(.*)\.jpg"></exclude>
        <exclude expression="(.*)\.gif"></exclude>
        <exclude expression="(.*)\.png"></exclude>
        <exclude expression="(.*)\.css"></exclude>
        <exclude expression="(.*)\.js"></exclude> 
        <exclude expression="^tmp/*"></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/p_w_picpaths"/> 
        </localpath> 
    </plugin> 
</head>


三、启用同步功能:

再次强调,所有sersync客户端都需要安装rsync,并启动为服务。在上述配置文件中须将所有需要同步的客户端IP加入。

使用如下命令进行初始化同步,并监视指定目录变化情况:

[root@master sersync]#./sersync2 -r
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r rsync all the local files to the remote servers before the sersync work
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
will ignore the inotify createFile event 
Start the crontab Every 600 minutes rsync all the files to the remote servers entirely
开启crontab过滤功能: 作整体同步的时候会对如下文件进行过滤
*.php
info/*
use rsync password-file :
user is client
passwordfile is /usr/local/sersync/rsync.passwd
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 /var/www/html && rsync -artuz -R --delete ./ client@172.20.45.48::webhome --password-file=/usr/local/sersync/rsync.passwd --exclude="*.php" --exclude="info/*" >/dev/null 2>&1 
run the sersync: 
watch path is: /var/www/html


Tip: 如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。

如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效。


相关参数:


参数-d:启用守护进程模式


参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍


c参数-n: 指定开启守护线程的数量,默认为10个


参数-o:指定配置文件,默认使用confxml.xml文件


参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块


参数-m:单独启用其他模块,使用 -m socket 开启socket模块


参数-m:单独启用其他模块,使用 -m http 开启http模块


不加-m参数,则默认执行同步程序



可选功能与xml高级配置





<host hostip="localhost" port="8008"></host>


hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可。


filter文件过滤功能


对于sersync监控的文件,会默认过滤系统的临时文件(以“.”开头,以“~”结尾),除了这些文件外,可以自定义其他需要过滤的文件。


<filter start="true">
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
</filter>


将start设置为 true,在exclude标签中,填写正则表达式,默认给出两个例子分别是过滤以”.gz”结尾的文件与过滤监控目录下的info路径(监控路径/info /*),

可以根据需要添加,但开启的时候,自己测试一下,正则表达式如果出现错误,控制台会有提示。相比较使用rsync 的exclude功能,被过滤的路径,不会加入监控,大大减少rsync的通讯量。



inotify监控参数设定

(优化)


对于inotify监控参数可以进行设置,根据您项目的特点优化srsync。


<inotify>
<delete start="true"/>
<createFolder  start="true"/>
<createFile start="true"/>
</inotify>


对于大多数应用,可以尝试把 createFile (监控文件事件选项)设置为false来提高性能,减少 rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事 件close_write,同样可以实现文件完整同步。

注意:强将createFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控。所以除非特殊需要,请开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete 参数设置为false,则不对删除事件进行监控。



Debug开启



<debug start="false"/>



设置为true,开启debug模式,会在sersync正在运行的控制台,打印inotify事件与rsync同步命令。

XFS文件系统



<fileSystem xfs="false"/>



对于xfs文件系统的用户,需要将这个选项开启,才能使sersync正常工作.


 

文件监控与远程同步设置



<localpath watch="/opt/tongbu">
<remote ip="192.168.0.104" name="tongbu1"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>




Rsync参数配置



<rsync>
<commonParams params="-artuz"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>



commonParams可以用户自定义rsync参数,默认是-artuz
auth start=”false” 设置为true的时候,使用rsync的认证模式传送,需要配置user与passwrodfile(–password-file=/etc/rsync.pas),来使用。
userDefinedPort  当远程同步目标服务器的rsync端口不是默认端口的时候使用(–port=874)。timeout设置rsync的timeout时间(–timeout=100)。ssh 使用rsync -e ssh的方式进行传输。
失败日志脚步配置



<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>



对于失败的传输,会进行重新传送,再次失败就会写入rsync_fail_log,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径。

Crontab定期整体同步功能



<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.gz"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>


crontab可以对监控路径与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了crontab功 能,还可以进一步保证各个服务器文件一致,
如果文件量比较大,crontab的时间间隔要设的大一些,否则可能增加通讯开销。schedule这个参数是设置crontab的时间间隔,默认是600分钟
如果开启了filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候 如果不单独设置crontabfilter,还会将需过滤的文件同步到远程,
crontab的过滤正则与filter过滤的不同,也给出了两个实例分别对 应与过滤文件与目录。总之如果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与filter的过滤一一对应。

插件设置



<plugin start="false" name="command"/>



当设置为true的时候,将文件同步到远程服务器后会调用name参数指定的插件。

command插件

当文件同步完成后,会调用command插件,如同步文件是test.php,则test.php文件在改动之后,调用rsync同步到远程服务器后,调用command插件,执行



/bin/sh test.php  suffix >/dev/null 2>&1



如果suffix 设置了,则会放在inotify事件test.php之后
如果ignoreError为true,则会添加>/dev/null 2>&1
当然还可以设置command的filter,当filter为ture,include可以只对正则匹配到的文件,调用command。

“refreshCDN”,就在同步过程中将文件发送到目的服务器后刷新cdn接口。如果不想使用,则将start属性设为false即可。如果需要使用其他插件,则查看其他plugin标签,将插件名称改为 xml中其它插件的名称即可。
该模块根据chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn接口发送需要刷新的路径位置。刷新CDN模块需要配置的xml文件如下。



<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/p_w_picpaths"/>
</localpath>
</plugin>



其中 localpath watch 是需要监控的目录。

cdnifo标签制定了cdn接口的域名,端口号,以及用户名与密码。

sendurl标签是需要刷新的url的前缀。

regexurl标签中的,regex属性为true时候,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url一部分,

举例:

如果产生文件事件为:/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/p_w_picpath/a/123.txt

经过上面的match正则匹配后,最后刷新的路径是:

http://pic.xoyo.com/cms/jx3/a/123.txt;

如果regex属性为false,最后刷新的路径是

http://pic.xoyo.com/cms/jx3.xoyo.com/p_w_picpaths/a/123.txt;

socket插件

socket插件,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息。

Http插件

http插件,可以向指定域名的主机post,inotify监控的事件。

单独运行插件

插件也可以单独使用,即不对远程目标机进行同步,直接调用插件:

只调用command插件



./sersync -d -m command



只调用refreshCDN插件



./sersync -d -m refreshCDN



只调用socket插件



./sersync -d -m socket



只调用http插件



./sersync -d -m http



 






 

 


转载于:https://blog.51cto.com/opsarno/1408974