一.簡介
sersync是基於Inotify開發的,類似於Inotify-tools的工具;
sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或某一個目錄的名字;
rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來說是很小的,rsync在遍歷查找比對文件時,速度很快),因此,效率很高;
注:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件很多時,建議使用Rsync+sersync。
二.具體操作
操作系統:centos6.6
源服務器(sersync):192.168.0.1 --> 目標服務器(rsync):192.168.0.2
目的:使用root用戶,把源服務器上的/test1目錄下的文件實時同步到目標服務器的/test2目錄下
(一)目標服務器(rsync):192.168.0.2
1、安裝rsync
yum -y install rsync
2、創建rsyncd.conf配置文件
vi /etc/rsyncd.conf
--------------------------
uid = root
gid = root
max connections = 10
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
[test] #模塊自定義名稱 ---這個需要與源服務器Sersync對應
path = /test2 #需要同步到這個目錄
comment = Mirror to test
ignore errors = yes
use chroot = no #默認為true,修改為no,增加對目錄文件軟連接的備份
read only = no #設置rsync服務端文件為讀寫權限
hosts allow = 192.168.0.1 #允許訪問源服務器IP
hosts deny = 0.0.0.0/32 #拒絕所有IP連接,先允許后拒絕
exclude =/data1 /data2 不同步的目錄
注:這里只是列出一些配置項而已,其他在配置時按需選擇。
------------------------------
3、啟動服務
/usr/bin/rsync --daemon /etc/rsyncd.conf
(二)源服務器(sersync):192.168.0.1
1、安裝rsync
yum -y install rsync
2、同步數據
rsync -avzP /test1/ root@192.168.0.2::test/ 測試同步文件
--/test1/ 同步目錄
--192.168.0.2 目的服務器,安裝並配置完畢rsync
--test 模塊自定義名稱,即目的服務器的rsync配置文件中配置的
3、安裝sersync工具,實時觸發rsync進行同步
#cd /usr/local/src
#wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
#tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
#mv /usr/local/src/GNU-Linux-x86/ /usr/local/sersync/
#vi /usr/local/sersync/confxml.xml
需要修改如下幾個地方:
<localpath watch="/data/test">
<!—-設置監控的目錄-->
<remote ip="192.168.0.2" name="test"/>
<!—設置從服務器的IP-->
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<!—-設置監控的目錄-->
<remote ip="192.168.0.2" name="test"/>
<!—設置從服務器的IP-->
<!--<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="root" passwordfile="/etc/rsyncd.secret"/>
<!—-設置同步的用戶名和密碼文件-->
<userDefinedPort start="true" port="873"/><!-- port=874 -->
<!—-設置rsync的端口,要和從那邊開啟的端口一致-->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<commonParams params="-artuz"/>
<auth start="true" users="root" passwordfile="/etc/rsyncd.secret"/>
<!—-設置同步的用戶名和密碼文件-->
<userDefinedPort start="true" port="873"/><!-- port=874 -->
<!—-設置rsync的端口,要和從那邊開啟的端口一致-->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
注:如果目的服務器的rsync有配置賬號密碼時,這里需要配置/etc/rsyncd.secret
4、啟動sersync
/usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml