Sersync 配置和使用

sersync + inotify-tools + rsync 可以实现服务器文件实时同步。

环境

服务器A(SourA):主服务器/源文件服务器,该服务器的文件变动,会实时同步到B服务器。

  • 安装: rsync(daemon)

服务器B(DestB):备份服务器/目的文件服务器

  • 安装: rsync inotify-tools sersync

备份服务器B配置

安装 Rsync
# 使用 yum 或者 源码编译 安装,省略
配置 rsync,以 daemon 方式运行
# 具体参数和配置可以参照 《rsync的配置和使用》
[root@DestB rsync]# ls /etc/rsync
rsyncd.conf  rsyncd.secrets

[root@DestB rsync]# cat /etc/rsync/rsyncd.conf 
use chroot = no
hosts allow = SourA_ip
hosts deny = *
max connections = 4
pid file = /var/run/rsyncd.pid
log format = %t %a %m %f %b
log file = /var/log/rsync.log
exclude = ^settings/*     # 排除 settings 目录
transfer logging = yes
timeout = 900

[lfbak4nfs]
uid = www
gid = www
read only = no
path = /mydata/nfs/   # 要备份的目录
list = no
ignore errors = yes
auth users = satebak
secrets file = /etc/rsync/rsyncd.secrets

[root@DestB rsync]# cat /etc/rsync/rsyncd.secrets 
satebak:passwd@2017

[root@DestB rsync]# chmod 600 /etc/rsync/rsyncd.secrets 
启动
[root@DestB rsync]# rsync --daemon --config=/etc/rsync/rsyncd.conf

主服务器A配置

安装 Rsync
# 使用 yum 或者 源码编译 安装,省略
安装 inotify-tools
# 下载,编译安装
[root@SourA rsync]# cd /opt/
[root@SourA opt]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@SourA opt]# tar xvf inotify-tools-3.14.tar.gz
[root@SourA opt]# cd inotify-tools-3.14
[root@SourA inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@SourA inotify-tools-3.14]# make -j2 && make install

# 添加环境变量
[root@SourA inotify-tools-3.14]# cat /etc/profile.d/inotify.sh
export PATH=/usr/local/inotify/bin:$PATH
[root@SourA inotify-tools-3.14]# source /etc/profile
安装 sersync
# 下载
[root@SourA rsync]# cd /opt/
[root@SourA opt]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@SourA opt]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@SourA opt]# mv GNU-Linux-x86/ /usr/local/sersync/
配置 sersync

修改配置文件 /usr/local/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="false">
    <exclude expression="(.*)\.svn"></exclude>
    <exclude expression="(.*)\.gz"></exclude>
    <exclude expression="^info/*"></exclude>
    <exclude expression="^static/*"></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="/data/bak">   <!-- 定义要同步的文件目录-->
        <remote ip="DestB_ip" name="lfbak4nfs"/>  <!-- 定义B服务器的Ip和B服务器上rsync定义的模块名-->
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync>
        <commonParams params="-artuz"/> <!-- rsync 执行时使用的参数-->
        <auth start="true" users="satebak" passwordfile="/usr/local/sersync/user.pass"/>  <!-- rsync 执行时使用的用户名和密码文件-->
        <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="false" schedule="600"><!--600mins-->
        <crontabfilter start="false">
        <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>

添加密码文件

[root@SourA sersync]# echo 'passwd@2017' > /usr/local/sersync/user.pass
[root@SourA sersync]# chmod 600 /usr/local/sersync/user.pass
运行
[root@SourA sersync]# nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml > /usr/local/sersync/rsync.log 2>&1 &

-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n:指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用confxml.xml文件
 

 
 
 
 
 



posted @ 2021-05-20 11:01  woaibaobei  阅读(442)  评论(0编辑  收藏  举报