CentOS6.8使用Rsync+sersync實現數據實時同步

Sersync簡介php

        Sersync利用inotify與rsync對服務器進行實時同步,其中inotify用於監控文件系統事件,rsync是目前普遍使用的同步算法,其優勢是隻對文件不一樣的部分進行操做,因此其優點大大超過使用掛接文件系統的方式進行鏡像同步。由金山的周洋開發完成,是目前使用較多的文件同步工具之一。該工具和其餘的工具相比有以下優勢:c++

  • sersync是使用c++編寫,因爲只同步發生更改的文件,所以比其餘同步工具更節約時間、帶寬;算法

  • 安裝方便、配置簡單;express

  • 使用多線程進行同步,可以保證多個服務器實時保持同步狀態;vim

  • 自帶出錯處理機制,經過失敗隊列對出錯的文件從新出錯,若是仍舊失敗,則每10個小時對同步失敗的文件從新同步;bash

  • 自帶crontab功能,只需在xml配置文件中開啓,便可按您的要求,隔一段時間總體同步一次;服務器

  • 自帶socket與http協議擴展,你能夠方便的進行二次開發;多線程


rsync+sersync與rsync+Inotify-tools區別app

Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增長、刪除、修改),並無把具體是哪一個文件或者哪一個目錄發生了變化記錄下來;dom

rsync在同步的時候,並不知道具體是哪一個文件或者哪一個目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步很是耗時(rsync要對整個目錄遍歷查找對比文件),所以,效率很低。

sersync是基於Inotify開發的,相似於Inotify-tools的工具;

sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字;

rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來講是很小的,rsync在遍歷查找比對文件時,速度很快),所以,效率很高。

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


地址:http://code.google.com/p/sersync/,要***才能下載

            http://download.csdn.net/detail/hellopengyl/9918625  


安裝、配置

和Inotify-tools同樣,只須要在數據源端安裝


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

[root@localhost src]# ll /proc/sys/fs/inotify    #查看服務器內核是否支持inotify,出現下面的內容,說明服務器內核支持inotify
total 0
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_queued_events
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_instances
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_watches
[root@localhost src]# uname -r        #Linux下支持inotify的內核最小爲2.6.13
2.6.32-642.el6.x86_64
[root@localhost src]# sysctl -a|egrep -i "max_queued_events|max_user_watches|max_user_instances"    #修改inotify默認參數(inotify默認內核參數值過小)
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 8192
fs.inotify.max_queued_events = 16384
fs.epoll.max_user_watches = 201420
[root@localhost src]# vim /etc/sysctl.conf
fs.inotify.max_user_instances = 65535
fs.inotify.max_user_watches = 99999999
fs.inotify.max_queued_events = 99999999    
[root@localhost src]# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}
65535
99999999
99999999
[root@localhost src]#

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

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

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


二、安裝、配置sersync

