實時同步sersync實戰

實時同步sersync實戰

什麼是實時同步

實時同步是一種只要當前目錄發生變化則會觸發一個事件,事件觸發後會將變化的目錄同步至遠程服務器。html

sersync和rsync+inotify對比

提到數據同步就必然會談到rsync,通常簡單的服務器數據傳輸會使用ftp/sftp等方式,可是這樣的方式效率不高,不支持差別化增量同步也不支持實時傳輸。針對數據實時同步需求大多數人會選擇rsync+inotify-tools的解決方案,可是這樣的方案也存在一些缺陷(文章中會具體指出),sersync是國人基於前二者開發的工具,不只保留了優勢同時還強化了實時監控,文件過濾,簡化配置等功能,幫助用戶提升運行效率,節省時間和網絡資源。git

項目地址github

sersync項目實戰

1)環境準備web

角色 外網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.實時同步哪臺服務器的目錄,那麼就在哪臺服務器上安裝sersyncexpress

2.只要安裝sersync 就必須安裝rsyncinotifyvim

安裝rsync的服務端(backup)

1)安裝rsync服務bash

[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

相關文章
相關標籤/搜索