---
title: "sersync2 同步"
author: "flashdream8"
date: "2013-09-17T19:06:48Z"
tags: []
categories: ["企业架构"]
slug: "sersync2"
draft: false
lastmod: "2013-09-17T19:06:48Z"
id: 1943422
url: "https://flashdream8.iteye.com/blog/1943422"
---

sersync2 实时同步配置

 

 

 

**自动同步文件或者是目录**

**实现的方式：**

在同步服务器上开启sersync，将监控路径中的文件同步到目标服务器，因此需要在同步服务器配置sersync，在同步目标服务器配置rsync

实验机器的地址分配：

Rsync 服务器：   192.168.1.112

Sersync2 服务器：192.168.1.110

**系统环境**

\[root@liyao home]# uname -a  
       Linux liyao 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 i686 i386 GNU/Linux  
       \[root@liyao home]#

一、     rsync 服务器的配置

1.       先查看下是否安装了 rsync 软件包

\[root@liyao ~]# rpm -qa | grep rsync

rsync-2.6.8-3.1

\[root@liyao ~]#

2.     如果系统默认安装了 rsync 软件包，我们可以使用 yum 命令将其卸载

\[root@liyao ~]# yum remove -y rsync

Loaded plugins: fastestmirror

Setting up Remove Process

Resolving Dependencies

--&gt; Running transaction check

---&gt; Package rsync.i386 0:2.6.8-3.1 set to be erased

--&gt; Finished Dependency Resolution

 

Dependencies Resolved

 

================================================================================

 Package         Arch           Version               Repository           Size

================================================================================

Removing:

 rsync           i386           2.6.8-3.1             installed           392 k

 

Transaction Summary

================================================================================

Remove        1 Package(s)

Reinstall     0 Package(s)

Downgrade     0 Package(s)

 

Downloading Packages:

Running rpm\_check\_debug

Running Transaction Test

Finished Transaction Test

Transaction Test Succeeded

Running Transaction

  Erasing        : rsync                                                    1/1

 

Removed:

  rsync.i386 0:2.6.8-3.1                                                       

 

Complete!

\[root@liyao ~]#

3.     下载 rsync 3.0 的源码包

\[root@liyao~]#wget-chttp://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz

--2011-08-20 08:05:05--  http://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz

Resolving rsync.samba.org... 216.83.154.106, 2001:470:1f05:1a07::1

Connecting to rsync.samba.org|216.83.154.106|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 790722 (772K) \[application/x-gzip]

