Sersync实时同步

sersync利用inotify与rsync对服务器进行实时同步,其中inotify用于监控文件系统事件,rsync是目前广泛使用的同步算法,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。由金山的周洋开发完成,是目前使用较多的文件同步工具之一。该工具和其他的工具相比有如下优点:

sersync是使用c++编写,由于只同步发生更改的文件,因此比其他同步工具更节约时间、带宽;

安装方便、配置简单;

使用多线程进行同步,能够保证多个服务器实时保持同步状态;

自带出错处理机制,通过失败队列对出错的文件重新出错,如果仍旧失败,则每10个小时对同步失败的文件重新同步;

自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次;

自带socket与http协议扩展,你可以方便的进行二次开发;

数据从192.168.10.180同步到192.168.10.175


客户端:192.168.10.175  

安装rsync

1
yum -y install rsync

cat /etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pid file =/var/run/rsyncd.pid
lock file =/var/run/rsync.lock
log file =/var/log/rsyncd.log
uid = nobody
gid = nobody
use chroot = no
[data]
path =/data
ignore errors = no 
read only = no  
write only = no   
uid = root
gid = root
auth users = root  
secrets file = /etc/rsync.password

[root@rsyncServer data]# cat /etc/rsync.password 

1
root:123456

启动rsync:/usr/bin/rsync --daemon


服务端:192.168.10.180

安装rsync

1
yum -y install rsync

新建/etc/rsync.password 文件

1
2
[root@rsyncServer data]# cat /etc/rsync.password 
123456

安装sersync 

1
2
3
4
wget https://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
tar -zxvf sersync2.5_64bit_binary_stable_final.tar.gz
mkdir /usr/local/sersync/
mv GNU-Linux-x86/* /usr/local/sersync/

vim confxml.xml

1
2
3
4
5
6
7
8
9
10
11
   <sersync>
        <localpath watch="/data">    #设置监控目录,有文件改动就会同步。在服务端
            <remote ip="192.168.10.175" name="data"/>  #客户端IP及模块名
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="root" passwordfile="/etc/rsync.password"/> #启动验证,填写验证的用户名及密码存放文件
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>

#在confxml.xml是默认的配置文件,只需修改上面有注释的三行。上面只是配置文件中的一部分。


vim /etc/init.d/sersync   #sersync启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#! /bin/bash  
#chkconfig: 35 10 90
#description: 监控目标目录[事件触发]同步到备份机
#  
/etc/rc.d/init.d/functions
case "$1" in
       start)
       cd /usr/local/sersync
       ./sersync2 -r -d
        if [ $? -eq 0 ]
                then
                echo -e "Staring sersyncd      [  OK  ]" 
                exit 0
        fi
        ;;
        stop)
        kill `ps aux | grep sersync2 | grep -v grep awk '{print $2}'`
        if [ $? -eq 0 ]
               then
                echo -e "Stopping sersyncd     [  OK  ]" 
                exit 0
        fi
        ;;
       status)
        ps aux | grep sersync2 | grep -v grep
        ;;
esac

启动sersync

[root@rsyncClient sersync]# /etc/init.d/sersync start

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
daemon thread num: 10
parse xml config file
host ip : localhosthost port: 8008
daemon start,sersync run behind the console 
use rsync password-file :
user isroot
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: 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 /data && rsync -artuz -R --delete ./ root@192.168.10.175::data --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data
Staring sersyncd      [  OK  ]


sersync参数介绍

[root@rsyncClient sersync]# ./sersync2 -help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________

confxml.xml配置文件详细介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="ISO-8859-1"?> 
<head version="2.5"
    <host hostip="localhost" port="8008"></host>    #针对插件保留的,默认配置即可
    <debug start="false"/>  #开户debug信息,会在sersync当前运行台,打印Debug信息
    <fileSystem xfs="false"/>   #是否支持xfs文件系统
    <filter start="false">      # 是否开户文件过滤,可以在下面添加过滤类型
    <exclude expression="(.*)\.svn"></exclude
    <exclude expression="(.*)\.gz"></exclude
    <exclude expression="^info/*"></exclude
    <exclude expression="^static/*"></exclude
    </filter
    <inotify>                   # inotify监控的事件
    <delete start="false"/>    #是否保持Sersync和同步端两端文件完全一致
    <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">   #这里定义要监控的本地目录,这个很重要
        <remote ip="192.168.10.175" name="data"/>   #要同步到哪台服务器及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="true" users="syncuser" passwordfile="/etc/rsyncd/rsyncd.pass"/> 
        <userDefinedPort start="false" port="874"/><!-- port=874 -->  # 定义rsync端口
        <timeout start="false" time="100"/><!-- timeout=100 -->  #定义传输超时时间
        <ssh start="false"/>        #Rsync的时候,是否使用ssh加密
    </rsync
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  
#对于失败的传输,会进行重新传送,再次失败就会写入rsync_fail_log,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径。
    <crontab start="false" schedule="600"><!--600mins-->   #定义crontab定期完全同步两端文件
        <crontabfilter start="false">            #crontab同步时候的过滤条件,上面的过滤部开头要开
        <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>


   

本文转自 irow10 51CTO博客,原文链接:http://blog.51cto.com/irow10/1836560,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值