[root@localhost src]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@localhost src]# mv GNU-Linux-x86 /app/sersync
[root@localhost src]# cd /app/sersync/
[root@localhost sersync]# ls
confxml.xml  sersync2
[root@localhost sersync]# cp confxml.xml{,.default}
[root@localhost sersync]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>    #針對插件的保留字段,保留默認便可。
    <debug start="true"/>    #在sersync正在運行的控制檯,打印inotify,rsync同步命令
    <fileSystem xfs="false"/>    #對於xfs文件系統用戶,須要將這個選項開啓才正常工做
    <filter start="false">    #過濾系統的臨時文件,被過濾的文件不會被監控提升,默認過濾系統的臨時文件(以「.」開頭,以「~」結尾)
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>    #inotify監控文件模塊
	<delete start="true"/>    #若是本地文件刪除,不須要刪除遠程段的文件能夠設置成false
	<createFolder start="true"/> #若是將createFolder設爲false,則不會對產生的目錄進行監控,該目錄下的子文件與子目錄也不會被監控;
	<createFile start="false"/>  #把createFile(監控文件事件選項)設置爲false來提升性能,減小rsync通信;由於拷貝文件到監控目錄會產生create事件與close_write事件,因此若是關閉create事件,只監控文件拷貝結束時的時間close_write,一樣能夠實現文件完整同步;
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="false"/>
	<modify start="false"/>
    </inotify>

    <sersync>    #進行數據同步的模塊
	<localpath watch="/app/rsync_client">    #定義本地要同步的目錄
	    <remote ip="10.15.43.100" name="app_rsync_server"/>#遠程接受同步的IP和rsync模塊名
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-artuz"/>    #自定義rsync參數,默認是-artuz
	    <auth start="true" users="rsync" passwordfile="/etc/rsyncd.secret"/>    #開啓用戶認證,定義用戶名密碼
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>    #設置rsync+ssh加密傳輸模式,默認關閉,開啓需設置SSH加密證書
	</rsync>
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  #對於失敗的傳輸,會進行從新傳送,再次同步失敗後日志記錄到/tmp/rsync_fail_log.sh,而且每60分鐘對失敗的log進行從新同步
	<crontab start="true" schedule="600"><!--600mins-->    #每隔600s會作一次徹底同步
	    <crontabfilter start="false">    #若是開啓了filter文件過濾功能,那麼crontab總體同步也須要設置過濾,不然雖然實時同步的時候文件被過濾了,但crontab總體同步的時候 若是不單獨設置crontabfilter,還會將需過濾的文件同步到遠程,
		<exclude expression="*.php"></exclude> #crontab的過濾正則與filter過濾的不一樣,果同時開啓了filter與crontab,則要開啓crontab的crontabfilter,並按示例設置使其與filter的過濾一一對應。
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
    	<plugin start="false" name="command"/>    #當設置爲true的時候,將文件同步到遠程服務器後會調用name參數指定的插件。
    </sersync>

    <plugin name="command">    #name指定的插件
    #當文件同步完成後,會調用command插件,例如同步文件是file.txt,file.txt文件在改動以後,調用rsync同步到遠程服務器後,調用command插件,執行/bin/sh file.txt suffix >/dev/null 2>&1
    #若是suffix 設置了,則會放在inotify事件file.txt以後,若是ignoreError爲true,則會添加>/dev/null 2>&1
	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>    #當filter爲ture,include能夠只對正則匹配到的文件,調用command。
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>
        #http插件,能夠向指定域名的主機post,inotify監控的事件。
        #socket插件,開啓該模塊,則向指定ip與端口發送inotify所產生的文件路徑信息
    <plugin name="socket">    
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    #在同步過程當中將文件發送到目的服務器後刷新cdn接口。若是不想使用,則設置<plugin start="false" name="refreshCDN"/>
    #該模塊根據chinaCDN的協議,進行設計,當有文件產生的時候,就向cdn接口發送須要刷新的路徑位置
    <plugin name="refreshCDN">    
	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">  #須要監控的目錄
	        #cdnifo標籤制定了cdn接口的域名,端口號,以及用戶名與密碼。
	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
	    #sendurl標籤是須要刷新的url的前綴
	    <sendurl base="
	    #regexurl標籤中的,regex屬性爲true時候,使用match屬性的正則語句匹配inotify返回的路徑信息,並將正則匹配到的部分做爲url一部分,
	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
	        #若是產生文件事件爲:/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/image/a/123.txt
	        #通過上面的match正則匹配後,最後刷新的路徑是:http://pic.xoyo.com/cms/jx3/a/123.txt;
	        #若是regex屬性爲false,最後刷新的路徑是http://pic.xoyo.com/cms/jx3.xoyo.com/images/a/123.txt;
	</localpath>
    </plugin>
