CentOS7+rsync+sersync實現數據實時同步

1、爲何要用Rsync+sersync架構?

一、sersync是基於Inotify開發的,相似於Inotify-tools的工具
二、sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字,而後使用rsync同步的時候,只同步發生變化的這個文件或者這個目錄。

2、Rsync+Inotify-tools與Rsync+sersync這兩種架構有什麼區別?

一、Rsync+Inotify-tools
(1):Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增長、刪除、修改),並無把具體是哪一個文件或者哪一個目錄發生了變化記錄下來;
(2):rsync在同步的時候,並不知道具體是哪一個文件或者哪一個目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步很是耗時(rsync要對整個目錄遍歷查找對比文件),所以,效率很低。

二、Rsync+sersync
(1):sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字;
(2):rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來講是很小的,rsync在遍歷查找比對文件時,速度很快),所以,效率很高。

小結:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件不少時,建議使用Rsync+sersync。


準備
操做系統 CentOS 7
sersync2.5.4_64bit
源服務器 192.168.0.248
目標服務器 192.168.0.249
把A機器上的一個目錄下文件的變化實時同步到B機器上,兩邊文件保持一致;
兩臺服務器的selinux與iptables自行設置好,rsync的端口兩邊要配置一致,默認是873


PS:本文中附上了目標服務器與源服務器的一鍵部署腳本(親測可用),若有須要可直接運行腳本進行環境部署,部署完成後只需修改你須要的參數便可使用(install_des.sh目標服務器腳本,install_source.sh源服務器腳本)

操做
目標服務器


1.安裝rsync服務與xinetd服務php

  • yum install -y rsync xinetd


