🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 一、Sersync项目简介与框架设计 [TOC] ## 1.1 项目简介   Sersync项目利用inotify与rsync技术实现对服务器数据实时同步的解决方案,其中inotify用于监控sersync所在服务器上文件系统的时间变化,rsync是目前广泛使用的本地及异地数据同步工具,其优点是只对变化的目录数据操作,甚至是一个文件不同的部分进行同步,所以其优势大大超过使用挂接文件系统或scp等方式进行镜像同步。   目前使用的比较多的同步程序版本是inotify-tools,另外一个是google开源项目Openduckbill(依赖于inotify-tools),这两个都是基于脚本语言编写的,其设计思路同样是采用inotify与rsync命令。   相比上面两个项目,sersync项目的优点是   1. 使用C++编写,对于Linux系统文件产生的临时文件和重复的文件操作会进行过滤,再结合了rsync同步的时候,会减少运行时消耗的本地及网络资源,因此速度会更快;   2. 相比较上面两个项目,sersync配置起来很简单,其中bin目录下是已经编译好的二进制文件,配合bin目录下的xml文件直接使用即可;   3. 使用多线程进行同步(即可以并发同步多个不同文件),尤其在同步较大文件时,能够保证多个服务器实时保持同步状态;   4. sersync自带出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每10个小时对同步失败的文件再重新同步;   5. sersync自带crontab功能,只需在xml配置文件中开启,即可按预先的配置,隔一段时间整体同步一次;   6. sersync自带socket与http的协议拓展,可以满足有特殊需求的公司二次开发。 ## 1.2 基本构架 ![](https://box.kancloud.cn/2015-11-03_56385ed070ec9.png) ## 1.3 设计简析 1. 线程组线程是等待县城对立的守护线程,当事件队列中有数据产生的时候,线程组守护线程就会组个唤醒同步线程,当队列中inotify事件较多的时候,同步线程就会被全部唤醒一起工作。这样设计的目的是能够同时处理多个inotify事件,从而提升服务器的冰法同步能力。 之所以称之为线程组线程,是因为每个线程在工作的时候,会根据服务器上新写入文件的数量建立子线程,子线程可以保证所有的文件与各个服务器同时同步,当要同步的文件较大的时候,这样的设计可以保证各个远程服务器可以同事获得要同步的文件。 2. 服务器线程的作用有三个(保证主服务器和从服务器数据一致且实时): 2.1 处理同步失败的文件,将这些文件再次同步,对于再次同步失败的文件会生成rsync_fail_log.sh脚本,记录失败的事件 2.2 同时每个10小时执行脚本一次,同时清空脚本。 2.3 还有就是crontab功能,可以每隔一定事件,将所有路径整体同步一次。 3. 过滤队列的建立是为了过滤短时间内产生的重复的inotify信息,例如在删除文件夹的时候,inotify就会同事产生删除文件夹里的文件与删除文件夹的事件,通过过滤队列,当删除文件夹时间产生的时候,会将之前加入队列的删除文件的事件全部过滤掉,这样只产生一条删除文件夹事件,从而减轻了同步的负担。同时对于修改文件的操作的时候,会产生临时文件的重复操作。 # 二、sersync安装配置 ## 2.1 sersync同步需求逻辑图 当前版本的sersync依赖于rsync进行数据同步,如下图所示,在同步主服务器(Master)上开启sersync,sersync负责监控配置路径中的文件系统事件变化,然后调用rsync命令把更新的文件同步到目标服务器(Slave),因此,需要在主服务器配置sersync,在同步目标服务器配置rsync server(注意:是rsync服务器) ![](https://box.kancloud.cn/2015-11-03_56385ed31c2a9.png) 如图所示,用户会实时网左边的同步主服务器(M)上写入或更新文件数据,此时需要在左边的同步服务器(M)上配置sersync服务,在右边的同步目标服务器S1和S2服务器上配置rsync守护进程,这样在主服务器M上产生的写入或更新的文件就会被sersync服务实时的同步到多个目标服务器(S1,S2等)。一般的Linux服务器一般安装了rsync软件,因此在主服务器(M)上有rsync命令即可,在目标服务器(S1、S2)上只需要进行简单的配置,并开启rsync守候进程即可。 ## 2.2 安装环境准备 |角色|服务器配置|操作系统版本|IP|机器名| |-|-|-|-|-| |sersync服务(M)|VM|CentOS6.5/2.6.32-431.el6.x86_64|192.168.0.100|luo.centos5.5| |rsync服务(S1)|VM|CentOS6.5/2.6.32-431.el6.x86_64|192.168.0.101|luo.centos6.5| |rsync服务(S2)|VM|CetnOS6.7/2.6.32-573.el6.x86_64|192.168.0.102|luo.centos6.7| ## 2.3 配置同步服务器 ~~~ 部署总结: 步骤一、 配置slave上的rsync服务 步骤二、 配置master上sersync客户端 ~~~ ### 2.3.1 slave上部署rsync服务 本次测试使用的服务器为S1和S2 server,即IP为192.168.0.101,192.168.0.102。 #### 2.3.1.1 升级rsync到3.0 检查rsync版本,此操作在所有的机器上执行 ![](https://box.kancloud.cn/2015-11-03_56385ed332d11.png) 如果版本较低的5.*系统,rsync的本本为3以下,我们可以通过yum -y install rsync命令来升级。(非必须) #### 2.3.1.2 部署rsync服务 (Slave服务器上相关操作) 升级完毕后,在多台同步slave服务器(S1,S2)上配置如下: `vim /etc/rsyncd.conf` 写入如下测试内容: ~~~ # Minimal configuration file for rsync daemon # See rsync(1) and rsyncd.conf(5) man pages for help # This line is required by the /etc/init.d/rsyncd script # GLOBAL OPTIONS uid = root gid = root use chroot = no read only = false #limit access to private LANs hosts allow=192.168.0.100/24 hosts deny=* ignore errors max connections = 2000 pid file = /var/run/rsyncd.pid auth users = rsync_backup secrets file = /etc/rsyncd.password #lock file = /var/run/rsync.lock motd file = /etc/rsyncd/rsyncd.motd #This will give you a separate log file log file = /var/log/rsync.log #This will log every file transferred - up to 85,000+ per user, per sync transfer logging = yes log format = %t %a %m %f %b syslog facility = local3 timeout = 300 # MODULE OPTIONS [bbs] comment = bbs by www 2015年10月27日15:53:24 path = /var/www/html/bbs [blog] comment = blog by www 2015年10月27日15:53:24 path = /var/www/html/blog ~~~   上面的rsync服务的配置文件表明允许sersync主服务器(IP为192.168.0.100)访问,rsync同步模块名为[bbs],[blog]将同步过来的文件分别放入对应的path指定的目录 /var/www/html/bbs,/var/www/html/blog下,如果有多台目标服务器,则每台都需要进行类似的rsync服务配置,上面的uid,gid要换成本服务器对应的同步用户。注意rsync服务账户(上述用了root用户)要有对被同步目录(/var/www/html)的写入更新权限。 #### 创建待同步的目录 ~~~ [root@li-centos6.5 /var/www] # mkdir -p /var/www/html/bbs /var/www/html/blog [root@li-centos6.5 /var/www] # tree /var/www/html /var/www/html ├── bbs └── blog # 此步骤在S1,S2上都要执行,否则,rsync服务会因为没有path路径而无法启动。 ~~~ #### 配置相关权限认证 ~~~ echo "rsync_backup:rsync-aaaaaa">/etc/rsyncd.password chmod 600 /etc/rsyncd.password # order for check cat /etc/rsyncd.password ll /etc/rsyncd.password ~~~ #### 配置好后,使用下面的命令开启rsync守护进程 ~~~ rsync --daemon ps -ef|grep rsync netstat -tunpl|grep :873 ~~~ #### 配置开机自启动 ~~~ echo "/usr/bin/rsync --daemon">>/etc/rc.local grep demoan /etc/rc.local ~~~ #### 重启命令 ~~~ pkill rsync rsync --daemon ps -ef |grep rsync ~~~ ### 2.3.2 master上配置rsync客户端(Master操作) #### 2.3.2.1 master上配置rsync权限   在master(192.168.0.100)上配置rsync客户端相关权限 ~~~ echo 'rsync-aaaaaa' > /etc/rsync.password chmod 600 /etc/rsync.password cat /etc/rsync.password ll /etc/rsync.password ~~~ #### 2.3.2.2 master上手工测试rsync同步情况 >[danger]  此步骤非常关键且重要,如果这里测试都不成功,后面的sersync配好也不会同步数据,同时当sersync同步出错的时候我们也可以手工同步排错。 **1. 分别创建待同步数据** ~~~ [root@li.centos5.5 /var/www] # mkdir -p /var/www/html/bbs /var/www/html/blog # 创建待同步的目录 [root@li.centos5.5 ~] # touch /var/www/html/bbs/bbs.log /var/www/html/blog/blog.log # 在目录下创建带同步的文件 [root@li.centos5.5 /var/www] # tree /var/www/html/ # 检查目录和文件创建情况 /var/www/html/ |-- bbs | `-- bbg.log `-- blog `-- blog.log 2 directories, 2 files ~~~ **2. 执行同步命令(操作之前停用防火墙)** ~~~ rsync -avzP /var/www/html/bbs/ rsync_backup@192.168.0.101::bbs/ --password-file=/etc/rsync.password rsync -avzP /var/www/html/bbs/ rsync_backup@192.168.0.102::bbs/ --password-file=/etc/rsync.password rsync -avzP /var/www/html/blog/ rsync_backup@192.168.0.101::blog/ --password-file=/etc/rsync.password rsync -avzP /var/www/html/blog/ rsync_backup@192.168.0.102::blog/ --password-file=/etc/rsync.password ~~~ >[danger] 提示:在后面进行部署sersync前,sersync主服务器上必须要确保手工可以把文件推送到S1,S2上,这样后续sersync才能调用这些命令进行自动推送!!! 同步完后,可以到两台slave服务器上查看结果,如下: ~~~ [root@li-centos6.5 /var/www/html] # tree /var/www/html # 检查目录和文件同步情况 ├── bbs │ └── bbg.log └── blog └── blog.log 2 directories, 2 files [root@li-centos6.7 /var/www/html] # tree /var/www/html. # 检查目录和文件同步情况 ├── bbs │ └── bbg.log └── blog └── blog.log 2 directories, 2 files ~~~ ### 2.3.3 master上配置rsync客户端 #### 2.3.3.1 下载sersync 在google code ( `http://code.google.com/p/sersync/downloads/list` )下载sersync的可执行文件版本,里面有配置文件和可执行文件,如下: `cd /usr/local/src && wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz` #### 2.3.3.2 安装sersync ~~~ tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz mv GNU-Linux-x86 /usr/local/sersync [root@li.centos5.5 /usr/local] # tree /usr/local/sersync/ # 查看 sersync/ |-- confxml.xml `-- sersync2 0 directories, 2 files ~~~ #### 2.3.3.2 规范sersync目录结构(非必须) ~~~ cd /usr/local/sersync/ && mkdir conf bin logs mv confxml.xml ./conf/. && mv sersync2 ./bin/sersync [root@li.centos5.5 /usr/local/sersync] # tree . |-- bin | `-- sersync2 |-- conf | `-- confxml.xml `-- logs 3 directories, 2 files ~~~ #### 2.3.3.3 配置sersync ~~~ # /bin/cp conf/confxml.xml conf/confxml.xml.luo.$(date +%F) # 备份配置文件 [root@li.centos5.5 /usr/local/sersync] # ls conf/ # 查看配置文件备份情况 confxml.xml confxml.xml.luo.2015-10-29 ~~~ 初始化的配置文件内容如下: ~~~ 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> 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"/> 23 <sersync> 24 <localpath watch="/opt/tongbu"> # 定义本地要同步的目录 25 <remote ip="127.0.0.1" name="tongbu1"/> # 定义要同步的服务器IP和模块名(例如上述的blog、bbs) 26 <!--<remote ip="192.168.8.39" name="tongbu"/>--> 27 <!--<remote ip="192.168.8.40" name="tongbu"/>--> 28 </localpath> 29 <rsync> 30 <commonParams params="-artuz"/> 31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> 32 <userDefinedPort start="false" port="874"/><!-- port=874 --> 33 <timeout start="false" time="100"/><!-- timeout=100 --> 34 <ssh start="false"/> 35 </rsync> 36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--defaul t every 60mins execute once--> 37 <crontab start="false" schedule="600"><!--600mins--> 40 <exclude expression="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/tongb u/mmm.sh suffix--> 48 <filter start="false"> 49 <include expression="(.*)\.php"/> 50 <include expression="(.*)\.sh"/> 51 </filter> 52 </plugin> 53 54 <plugin name="socket"> 55 <localpath watch="/opt/tongbu"> 56 <deshost ip="192.168.138.20" port="8009"/> 57 </localpath> 58 </plugin> 59 <plugin name="refreshCDN"> 60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passw d="xxxx"/> 62 <sendurl base="http://pic.xoyo.com/cms"/> 63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/ images"/> 64 </localpath> 65 </plugin> 66 </head> ~~~ **1) 修改24~28行的内容** ~~~ 24 <localpath watch="/opt/tongbu"> # 定义本地要同步的目录 25 <remote ip="127.0.0.1" name="tongbu1"/> # 定义要同步的服务器IP和模块名(例如上述的blog、bbs) 26 <!--<remote ip="192.168.8.39" name="tongbu"/>--> 27 <!--<remote ip="192.168.8.40" name="tongbu"/>--> 28 </localpath> ~~~ **修改后如下:** ~~~ <localpath watch="/var/www/html/blog"> # 定义本地要同步的目录 <remote ip="192.168.0.101" name="blog"/> # 定义要同步的服务器IP和模块名(例如上述的blog、bbs) <remote ip="192.168.0.102" name="blog"/> </localpath> <localpath watch="/var/www/html/bbs"> # 定义本地要同步的目录 <remote ip="192.168.0.101" name="bbs"/> # 定义要同步的服务器IP和模块名(例如上述的blog、bbs) <remote ip="192.168.0.102" name="bbs"/> </localpath> ~~~ **如下图所示:** ![](https://box.kancloud.cn/2015-11-03_56385ed342dee.png) **2) 修改31~34行(认证部分)** ~~~ 31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> 32 <userDefinedPort start="false" port="874"/><!-- port=874 --> 33 <timeout start="false" time="100"/><!-- timeout=100 --> 34 <ssh start="false"/> ~~~ **修改后的内容如下:** ~~~ <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <!--slave服务器的用户名--> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="true" time="100"/><!-- timeout=100 --> <ssh start="false"/> ~~~ 其实以上的配置文件就是在拼接同步语句`rsync -avzP /var/www/html/blog/ rsync_backup@192.168.0.102::blog/ --password-file=/etc/rsync.password` 这个命令的参数而已。 **如下图所示:** ![](https://box.kancloud.cn/2015-11-03_56385ed35ca9c.png) **3) 修改36~37行** `36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->` **修改后的内容如下:** `36 <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->` 当同步失败后,日志记录到`/usr/local/sersync/logs/rsync_fail_log.sh`文件中,并且每60分钟对失败的log进行重新同步。 **修改后的完整配置文件如下:** ~~~ [root@luo.centos5.5 /usr/local/sersync] # cat -n /usr/local/sersync/conf/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="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="/var/www/html/blog"> 25 <remote ip="192.168.0.101" name="blog"/> 26 <remote ip="192.168.0.102" name="blog"/> 27 </localpath> 28 <localpath watch="/var/www/html/bbs"> 29 <remote ip="192.168.0.101" name="bbs"/> 30 <remote ip="192.168.0.102" name="bbs"/> 31 </localpath> 32 <rsync> 33 <commonParams params="-artuz"/> 34 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> 35 <userDefinedPort start="false" port="874"/><!-- port=874 --> 36 <timeout start="true" time="100"/><!-- timeout=100 --> 37 <ssh start="false"/> 38 </rsync> 39 <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> 40 <crontab start="false" schedule="600"><!--600mins--> 41 <crontabfilter start="false"> 42 <exclude expression="*.php"></exclude> 43 <exclude expression="info/*"></exclude> 44 </crontabfilter> 45 </crontab> 46 <plugin start="false" name="command"/> 47 </sersync> 48 49 <plugin name="command"> 50 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> 51 <filter start="false"> 52 <include expression="(.*)\.php"/> 53 <include expression="(.*)\.sh"/> 54 </filter> 55 </plugin> 56 57 <plugin name="socket"> 58 <localpath watch="/opt/tongbu"> 59 <deshost ip="192.168.138.20" port="8009"/> 60 </localpath> 61 </plugin> 62 <plugin name="refreshCDN"> 63 <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 64 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 65 <sendurl base="http://pic.xoyo.com/cms"/> 66 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 67 </localpath> 68 </plugin> 69 </head> ~~~ #### 2.3.3.4 开启sersync守护进程同步数据 配置sersync环境变量 ~~~ echo 'export PATH=/usr/local/sersync/bin:$PATH'>>/etc/profile tail -1 /etc/profile source /etc/profile which sersync ~~~ **启动sersync命令:** `sersync -r -d -o /usr/local/sersync/conf/confxml.xml` 正确的配置启动结果如下: ~~~ [root@luo.centos6.5 /usr/local/sersync/conf] # ~/rsesync_start.sh 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 option: -d run as a daemon option: -o config xml name: /usr/local/sersync/conf/bbs_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 rsync_backup passwordfile is /etc/rsync.password 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: 32 = 12(Thread pool nums) + 20(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/bbs && rsync -artuz -R --delete ./ --timeout=100 rsync_backup@192.168.0.101::bbs --password-file=/etc/rsync.password >/dev/null 2>&1 run the sersync: watch path is: /var/www/html/bbs set the system param ~~~ 测试结果:发现问题,多实例的情况下仅第一个模块的路径能同步,其他模块下面的路径不能同步。 **配置多实例的同步情况**(针对多个模块同步的情况) ~~~ sersync –d –o /usr/local/serysnc/conf/blog_confxml.xml sersync –d –o /usr/local/serysnc/conf/bbs_confxml.xml … … ~~~ 不同的config文件是将模块分开放置,即:**2.3.3.3**步骤中的第一步中的代码分开在几个不同的文件中,这样启动的时候指定模块同步。 **将上述命令,写入/etc/rc.local中,随系统自启动** ~~~ /bin/cp /etc/rc.local /etc/rc.local_luo_$(date +%F) cat >>/etc/rc.local<<EOF # sersync data to 192.168.0.101,192.168.1.102 sersync –d –o /usr/local/serysnc/conf/blog_confxml.xml sersync –d –o /usr/local/serysnc/conf/bbs_confxml.xml EOF [root@luo.centos6.5 /var/www/html] # tail -3 /etc/rc.local # sersync data to 192.168.0.101,192.168.1.102 sersync –d –o /usr/local/serysnc/conf/blog_confxml.xml sersync –d –o /usr/local/serysnc/conf/bbs_confxml.xml ~~~ 至此,已经完成了Master上对Slave1,Slave2上多实例(bbs,blog多模块)的同步。 **Sersync同步参数说明** |Sersync参数|说明| |-|-| | -r | -r参数作用是在开启实时监控之前对主服务器目录与远程目标机目录进行一次整体同步,如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则需要指定-r参数运行sersync,将本地与远程整体同步一次**。说明:如果设置了过滤器,即在xml配置文件中,filter为true,则暂时不能使用-r参数进行整体同步。**| |–o confxml.xml|不指定-o参数时,sersync可执行文件目录下的默认配置文件confxml.xml,如果需要使用其他的配置文件,可以使用-o参数指定其他配置文件,通过-o参数,我们可以指定多个不同的配置文件,从而实现sersync多进行多实例的数据同步。| |–n number|-n参数为指定默认的线程池的现充总数。| 例如:sersync –n 5 则指定线程总数为5,如果不指定。默认启用线程池数量为10,如果cpu使用过高,可通过该参数调低,如果机器配置较高,可以通过该参数调高线程总数,提高同步效率 |-d|-d参数为后台启动服务,在通常情况下,使用-r参数对本地到远程整体同步一遍后,在后台运行此参数启动守护进程实时同步,在第一次整体同步时,-d和-r参数经常会联合使用。| |-m pluginName|-m参数为不进行同步,只运行插件 例如 sersync –m command,则在监控到时间后,不对远程目标服务器进行同步,而是直接运行command插件。| # 三、sersync配置文件说明 ## 3.1 XML配置文件说明 Sersync可选功能是通过xml配置文件来实现的,基本配置文件内容如下 **初始化配置文件:** ~~~ [root@luo.centos6.5 /usr/local/sersync] # cat -n conf/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="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 <remote ip="127.0.0.1" name="tongbu1"/> 26 <!--<remote ip="192.168.8.39" name="tongbu"/>--> 27 <!--<remote ip="192.168.8.40" name="tongbu"/>--> 28 </localpath> 29 <rsync> 30 <commonParams params="-artuz"/> 31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> 32 <userDefinedPort start="false" port="874"/><!-- port=874 --> 33 <timeout start="false" time="100"/><!-- timeout=100 --> 34 <ssh start="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 <crontabfilter start="false"> 39 <exclude expression="*.php"></exclude> 40 <exclude expression="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 <include expression="(.*)\.php"/> 50 <include expression="(.*)\.sh"/> 51 </filter> 52 </plugin> 53 54 <plugin name="socket"> 55 <localpath watch="/opt/tongbu"> 56 <deshost ip="192.168.138.20" port="8009"/> 57 </localpath> 58 </plugin> 59 <plugin name="refreshCDN"> 60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 62 <sendurl base="http://pic.xoyo.com/cms"/> 63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 64 </localpath> 65 </plugin> 66 </head> ~~~ 下面一一进行解释说明: xml配置文件的注释不是 "#",而是 "<!—中间是注释的内容书写区域--> " ` 3 <host hostip="localhost" port="8008"></host>` hostip和port是针对插件的保留字段,对于同步功能没有任何作用,默认保留即可。 ## 3.2 debug开启开关 ` 4 <debug start="false"/>` 设置为true值时,开启debug模式,会在sersync正在运行的控制台,打印inotify时间与rsync的同步命令。 ## 3.3 xfs文件系统开关 `5 <fileSystem xfs="false"/>` 对于xfs文件系统的用户,需要将这个选项开启,才能使sersync正常工作。 ### 3.4 filter文件过滤功能 对于sersync监控的文件,会默认过来系统的临时文件(以"."开头,以"~"结尾),除了这些文件意外,可以自定义其他需要过滤的文件。 ~~~ 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> ~~~ 将start设置为true后开启过滤功能,在exclude标签中,填写正则表达式,默认给出两个例子分别是过滤以".gz"结尾的文件与过滤监控目录下的info路径(监控路径^/info /*),可以根据需要添加,但开启的时候要提前测试,正则表达式如果出现错误,控制台会有所提示,相比较使用rsync的exclude功能,被过滤的路径不会加入监控,大大减少了rsync同步的通讯量。 ## 3.5 inotify监控参数设定(调优) 对于inotify监控参数可以进行设置,根据项目的特点优化sersync ~~~ 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> ~~~ 对于大多数的应用,可以尝试吧createFile(监控文件时间选项)设置为false来提高性能,减少rsync通讯,因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事件close_write,同样可以实现文件的完整同步。 注意:要使得createFolder保持为true,如果将createFolder设为flase,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控,除非有特殊需要,否则建议开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete参数设置为false,则不对删除事件监控。 ## 3.6 文件监控与远程同步设置 ~~~ 24 <localpath watch="/opt/tongbu"> 25 <remote ip="127.0.0.1" name="tongbu1"/> 26 <!--<remote ip="192.168.8.39" name="tongbu"/>--> 27 <!--<remote ip="192.168.8.40" name="tongbu"/>--> 28 </localpath> ~~~ /opt/tongbu 目录为sersync主服务器本地待同步的目录。 ip=192.168.8.39,为服务器的IP地址,如果有多个从服务器依次列出来即可。 Name=tonngbu,这里的tongbu是slave服务器上的rsyncd.conf中的模块名字。 ## 3.7 rsync相关参数设置 ~~~ 29 <rsync> 30 <commonParams params="-artuz"/> 31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> 32 <userDefinedPort start="false" port="874"/><!-- port=874 --> 33 <timeout start="false" time="100"/><!-- timeout=100 --> 34 <ssh start="false"/> 35 </rsync> ~~~ 在commonParams项中,可以自定义rsync的同步参数,默认是-artuz。 auth start=false,设置为true的时候,使用rsync的认证模式传送,需要配置user与passwordfile(--password-file=/etc/rsyncpassword),来使用。 userDefinedPort当远程同步目标服务器的rsync端口不是默认端口的时候使用(-port=874)。 timeout设置rsync的timeout时间(-timeout=100)。 ssh start=false 如果改成true时表示ssh使用rsync –e ssh的方式进行传输。 ## 3.8 失败日志脚本配置 ` 36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->` 如果文件或文件夹同步传输失败,会送心传送,再次失败就会写入 `/tmp/rsync_fail_log.sh`,然后每隔一段时间(timeToExecute进行设置),执行该脚本再重新传送,然后清空该脚本,可以通过path来设置日志路径。 ## 3.9 crontab定期整体同步功能 ~~~ 37 <crontab start="false" schedule="600"><!--600mins--> 38 <crontabfilter start="false"> 39 <exclude expression="*.php"></exclude> 40 <exclude expression="info/*"></exclude> 41 </crontabfilter> 42 </crontab> ~~~ crontab可以对健康服务与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了crontab功能,还可以进一步保证各服务器文件一直,如果文件两比较大,crontab的时间间隔要设置大些,否则可能增加服务器之间通讯的开销。 schedule参数时设置crontab的时间间隔,默认每600分钟同步一次。 如果开启了filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候,如果不单独设置crontabfilter,还会将要过滤的文件同步到远程从服务器,crontab的过滤正则与filter过滤不同,也给出了两个实例分别对应于过滤文件与目录。总之如果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与filter的过滤一一对应。 ## 3.10 插件设置 ` 43 <plugin start="false" name="command"/>` 当start=false处设置为true时,将文件同步到元辰服务器后会调用name参数指定的插件。参见插件设置。 # 四、sersync插件基本配置和使用 ## 4.1插件部分xml内容 ~~~ 46 <plugin name="command"> 47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> 48 <filter start="false"> 49 <include expression="(.*)\.php"/> 50 <include expression="(.*)\.sh"/> 51 </filter> 52 </plugin> 53 54 <plugin name="socket"> 55 <localpath watch="/opt/tongbu"> 56 <deshost ip="192.168.138.20" port="8009"/> 57 </localpath> 58 </plugin> 59 <plugin name="refreshCDN"> 60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 62 <sendurl base="http://pic.xoyo.com/cms"/> 63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 64 </localpath> 65 </plugin> ~~~ 如上所示,其中plugin标签设置为true时,在同步文件或路径到远程服务器之后,会调用插件。通过name参数指定需要执行的插件,目前支持的有command refreshCDN socket http四种插件,其中 http插件目前由于兼容性原因已经去除,以后会重新加入。 以下模块(command refreshCDN socket http)可以大度使用(发生文件爱你改变不同步只调用插件),只需在命令行下使用-m参数即可,如果需要作为插件与同步程序一起使用,详见同步程序说明的插件设置。 ## 4.2 command插件 ~~~ 46 <plugin name="command"> 47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> 48 <filter start="false"> 49 <include expression="(.*)\.php"/> 50 <include expression="(.*)\.sh"/> 51 </filter> 52 </plugin> ~~~ 当文件同步完成后,毁掉红command插件,如同步文件是index.php,则index.php文件在改动之后,调用rsync同步到远程服务器后,调用command插件,执行 `/bin/sh index.php suffix >/dev/null 2>&1` 如果suffix设置了,则会放在inotify事件test.php之后,如果ignoreError为true,则会添加 >/dev/null 2>&1,当然还可以设置command的filter,当filter为true时,include可以只对正则匹配到的文件调用command。 “refreshCDN”就是同步过程中将文件发送到目的服务器后刷新cdn接口,如果不想使用,则将start属性设置为flase即可。 如果需要使用其他插件,则查看其他差价你的plugin标签,将插件名称改为xml中其他插件的名称即可,该模块根据chinaCND的协议进行设计,当有文件产生的时候,就像cdn接口发送需要刷新的路径位置,刷新CDN模块需要配置xml文件如下: ~~~ 59 <plugin name="refreshCDN"> 60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 62 <sendurl base="http://pic.xoyo.com/cms"/> 63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 64 </localpath> ~~~ 其中,`localpath watch=/data0/htdocs/cms.xoyo.com/site/` 是需要监控的目录,cdninfo标签指定了cdn借口的域名。端口号以及用户名和密码。 Sendurl标签是需要刷新的url的前缀。 regexurl属性为true时,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url的一部分。 ## 4.3 socket插件 socket插件,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息。 ## 4.4 http插件 http插件,可以向指定域名的主机post,inotify监控的事件。目前已经去除。 ## 4.5 单独运行插件 插件也可以单独运行,即不对远程目标主句进行同步,直接调用插件: 只调用command插件 `sersync –d –m command` 只调用refreshCDN插件 `sersync –d –m refreshCDN` 只调用socket插件 `sersync –d –m socket` 只调用http插件 `sersync –d –m http`