sersync下載安裝及配置(有演示示例)
sersync是基於inotify+rsync的大量文件的多服務器自動同步程序
使用 Linux 2.6 內核的 inotify 監控 Linux 文件系統事件,被監聽目錄下如果有文件發生修改,sersync 將通過內核自動捕獲到事件,並將該文件利用 rsync 同步到多台遠程服務器。sersync 僅僅同步發生增、刪、改事件的單個文件或目錄,不像rsync鏡像同步那樣需要比對雙方服務器整個目錄下數千萬的文件,並且支持多線程同步,因此效率非常高。
sersync已不在更新,感謝sersync作者周洋
谷歌項目地址:https://code.google.com/archive/p/sersync/
上不去google的可以直接wget我本地的包,這里只提供64位版本的:http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
壓縮包里面有兩個文件confxml.xml sersync2
sersync2 這個是sersync主程序
confxml.xml 這個是sersync配置文件
配置文件部分常用參數解釋:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host> #保留字段,默認即可
<debug start="false"/> #是否開啟調試模式,默認false即可
<fileSystem xfs="true"/> #是否開啟支持xfs文件系統,Centos7默認都是xfs的了,所以建議true開啟
<filter start="false"> #是否開啟過濾模式,根據需求開啟,例:過濾以.php結尾的文件(.*)\.php
<exclude expression="(.*)\.svn"></exclude> #過濾以.svn結尾的文件
<exclude expression="(.*)\.gz"></exclude> #過濾以.gz結尾的文件
<exclude expression="^info/*"></exclude> #過濾監控目錄下的info路徑
<exclude expression="^static/*"></exclude> #過濾監控目錄下的static路徑
<exclude expression="wwwroot/blogwhsir/*"></exclude> #過濾wwwroot/blogwhsir/目錄下所有文件
</filter>
<inotify> #inotify監控事件
<delete start="true"/> #如果不開啟此項,在刪除監控目錄下的文件時,目標服務器的文件則不會同時刪除,根據需求開啟
<createFolder start="true"/> #不開啟不能監控子目錄,建議true
<createFile start="false"/> #關閉提高通訊性能,默認就好
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/opt/tongbu"> #指定要監控的本地目錄
<remote ip="127.0.0.1" name="tongbu1"/> #指定要同步的目標服務器的IP地址,及目標服務器rsync的[模塊]
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync> #配置rsync
<commonParams params="-artuz"/> #rsync的參數
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/> #是否開啟rsync的認證模式,需要配置users及passwordfile,根據情況開啟(如果開啟,注意密碼文件權限一定要是600)
<userDefinedPort start="false" port="874"/><!-- port=874 --> #遠程目標服務器的端口不是默認端口時使用
<timeout start="false" time="100"/><!-- timeout=100 --> #是否開啟rsync的超時時間
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #目標服務器傳輸失敗時會重新傳輸,再次失敗會寫入rsync_fail_log中,每隔一段時間(timeToExecute)執行腳本再次傳輸
<crontab start="false" schedule="600"><!--600mins--> #對監控目錄與目標服務器每隔一段時間進行一次整體同步,默認600分鍾,根據個人情況是否開啟
<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="https://blog.whsir.com"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
|
一個簡單的演示示例:
確認當前系統關閉selinux及防火牆,或將873端口加入iptables白名單
我這里有兩台centos6.7的主機,IP分別是192.168.0.70和192.168.0.31
我在192.168.0.70安裝sersync,主動推送數據到192.168.0.31上
192.168.0.31上配置
安裝創建rsync配置文件
|
1
|
yum -y install rsync
|
|
1
|
vi /etc/rsyncd.conf
|
|
1
2
3
4
5
6
7
8
9
10
11
|
uid = root
gid = root
use chroot = no
hosts allow=*
max connections = 3
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
[tongbu]
path = /whsir
comment = whsir
read only = false
|
以守護進程方式啟動rsync
|
1
|
/usr/bin/rsync --daemon
|
192.168.0.70上配置
|
1
2
3
4
5
6
|
cd /usr/local/
wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar xzf sersync2.5.4_64bit_binary_stable_final.tar.gz
rm -rf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86/ sersync
cd sersync
|
我這里將sersync放在了/usr/local/sersync/目錄下
我這里修改了下confxml.xml文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?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>
<span style="color: #800000;"><localpath watch="/opt/tongbu"></span>
<span style="color: #800000;"><remote ip="192.168.0.31" name="tongbu"/></span>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<span style="color: #800000;"><commonParams params="-azvP"/></span>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<span style="color: #800000;"><failLog path="/tmp/rsync_fail_log.sh" timeToExecute="15"/><!--default every 60mins execute once--></span>
<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>
</head>
|
啟動sersync
|
1
|
chmod +x sersync2
|
|
1
|
/usr/local/sersync/sersync2 -n 10 -d -o /usr/local/sersync/confxml.xml
|
-n 啟用線程數量
-d daemon方式啟動
-o 指定配置文件
sersync整體同步一次
在sersync目錄下運行./sersync2 -r
每當192.168.0.70 /opt/tongbu目錄下上傳文件會主動推送到192.168.0.31 /whsir目錄下
如果推送失敗則在15分鍾后再次推送,我這里改的15分鍾,如果連續兩次失敗,則不會在推送了,建議在192.168.0.31上做個rsync拉,確保文件一致