2.rsync配置(千萬注意:本人親自入坑,下面的配置信息,請在添加到配置文件中後千萬記得刪除掉後面#部分的備註信息,不然同步過程會失敗,不知緣由,刪除後便可正常同步,超級大坑...測試了一個早上才找到緣由
 linux

  • vim /etc/rsyncd.conf
  •  
  • uid = root  #設置rsync的運行用戶權限爲root
  • gid = root  #設置rsync的運行組權限爲root
  • use chroot =no
  • list = no  #不顯示rsync服務端資源列表
  • log file = /var/log/rsyncd.log  #配置rsync日誌文件存放路徑
  • [data  #自定義模塊名稱
  • path=/data  #定義須要實時同步的路徑
  • comment=data  #與模塊名稱相同便可
  • ignore errors
  • read only=no  #設置rsync服務端文件爲讀寫權限
  • list=no  #不顯示rsync服務端資源列表
  • max connections=200  #容許最大的客戶端鏈接數
  • timeout=600  #設置超時時間
  • auth users=rsync  #執行數據同步的虛擬用戶名,能夠設置多個,用逗號隔開
  • secrets file=/etc/rsync.pas  #設置同步使用的用戶及密碼信息文件存放路徑
  • hosts allow=*  #容許全部用戶訪問
  • #host deny=0.0.0.0/0  #用於設置拒絕訪問的IP,這裏註銷便可


3.建立用於存放同步的用戶及密碼的文件/etc/rsync.pas,並將此文件權限修改成600
 git

  • echo "rsync:123456lzz" >> /etc/rsync.pas
  • chmod 600 /etc/rsync.pas


4.建立同步的目錄/data
 github

  • mkdir /data


5.啓動rsync與xinetd服務
 正則表達式

  • systemctl start xinetd  #啓動xinetd服務
  • systemctl enable xinetd  #將xinetd服務加入開機項
  • rsync --daemon --config=/etc/rsyncd.conf  #加載配置文件rsyncd.conf啓動rsync服務


6.配置rsync開機自啓動
 express

  • echo "rsync --daemon --config=/etc/rsyncd.conf"  >> /etc/rc.d/rc.local




源服務器vim

一、查看服務器內核是否支持inotifybash

ll /proc/sys/fs/inotify   #列出文件目錄,出現下面的內容,說明服務器內核支持inotify服務器

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_queued_events架構

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_instances

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_watches

備註:Linux下支持inotify的內核最小爲2.6.13,能夠輸入命令:uname -a查看內核

CentOS 5.X 內核爲2.6.18,默認已經支持inotify

二、修改inotify默認參數(inotify默認內核參數值過小)

查看系統默認參數值:

sysctl -a | grep max_queued_events

結果是:fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches

結果是:fs.inotify.max_user_watches = 8192

sysctl -a | grep max_user_instances

結果是:fs.inotify.max_user_instances = 128

修改參數:

sysctl -w fs.inotify.max_queued_events="99999999"

sysctl -w fs.inotify.max_user_watches="99999999"

sysctl -w fs.inotify.max_user_instances="65535"

vi /etc/sysctl.conf #添加如下代碼

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535

:wq! #保存退出

參數說明:

max_queued_events:

inotify隊列最大長度,若是值過小,會出現"** Event Queue Overflow **"錯誤,致使監控文件不許確

max_user_watches:

要同步的文件包含多少目錄,能夠用:find /home/www.osyunwei.com -type d | wc -l 統計,必須保證max_user_watches值大於統計結果(這裏/home/www.osyunwei.com爲同步文件目錄)

max_user_instances:

每一個用戶建立inotify實例最大值

 


1.下載sersync源碼包並安裝sersync服務

  • wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
  • mkdir /usr/local/sersync
  • mkdir /usr/local/sersync/conf
  • mkdir /usr/local/sersync/bin
  • mkdir /usr/local/sersync/log
  • tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
  • cd GNU-Linux-x86/
  • cp confxml.xml /usr/local/sersync/conf
  • cp sersync2 /usr/local/sersync/bin


2.建立密碼文件跟目標服務器同樣,不過這個文件只要保存密碼便可,不須要保存用戶名,權限也是600
 

  • echo "123456lzz" >> /etc/rsync.pas
  • chmod 600 /etc/rsync.pas


3.配置sersync服務
 

  • vim /usr/local/sersync/conf/confxml.xml
  • <?xml version="1.0" encoding="ISO-8859-1"?>
  •    <head version="2.5">
  •    <!--設置本地IP和端口-->
  •    <host hostip="localhost" port="8008"></host>
  •    <!--開啓DUBUG模式-->
  •    <debug start="false"/>
  •    <!--開啓xfs文件系統-->
  •    <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">
  •    <!--遠程IPrsync模塊名-->
  •            <remote ip="192.168.0.249" name="data"/>  
  •            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
  •            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
  •        </localpath>
  •        <rsync>
  •    <!--rsync指令參數-->
  •            <commonParams params="-auvzP"/>
  •    <!--rsync同步認證-->
  •            <auth start="true" users="rsync" passwordfile="/etc/rsync.pas"/>
  •    <!--設置rsync遠程服務端口,遠程非默認端口則需打開自定義-->
  •            <userDefinedPort start="false" port="873"/><!-- port=874 -->
  •    <!--設置超時時間-->
  •            <timeout start="true" time="100"/><!-- timeout=100 -->
  •    <!--設置rsync+ssh加密傳輸模式,默認關閉,開啓需設置SSH加密證書-->
  •            <ssh start="false"/>
  •        </rsync>
  •     <!--sersync傳輸失敗日誌腳本路徑,每隔60會從新執行該腳本,執行完畢會自動清空-->
  •        <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
  •     <!--設置rsync+crontab定時傳輸,默認關閉-->
  •        <crontab start="false" schedule="600"><!--600mins-->
  •            <crontabfilter start="false">
  •                <exclude expression="*.php"></exclude>
  •                <exclude expression="info/*"></exclude>
  •            </crontabfilter>
  •        </crontab>
  •    <!--設置sersync傳輸後調用name指定的插件腳本,默認關閉-->
  •        <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="xxx"/>
  •            <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>


4.建立同步目錄
 

  • mkdir /data


5.設置環境變量
 

  • echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
  • source /etc/profile


6.啓動、重啓、中止sersync服務
 

  • sersync2 ---/usr/local/sersync/conf/confxml.xml  #啓動
  • killall sersync2 && sersync2 ---/usr/local/sersync/conf/confxml.xml  #重啓
  • killall sersync2  #中止


7.設置開機啓動sersync
 

  • echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.d/rc.local


最後啓動完源服務器上的sersync服務後重啓下目標服務器上的xinetd服務

vi sersync.sh

#!/bin/bash

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

chmod+x sersync.sh

將 sersync.sh加入 /etc/rc.local 開機啓動




測試:
在源服務器上的/data目錄新建文件或目錄再查看目標服務器上的/data目錄狀態

 

實例

 

vi /usr/local/sersync/conf/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/lin">
            <remote ip="192.168.30.171" name="data"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="lgsync" passwordfile="/etc/rsyncd.passwd"/>
            <userDefinedPort start="true" port="878"/><!-- 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>

 

 

 

參數說明:

localpath watch="/home/www.osyunwei.com":#源服務器同步目錄

192.168.21.127,192.168.21.128:#目標服務器IP地址

name="home_www.osyunwei.com": #目標服務器rsync同步目錄模塊名稱

users="home_www.osyunwei.com_user": #目標服務器rsync同步用戶名

passwordfile="/etc/passwd.txt": #目標服務器rsync同步用戶的密碼在源服務器的存放路徑

remote ip="192.168.21.127":  #目標服務器ip,每行一個

remote ip="192.168.21.128":  #目標服務器ip,每行一個

failLog path="/tmp/rsync_fail_log.sh"  #腳本運行失敗日誌記錄

start="true"  #設置爲true,每隔600分鐘執行一次全盤同步

五、設置sersync監控開機自動執行

vi /etc/rc.d/rc.local  #編輯,在最後添加一行

/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #設置開機自動運行腳本

:wq!  #保存退出

六、添加腳本監控sersync是否正常運行

vi  /home/crontab/check_sersync.sh  #編輯,添加如下代碼

#!/bin/sh

sersync="/usr/local/sersync/bin/sersync2"

confxml="/usr/local/sersync/conf/confxml.xml"

status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)

if [ $status -eq 0 ];

then

$sersync -d -r -o $confxml &

else

exit 0;

fi

:wq!  #保存退出

chmod +x /home/crontab/check_sersync.sh #添加腳本執行權限

vi /etc/crontab #編輯,在最後添加下面一行

*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1  #每隔5分鐘執行一次腳本

service crond reload  #從新加載服務

相關文章
相關標籤/搜索