</head>
[root@localhost sersync]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/confxml.xml
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
option: -d 	run as a daemon
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -n 	thread num is:  8
option: -o 	config xml name:  /app/sersync/confxml.xml
parse xml config file
host ip : localhost	host port: 8008
Open debug, you will see debug infomation 
daemon start,sersync run behind the console 
Start the crontab 	Every 600 minutes rsync all the files to the remote servers entirely
use rsync password-file :
user is	rsync
passwordfile is 	/etc/rsyncd.secret
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 10  = 1(primary thread) + 1(fail retry thread) + 8(daemon sub threads) 
Max threads numbers is: 18 = 10(Thread pool nums) + 8(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /app/rsync_client && rsync -artuz -R --delete ./ rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
crontab command:cd /app/rsync_client && rsync -artuz -R --delete ./ rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
run the sersync: 
watch path is: /app/rsync_client
add watch: /app/rsync_client return wd is: 1
add watch: /app/rsync_client/test return wd is: 2
[root@localhost sersync]#

-d     後臺啓動 

-r    同步前將已存在的文件所有同步過去,若是設置了過濾器,即在xml文件中,filter爲true,則暫時不能使用-r參數進行總體同步;

-n    開啓的線程總數默認10 

-o    指定配置文件,指定 -o 參數能夠指定多個不一樣的配置文件,從而實現sersync多進程多實例的數據同步

-m    不進行同步,只運行插件 ./sersync -m pluginName 例如:./sersync -m command,則在監控到事件後,不對遠程目標服務器進行同步,而是直接運行command插件


[root@localhost sersync]# cat monitor_sersync.sh #監控Sersync運行狀態的腳本,若是服務中止了就重啓服務
#!/bin/bash
server_file="/app/sersync/sersync2"
conf_file="/app/sersync/confxml.xml"
options="-d -r -n 8 -o"
proc_num=$(ps -ef|grep -i sersync2|grep -v "grep"|wc -l)
if [ $proc_num -lt 1 ];then
    cd $(dirname $server_file)
    nohup $server_file $options $conf_file &
else
   exit 0;
fi
[root@localhost sersync]# chmod +x monitor_sersync.sh 
[root@localhost sersync]# crontab -l
*/5 * * * *  /app/sersync/monitor_sersync.sh
[root@localhost sersync]# /etc/init.d/crond restart
[root@localhost sersync]# vim /etc/rc.d/rc.local    #設置開機自動運行腳本
/app/sersync/sersync2 -d -r -n 8 -o /app/sersync/confxml.xml
[root@localhost sersync]#


測試

在客戶端監控的目錄/app/rsync_client建立文件,而後查看服務器端app_rsync_server模塊對應的目錄是否同步更新

[root@localhost rsync_client]# touch file{1..9}
inotify wd:1	name:file1	mask:256
inotify wd:1	name:file1	mask:8
inotify wd:1	name:file2	mask:256
inotify wd:1	name:file2	mask:8
inotify wd:1	name:file3	mask:256
inotify wd:1	name:file3	mask:8
inotify wd:1	name:file4	mask:256
inotify wd:1	name:file4	mask:8
inotify wd:1	name:file5	mask:256
[root@localhost rsync_client]# cd /app/rsync_client && rsync -artuz -R "./file2" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
inotify wd:1	name:file5	mask:8
inotify wd:1	name:file6	mask:256
inotify wd:1	name:file6	mask:8
inotify wd:1	name:file7	mask:256
inotify wd:1	name:file7	mask:8
inotify wd:1	name:file8	mask:256
inotify wd:1	name:file8	mask:8
inotify wd:1	name:file9	mask:256
inotify wd:1	name:file9	mask:8
cd /app/rsync_client && rsync -artuz -R "./file1" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file3" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file4" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file5" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file6" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file7" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file8" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file9" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
[root@localhost rsync_client]# touch file1
inotify wd:1	name:file1	mask:8
[root@localhost rsync_client]# cd /app/rsync_client && rsync -artuz -R "./file1" rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret

[root@localhost sersync]#

壓測

寫入10K個文件批量同步測試結果:

[root@localhost rsync_client]# for n in {1..10000};do dd if=/dev/zero of=/app/rsync_client/"$n".txt bs=1M count=5;done

查看同步速度,當10K個文件同步完後,在/app/rsync_server裏發現才同步了600多個文件


多實例狀況

配置多個confxml.xml文件(好比:www、bbs、blog....等等),根據不一樣的需求同步對應的實例文件

[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/www_confxml.xml
[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/bbs_confxml.xml
[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/blog_confxml.xml


rsync使用時的常見問題

錯誤1: rsync: read error: Connection reset by peer (104) 

rsync error: error in rsync protocol data stream (code 12) at io.c(794) [receiver=3.0.2] 

解決:很大多是服務器端沒有開啓 rsync 服務。開啓服務。 或者開啓了防火牆指定的端口沒法訪問。 


錯誤2:@ERROR: chdir failed 

rsync error: error starting client-server protocol (code 5) at main.c(1495) [receiver=3.0.2] 

解決:服務器端同步目錄沒有權限,cwrsync默認用戶是Svcwrsync。爲同步目錄添加用戶Svcwrsync權限。 


錯誤3:@ERROR: failed to open lock file 

rsync error: error starting client-server protocol (code 5) at main.c(1495) [receiver=3.0.2] 

解決:服務器端配置文件 rsyncd.conf中添加 lock file = rsyncd.lock 便可解決。 


錯誤4:@ERROR: invalid uid nobody 

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.2] 

解決:在rsyncd.conf文件中添加下面兩行便可解決問題 

UID = 0 

GID = 0 


錯誤5:@ERROR: auth failed on module test2 

rsync error: error starting client-server protocol (code 5) at main.c(1296) [receiver=3.0.2] 

解決:服務端沒有指定正確的secrets file,請在 [test2]配置段添加以下配置行: 

auth users = coldstar #同步使用的賬號 

secrets file = rsyncd.secrets #密碼文件 


錯誤6:password file must not be other-accessible 

解決:客戶端的pass文件要求權限爲600, chmod 600 /etc/rsync.pass 便可。 


錯誤7:rsync: chdir /cygdrive/c/work failed

: No such file or directory (2)

解決:服務器端同步文件夾路徑或名稱寫錯了,檢查path。


錯誤8:rsyncserver 服務啓動時報錯「rsyncserver服務啓動後又中止了。一些服務自動中止,若是它們沒有什麼可作的,例如「性能日誌和警報」服務。」

解決方法:將安裝目錄下的rsyncd.pid文件刪除,再從新啓動RsyncServer服務。通常是異常關機致使的。

相關文章
相關標籤/搜索