使用sersync实时同步文件

sersync的介绍

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

安装rsync

在使用sersync之前,我们必须安装配置好rsync服务器。这里我们需要注意的是,纯粹的使用rsync做单向同步时,rsync的守护进程是运行在文件推送的服务器上,而接收的服务器是运行rsync客户端。使用sersync做文件实时同步刚好相反,用于接收文件的服务器运行rsync守护进程。
安装rsync的步骤在此不叙述,请看以前的教程配置:https://www.centos.bz/2011/06/rsync-server-setup/或者使用本站提供的脚本更容易地安装:https://www.centos.bz/2011/09/centos-one-key-configure-rsync-server-script/

安装sersync

到这里http://coolcode.google.com/p/sersync/downloads/list下载最新的二进制安装包,现在最新的版本是sersync2.5,我们以centos-32位为例讲解。

  1. wget http://sersync.googlecoolcode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz
  2. mkdir /usr/sersync
  3. tar xzf sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/sersync/

就这样,sersync安装完成,下面介绍如何配置及使用。

配置sersync

sersync的配置文件在/usr/sersync/confxml.xml。
首先创建连接rsyncd的密码文件:

  1. echo "123456" >/usr/sersync/rsync.pas
  2. chmod 600 /usr/sersync/rsync.pas

下面是confxml.xml文件的一些配置解释:

  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=”true”>
  7. <exclude expression=”(.*)\.php”></exclude>
  8. <exclude expression=”(.*)\.html”></exclude>
  9. <exclude expression=”(.*)\.htm”></exclude>
  10. <exclude expression=”^tmp/*”></exclude>
  11. <!—监控事件的过程中过滤特定文件,和特定文件夹的文件 –>
  12. </filter>
  13. <inotify>
  14. <delete start=”true”/>
  15. <createFolder start=”true”/>
  16. <createFile start=”true”/>
  17. <closeWrite start=”true”/>
  18. <moveFrom start=”true”/>
  19. <moveTo start=”true”/>
  20. <attrib start=”false”/>
  21. <modify start=”true”/>
  22. <!—设置要监控的事件 –>
  23. </inotify>
  24.  
  25. <sersync>
  26. <localpath watch=”/var/www”>
  27. <!—设置要监控的目录 –>
  28. <remote ip=”xx.xx.xx.xx” name=”pppei”/>
  29. <!—指定远端rsync服务器的地址和模块名 –>
  30. </localpath>
  31. <rsync>
  32. <commonParams params=”-artuz”/>
  33. <auth start=”true” users=”pppei” passwordfile=”/usr/sersync/rsync.pas”/>
  34. <!—是否启用验证,并指定密码存放文件 –>
  35. <userDefinedPort start=”false” port=”874″/><!– port=874 –>
  36. <timeout start=”false” time=”100″/><!– timeout=100 –>
  37. <ssh start=”false”/>
  38. </rsync>
  39. <failLog path=”/tmp/rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>
  40. <crontab start=”true” schedule=”1440″><!–600mins–>
  41. <!—是否启用执行完整rsync,并指定执行周期 –>
  42. <crontabfilter start=”true”>
  43. <!—设置完整执行rsync时的过滤条件 –>
  44. <exclude expression=”*.php”></exclude>
  45. <exclude expression=”*.html”></exclude>
  46. <exclude expression=”*.htm”></exclude>
  47. <exclude expression=”tmp/*”></exclude>
  48. </crontabfilter>
  49. </crontab>
  50. <plugin start=”false” name=”command”/>
  51. </sersync>
  52.  
  53. <plugin name=”command”>
  54. <param prefix=”/bin/sh” suffix=”" ignoreError=”true”/>  <!–prefix /opt/tongbu/mmm.sh suffix–>
  55. <filter start=”false”>
  56. <include expression=”(.*)\.php”/>
  57. <include expression=”(.*)\.sh”/>
  58. </filter>
  59. </plugin>
  60.  
  61. <plugin name=”socket”>
  62. <localpath watch=”/opt/tongbu”>
  63. <deshost ip=”192.168.138.20″ port=”8009″/>
  64. </localpath>
  65. </plugin>
  66. <plugin name=”refreshCDN”>
  67. <localpath watch=”/data0/htdocs/cms.xoyo.com/site/”>
  68. <cdninfo domainname=”ccms.chinacache.com” port=”80″ username=”xxxx” passwd=”xxxx”/>
  69. <sendurl base=”http://pic.xoyo.com/cms”/>
  70. <regexurl regex=”false” match=”cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images”/>
  71. </localpath>
  72. </plugin>
  73. </head>

请根据自己的具体情况修改。

sersync2命令使用说明

1.在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。

  1. ./sersync -d

过程如下:

  1. [root@localhost GNU-Linux-x86]# ls
  2. confxml.xml  sersync2
  3. [root@localhost GNU-Linux-x86]# ./sersync2 -d
  4. set the system param
  5. execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
  6. execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
  7. parse the command param
  8. daemon thread num: 10
  9. parse xml config file
  10. host ip : localhost     host port: 8008
  11. config xml parse success
  12. please set /etc/rsyncd.conf max connections=0 Manually
  13. sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
  14. please according your cpu ,use -n param to adjust the cpu rate
  15. run the sersync:
  16. watch path is: /opt/tongbu

表明,sersync已经开启,可以在本地监控路径下建立文件,查看远程是否同步成功。

2.在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步

  1. ./sersync -r

如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效

3.查看启动参数帮助

  1. ./sersync --help

4.指定配置文件

  1. ./sersync -o XXXX.xml

对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。

5.指定默认的线程池的线程总数

  1. ./sersync -n num

例如 ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。

6.不进行同步,只运行插件

  1. ./sersync -m pluginName

例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。

7.多个参数可以配合使用

  1. ./sersync -n 8 -o abc.xml -r -d

表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。

8.通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。

  1. ./sersync -r -d

最后需要把sersync命令加入到/etc/rc.local以开机自启动:

  1. echo "/usr/sersync/sersync2 -d -o /usr/sersync/confxml.xml" >>/etc/rc.local

如果需要同步多个目录,可以创建多个配置文件,如/usr/sersync/sersync2 -d -o /usr/sersync/xxx.xml

参考:
http://www.pppei.net/blog/post/124
官方教程

标签:Rsync 发布于:2019-11-24 11:49:45