漫天飞雪

实时同步sersync实战

实时同步sersync实战

什么是实时同步

实时同步是一种只要当前目录发生变化则会触发一个事件,事件触发后会将变化的目录同步至远程服务器。

sersync和rsync+inotify对比

提到数据同步就必然会谈到rsync,一般简单的服务器数据传输会使用ftp/sftp等方式,但是这样的方式效率不高,不支持差异化增量同步也不支持实时传输。针对数据实时同步需求大多数人会选择rsync+inotify-tools的解决方案,但是这样的方案也存在一些缺陷(文章中会具体指出),sersync是国人基于前两者开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。

项目地址

sersync项目实战

1)环境准备

角色 外网IP(NAT) 内网IP(LAN) 安装工具
web01 eth0:10.0.0.7 eth1:172.16.1.7 部署代码(提交作业)
nfs-server eth0:10.0.0.31 eth1:172.16.1.31 rsync+inotify+sersync
backup eth0:10.0.0.41 eth1:172.16.1.41 rsync-server

1.实时同步哪台服务器的目录,那么就在哪台服务器上安装sersync

2.只要安装sersync 就必须安装rsyncinotify

安装rsync的服务端(backup)

1)安装rsync服务

[root@backup ~]# yum install -y rsync

2)配置文件

[root@backup ~]# vim /etc/rsyncd.conf 
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[zls]
comment = welcome to oldboyedu backup!
path = /backup

[nfs]
comment = welcome to oldboyedu backup!
path = /data

3)创建用户

[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

4)创建目录并授权

[root@backup ~]# mkdir /data  /backup
[root@backup ~]# chown -R www.www /data/ /backup/

5)创建虚拟用户的密码文件并授权

[root@backup ~]# echo 'rsync_backup:123' > /etc/rsync.passwd 
chmod 600 /etc/rsync.passwd

6)启动rsync服务

[root@backup ~]# systemctl start rsyncd

NFS服务端部署sersync

1)安装sersync需要依赖rsyncinotify

[root@nfs ~]# yum install -y rsync inotify-tools

2)下载sersync

[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

3)部署sersync

​ 源码包:解压 生成 编译 安装

​ 解压:

[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

4)移动并改名

[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync

5)编辑配置文件

[root@nfs ~]# vim /usr/local/sersync/confxml.xml
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>
-----------------------------------------------------------------------------------------
    <sersync>
        #监控的目录,改成/data
        <localpath watch="/opt/tongbu">
            #推送的IP(backup服务的IP)172.16.1.41 ,name是模块名
            <remote ip="127.0.0.1" name="tongbu1"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            #执行rsync的参数改成 -az
            <commonParams params="-artuz"/>
            #虚拟用户的用户名和密码文件,开启认证start=true  rsync_backup    /etc/rsync.pass
            <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            #设置超时时间
            <timeout start="true" 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>


#完整配置文件
[root@nfs ~]# cat /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="true"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="true"/>
	<modify start="true"/>
    </inotify>

    <sersync>
	<localpath watch="/data">
	    <remote ip="172.16.1.41" name="nfs"/>
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-az"/>
	    <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    <timeout start="true" 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>

6)创建虚拟用户的密码文件,并授权

[root@nfs sersync]# echo '123' > /etc/rsync.pass
[root@nfs sersync]# chmod 600 /etc/rsync.pass

7)查看帮助

[root@nfs sersync]# /usr/local/sersync/sersync2 -h
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参数,则默认执行同步程序
________________________________________________________________

8)启动sersync

[root@nfs data]# /usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml

------------------------------------------------rsync服务端--------------------------------------------------

1)安装

[root@backup ~]# yum install -y rsync

2)改配置

[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[zls]
comment = welcome to oldboyedu backup!
path = /backup

3)创建系统用户

[root@backup ~]# useradd rsync -s /sbin/nologin  -M

4)创建虚拟用户及密码文件并授权

[root@backup ~]# echo 'rsync_backup:123' > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd

5)创建目录并授权

[root@backup ~]# mkdir /backup
[root@backup ~]# chown -R rsync.rsync /backup

6)启动服务并加入开机自启

[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd

----------------------------------------------------------rsync的客户端(nfs)------------------------------------------------------------------

1)安装sersync(rsync+inotify)

[root@nfs ~]# yum install -y rsync inotify-tools

2)安装sersync

下载:

[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

解压:

[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

移动并改名:

[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync

3)修改配置文件

[root@nfs ~]# vim /usr/local/sersync/confxml.xml
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>

    <sersync>
        <localpath watch="/zls">
            <remote ip="172.16.1.41" name="zls"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-az"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pas"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>

4)创建目录

[root@nfs ~]# mkdir /zls

5)创建密码文件并授权

[root@nfs ~]# echo '123' > /etc/rsync.pas
[root@nfs ~]# chmod 600 /etc/rsync.pas

6)启动sersync

[root@nfs ~]# /usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml

sersync 就是rsync的客户端

底层调用:rsync和inotify

posted @ 2019-08-07 20:40  1naonao  阅读(390)  评论(0编辑  收藏  举报