SERSYNC实时同步

感谢金山,受益于开源,不忘奉献开源,以下是使用金山 sersync 在各分布式站点间进行实时同步的一例。
sersync使用 inotify 监控操作系统对磁盘的操作事件,通过接收到的事件生成带特定参数的rsync命令行并执行,从而达到实时增量同步的目的。
在使用pureftp做ftp服务器时,用flashfxp客户端向ftp服务器上传一个文件会依次触发以下几个主要事件:
createFile :pureftp生成临时文件pureftpd-rename.xxxx.xxxxxxxx
openFile:ftp服务器端以可写方式打开pureftpd-rename.xxxx.xxxxxxxx
Modify:向临时文件写入数据,在文件上传没有完成前会触发大量此事件
closeWrite:传输完毕,服务器关闭可写文件句柄触发该事件。
moveFrom:.pureftpd-rename.xxxx.xxxxxxxx,应该是从目录数据区删除文件相关信息
moveTo:<实际文件名>,应该是在目录数据区设置新的文件相关信息。

经过分析,在通常的应用中只需对 createFolder、closeWrite、moveFrom、moveTo四个事件进行监控。对于大多数可能对文件产生修改的操作,都要以可写的方式打开,在文件句柄关闭时,必然会产生可写文件关闭(closeWrite)事件,固不需要对其他文件操作事件进行处理,即可实现增量实时同步。

详细配置如下:

1.配置远端rsync(接收方)
编辑修改/etc/rsync.conf:
secrets file = /etc/rsyncd.secrets
#指定身份验证的用户密码文件
[pppei]
#同步模块名
uid = www
#rsync进程所属用户
gid = www
#rsync进程所属组
path = /var/www/
#模块所对应目录
comment = sync with www_root
#描述信息
list = no
#是否允许显示文件列表
auth users = pppei
#验证用户
read only = no
#是否为只读,因为要写数据所以改为否
hosts allow=xx.xx.xx.xx/32
#允许使用此模块的地址范围

创建用户密码文件:
touch /etc/rsyncd.secrets <    >pppei:123456
>EOF
chmod 600 /etc/rsyncd.secrets

在rc.local中加入如下内容使rsync开机可以自启动:
rsync –daemon –config /etc/rsyncd.conf

2.配置sersync(推送方)

解压sersync到/usr/sersync/。

修改sersync配置文件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=”(.*)\.php”></exclude>
        <exclude expression=”(.*)\.html”></exclude>
        <exclude expression=”(.*)\.htm”></exclude>
        <exclude expression=”^tmp/*”></exclude>
        <!—监控事件的过程中过滤特定文件,和特定文件夹的文件 –>
    </filter>
    <inotify>
        <delete start=”true”/>
        <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=”/var/www”>
            <!—设置要监控的目录 –>
            <remote ip=”xx.xx.xx.xx” name=”pppei”/>
            <!—指定远端rsync服务器的地址和模块名 –>
        </localpath>
        <rsync>
            <commonParams params=”-artuz”/>
            <auth start=”true” users=”pppei” passwordfile=”/usr/sersync/rsync.pas”/>
            <!—是否启用验证,并指定密码存放文件 –>
            <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–>
        <crontab start=”true” schedule=”1440″><!–600mins–>
            <!—是否启用执行完整rsync,并指定执行周期 –>
            <crontabfilter start=”true”>
                <!—设置完整执行rsync时的过滤条件 –>
                <exclude expression=”*.php”></exclude>
                <exclude expression=”*.html”></exclude>
                <exclude expression=”*.htm”></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/images”/>
        </localpath>
    </plugin>
</head>

创建密码文件:
touch /usr/sersync/rsync.pas <    >123456
>EOF
chmod 600 /usr/sersync/rsync.pas

在rc.local中加入如下内容使sersync开机可以自启动:
/usr/sersync/sersync2 -d -o /usr/sersync/confxml.xml

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据