Saving to: \`rsync-3.0.8.tar.gz'

 

100%\[======================================&gt;] 790,722      153K/s   in 5.7s   

 

2011-08-20 08:05:11 (135 KB/s) - \`rsync-3.0.8.tar.gz' saved \[790722/790722]

4.     将该软件包解压缩，并且进行编译安装

\[root@liyao ~]# tar zxf rsync-3.0.8.tar.gz

\[root@liyao ~]# cd rsync-3.0.8

\[root@liyao rsync-3.0.8]# ./configure --prefix=/usr/ && make && make install

5.     建立 rsync 服务器的配置文件

uid = nobody

gid = nobody

use chroot = no

strict modes = yes

max connections = 200

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log 

\[test]

path = /home/

comment = test

read only = no

write only = no

ignore errors = yes

list = yes

uid = root

gid = root

6.     启动 rsync 的服务

  \[root@liyao ~]# rsync --daemon

\[root@liyao ~]# netstat -npl | grep 873

tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      7731/rsync         

tcp        0      0 :::873                      :::*                        LISTEN      7731/rsync 

 

二、     配置Sersync2 服务器

二进制直接可运行

wget [http://sersync.googlecode.com/files/sersync2.5\_64bit\_binary\_stable\_final.tar.gz](http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz)

[http://sersync.googlecode.com/files/sersync2.5\_32bit\_binary\_stable\_final.tar.gz](http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz "http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz")

1、将该软件包下载下来之后解压缩，并进行安装

\[root@liyao ~]# tar zxf sersync2.5\_32bit\_binary\_stable\_final.tar.gz

\[root@liyao ~]# ls

anaconda-ks.cfg  install.log.syslog

GNU-Linux-x86    sersync2.5\_32bit\_binary\_stable\_final.tar.gz

install.log

\[root@liyao ~]# cd GNU-Linux-x86/

\[root@liyao GNU-Linux-x86]# ls

confxml.xml  sersync2

\[root@liyao GNU-Linux-x86]# mkdir /etc/sersync2

\[root@liyao GNU-Linux-x86]# cp confxml.xml /etc/sersync2/

\[root@liyao GNU-Linux-x86]# cp sersync2 /usr/bin/

\# 安装该软件只要把软件中的文件拷贝到相应的目录即可

2\. 修改 Sersync2 的配置文件

\[root@liyao ~]# cat /etc/sersync2/confxml.xml

&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;

&lt;head version="2.5"&gt;

    &lt;host hostip="localhost" port="8008"&gt;&lt;/host&gt;

    &lt;debug start="false"/&gt;

    &lt;fileSystem xfs="false"/&gt;

    &lt;filter start="false"&gt;

        &lt;exclude expression="(.\*)\\.svn"&gt;&lt;/exclude&gt;

        &lt;exclude expression="(.\*)\\.gz"&gt;&lt;/exclude&gt;

        &lt;exclude expression="^info/\*"&gt;&lt;/exclude&gt;

        &lt;exclude expression="^static/\*"&gt;&lt;/exclude&gt;

    &lt;/filter&gt;

    &lt;inotify&gt;

        &lt;delete start="true"/&gt;

        &lt;createFolder start="true"/&gt;

        &lt;createFile start="false"/&gt;

        &lt;closeWrite start="true"/&gt;

        &lt;moveFrom start="true"/&gt;

        &lt;moveTo start="true"/&gt;

        &lt;attrib start="false"/&gt;

        &lt;modify start="false"/&gt;

    &lt;/inotify&gt;

 

    &lt;sersync&gt;

        &lt;localpath watch="/home"&gt;

            &lt;remote ip="192.168.1.112" name="test"/&gt;

            # 指定 rsyncd 服务器的地址和备份的模块名称

            &lt;!--&lt;remote ip="192.168.8.39" name="tongbu"/&gt;--&gt;

            &lt;!--&lt;remote ip="192.168.8.40" name="tongbu"/&gt;--&gt;

        &lt;/localpath&gt;

        &lt;rsync&gt;

            &lt;commonParams params="-artuz"/&gt;

            &lt;auth start="false" users="root" passwordfile="/etc/rsync.pas"/&gt;

            &lt;userDefinedPort start="false" port="874"/&gt;&lt;!-- port=874 --&gt;

            &lt;timeout start="false" time="100"/&gt;&lt;!-- timeout=100 --&gt;

            &lt;ssh start="false"/&gt;

        &lt;/rsync&gt;

        &lt;failLog path="/var/log/rsync\_fail.log" timeToExecute="60"/&gt;&lt;!--default every 60mins execute once--&gt;

\# 指定日志存放的路径

        &lt;crontab start="false" schedule="600"&gt;&lt;!--600mins--&gt;

            &lt;crontabfilter start="false"&gt;

                &lt;exclude expression="\*.php"&gt;&lt;/exclude&gt;

                &lt;exclude expression="info/\*"&gt;&lt;/exclude&gt;

            &lt;/crontabfilter&gt;

        &lt;/crontab&gt;

        &lt;plugin start="false" name="command"/&gt;

    &lt;/sersync&gt;

 

    &lt;plugin name="command"&gt;

        &lt;param prefix="/bin/sh" suffix="" ignoreError="true"/&gt;  &lt;!--prefix /opt/tongbu/mmm.sh suffix--&gt;

        &lt;filter start="false"&gt;

            &lt;include expression="(.\*)\\.php"/&gt;

            &lt;include expression="(.\*)\\.sh"/&gt;

        &lt;/filter&gt;

    &lt;/plugin&gt;

 

    &lt;plugin name="socket"&gt;

        &lt;localpath watch="/opt/tongbu"&gt;

            &lt;deshost ip="192.168.138.20" port="8009"/&gt;

        &lt;/localpath&gt;

    &lt;/plugin&gt;

    &lt;plugin name="refreshCDN"&gt;

        &lt;localpath watch="/data0/htdocs/cms.xoyo.com/site/"&gt;

            &lt;cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/&gt;

            &lt;sendurl base="http://pic.xoyo.com/cms"/&gt;

            &lt;regexurl regex="false" match="cms.xoyo.com/site(\[/a-zA-Z0-9]\*).xoyo.com/images"/&gt;

        &lt;/localpath&gt;

    &lt;/plugin&gt;

&lt;/head&gt;

\[root@liyao ~]#

3\. 我们来运行如下的备份命令

在运行备份命令之前我们先看下 /home 这个目录下面有文件或者是目录

\[root@liyao home]# ls -l

total 4

drwxr-xr-x 2 root root 4096 Aug 20 08:51 liyao

\[root@liyao home]#

\[root@liyao ~]# sersync2 -r -d -o /etc/sersync2/confxml.xml

set the system param

execute：echo 50000000 &gt; /proc/sys/fs/inotify/max\_user\_watches

execute：echo 327679 &gt; /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：  /etc/sersync2/confxml.xml

daemon thread num: 10

parse xml config file

host ip : localhost     host port: 8008

daemon start，sersync run behind the console

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 && rsync -artuz -R --delete ./ 192.168.1.112::test &gt;/dev/null 2&gt;&1

run the sersync:

watch path is: /home

4\. 我们到 rsyncd 服务器（1.112）的 /home 目录下看看是否有我在 1.110 /home/liyao 这个目录

\[root@liyao home]# ls

liyao

\[root@liyao home]#

 

那我们在从 1.110 的 /home 目录下创建 abc 目录，不用执行任何命令他会自动同步到 1.112 上

\[root@liyao home]# mkdir abc

\[root@liyao home]# ls

abc  liyao

\[root@liyao home]#

 

我们在去 1.112 上去看下，abc 目录已经同步过来了

\[root@liyao home]# ll

total 8

drwxr-xr-x 2 root root 4096 Aug 21 04:12 abc

drwxr-xr-x 2 root root 4096 Aug 21 04:05 liyao

\[root@liyao home]#

 

OK ，这样我们就大功告成了。