Rsync+Sersync实时同步文件

时间:2022-07-24
本文章向大家介绍Rsync+Sersync实时同步文件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

实验环境

关闭firewalld和selinux防火墙
操作系统:CentOS7.3
sersync01:172.16.0.3
sersync02:172.16.0.9
sersyncN: 172.16.0.5

安装Rsync服务器|sersync01和sersync02安装

安装rsync软件

yum install rsync -y

启动rsync服务

systemctl start rsyncd.service

检查rsync服务状态

systemctl status rsyncd.service

配置/etc/rsyncd.conf 在文件末尾添加

vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = yes
max connections = 1024
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[hbgslz]
path = /tmp/hbgslz/
comment = ftp export area
read only = no
write only = no
list = yes

sersyncN服务器安装Rsync和sersync

安装Rsync软件,步骤和上面完全一样

yum install rsync -y
systemctl start rsyncd.service

下载sersync软件

wget https://www.endvv.com/wj/45/sersync2.5.4_64bit_binary_stable_final.tar.gz

解压sersync软件包

tar -zxf /tmp/sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/

将解压后的GNU-Linux-x86目录重新命名

mv /usr/local/GNU-Linux-x86/ /usr/local/sersync

给权限

chown -R 777 /usr/local/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="/opt/tongbu">
        <remote ip="172.16.0.3" name="hbgslz"/>
        <remote ip="172.16.0.9" name="hbgslz"/>
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync>
        <commonParams params="-artuz"/>
        <auth start="false" users="root" passwordfile="/etc/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="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>

启动

/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml &

验证

登录到sersyncN服务器,在/opt/tongbu/目录下新建文件1.txt

touch /opt/tongbu/1.txt

分别登录到sersync01、sersync02查看/tmp/hbgslz/目录下面是否存在1.txt文件