# 一、Sersync項目簡介與框架設計
[TOC]
## 1.1 項目簡介
Sersync項目利用inotify與rsync技術實現對服務器數據實時同步的解決方案,其中inotify用于監控sersync所在服務器上文件系統的時間變化,rsync是目前廣泛使用的本地及異地數據同步工具,其優點是只對變化的目錄數據操作,甚至是一個文件不同的部分進行同步,所以其優勢大大超過使用掛接文件系統或scp等方式進行鏡像同步。
目前使用的比較多的同步程序版本是inotify-tools,另外一個是google開源項目Openduckbill(依賴于inotify-tools),這兩個都是基于腳本語言編寫的,其設計思路同樣是采用inotify與rsync命令。
相比上面兩個項目,sersync項目的優點是
1. 使用C++編寫,對于Linux系統文件產生的臨時文件和重復的文件操作會進行過濾,再結合了rsync同步的時候,會減少運行時消耗的本地及網絡資源,因此速度會更快;
2. 相比較上面兩個項目,sersync配置起來很簡單,其中bin目錄下是已經編譯好的二進制文件,配合bin目錄下的xml文件直接使用即可;
3. 使用多線程進行同步(即可以并發同步多個不同文件),尤其在同步較大文件時,能夠保證多個服務器實時保持同步狀態;
4. sersync自帶出錯處理機制,通過失敗隊列對出錯的文件重新同步,如果仍舊失敗,則每10個小時對同步失敗的文件再重新同步;
5. sersync自帶crontab功能,只需在xml配置文件中開啟,即可按預先的配置,隔一段時間整體同步一次;
6. sersync自帶socket與http的協議拓展,可以滿足有特殊需求的公司二次開發。
## 1.2 基本構架

## 1.3 設計簡析
1. 線程組線程是等待縣城對立的守護線程,當事件隊列中有數據產生的時候,線程組守護線程就會組個喚醒同步線程,當隊列中inotify事件較多的時候,同步線程就會被全部喚醒一起工作。這樣設計的目的是能夠同時處理多個inotify事件,從而提升服務器的冰法同步能力。
之所以稱之為線程組線程,是因為每個線程在工作的時候,會根據服務器上新寫入文件的數量建立子線程,子線程可以保證所有的文件與各個服務器同時同步,當要同步的文件較大的時候,這樣的設計可以保證各個遠程服務器可以同事獲得要同步的文件。
2. 服務器線程的作用有三個(保證主服務器和從服務器數據一致且實時):
2.1 處理同步失敗的文件,將這些文件再次同步,對于再次同步失敗的文件會生成rsync_fail_log.sh腳本,記錄失敗的事件
2.2 同時每個10小時執行腳本一次,同時清空腳本。
2.3 還有就是crontab功能,可以每隔一定事件,將所有路徑整體同步一次。
3. 過濾隊列的建立是為了過濾短時間內產生的重復的inotify信息,例如在刪除文件夾的時候,inotify就會同事產生刪除文件夾里的文件與刪除文件夾的事件,通過過濾隊列,當刪除文件夾時間產生的時候,會將之前加入隊列的刪除文件的事件全部過濾掉,這樣只產生一條刪除文件夾事件,從而減輕了同步的負擔。同時對于修改文件的操作的時候,會產生臨時文件的重復操作。
# 二、sersync安裝配置
## 2.1 sersync同步需求邏輯圖
當前版本的sersync依賴于rsync進行數據同步,如下圖所示,在同步主服務器(Master)上開啟sersync,sersync負責監控配置路徑中的文件系統事件變化,然后調用rsync命令把更新的文件同步到目標服務器(Slave),因此,需要在主服務器配置sersync,在同步目標服務器配置rsync server(注意:是rsync服務器)

如圖所示,用戶會實時網左邊的同步主服務器(M)上寫入或更新文件數據,此時需要在左邊的同步服務器(M)上配置sersync服務,在右邊的同步目標服務器S1和S2服務器上配置rsync守護進程,這樣在主服務器M上產生的寫入或更新的文件就會被sersync服務實時的同步到多個目標服務器(S1,S2等)。一般的Linux服務器一般安裝了rsync軟件,因此在主服務器(M)上有rsync命令即可,在目標服務器(S1、S2)上只需要進行簡單的配置,并開啟rsync守候進程即可。
## 2.2 安裝環境準備
|角色|服務器配置|操作系統版本|IP|機器名|
|-|-|-|-|-|
|sersync服務(M)|VM|CentOS6.5/2.6.32-431.el6.x86_64|192.168.0.100|luo.centos5.5|
|rsync服務(S1)|VM|CentOS6.5/2.6.32-431.el6.x86_64|192.168.0.101|luo.centos6.5|
|rsync服務(S2)|VM|CetnOS6.7/2.6.32-573.el6.x86_64|192.168.0.102|luo.centos6.7|
## 2.3 配置同步服務器
~~~
部署總結:
步驟一、 配置slave上的rsync服務
步驟二、 配置master上sersync客戶端
~~~
### 2.3.1 slave上部署rsync服務
本次測試使用的服務器為S1和S2 server,即IP為192.168.0.101,192.168.0.102。
#### 2.3.1.1 升級rsync到3.0
檢查rsync版本,此操作在所有的機器上執行

如果版本較低的5.*系統,rsync的本本為3以下,我們可以通過yum -y install rsync命令來升級。(非必須)
#### 2.3.1.2 部署rsync服務 (Slave服務器上相關操作)
升級完畢后,在多臺同步slave服務器(S1,S2)上配置如下:
`vim /etc/rsyncd.conf` 寫入如下測試內容:
~~~
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
# GLOBAL OPTIONS
uid = root
gid = root
use chroot = no
read only = false
#limit access to private LANs
hosts allow=192.168.0.100/24
hosts deny=*
ignore errors
max connections = 2000
pid file = /var/run/rsyncd.pid
auth users = rsync_backup
secrets file = /etc/rsyncd.password
#lock file = /var/run/rsync.lock
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file
log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
# MODULE OPTIONS
[bbs]
comment = bbs by www 2015年10月27日15:53:24
path = /var/www/html/bbs
[blog]
comment = blog by www 2015年10月27日15:53:24
path = /var/www/html/blog
~~~
上面的rsync服務的配置文件表明允許sersync主服務器(IP為192.168.0.100)訪問,rsync同步模塊名為[bbs],[blog]將同步過來的文件分別放入對應的path指定的目錄 /var/www/html/bbs,/var/www/html/blog下,如果有多臺目標服務器,則每臺都需要進行類似的rsync服務配置,上面的uid,gid要換成本服務器對應的同步用戶。注意rsync服務賬戶(上述用了root用戶)要有對被同步目錄(/var/www/html)的寫入更新權限。
#### 創建待同步的目錄
~~~
[root@li-centos6.5 /var/www]
# mkdir -p /var/www/html/bbs /var/www/html/blog
[root@li-centos6.5 /var/www]
# tree /var/www/html
/var/www/html
├── bbs
└── blog
# 此步驟在S1,S2上都要執行,否則,rsync服務會因為沒有path路徑而無法啟動。
~~~
#### 配置相關權限認證
~~~
echo "rsync_backup:rsync-aaaaaa">/etc/rsyncd.password
chmod 600 /etc/rsyncd.password
# order for check
cat /etc/rsyncd.password
ll /etc/rsyncd.password
~~~
#### 配置好后,使用下面的命令開啟rsync守護進程
~~~
rsync --daemon
ps -ef|grep rsync
netstat -tunpl|grep :873
~~~
#### 配置開機自啟動
~~~
echo "/usr/bin/rsync --daemon">>/etc/rc.local
grep demoan /etc/rc.local
~~~
#### 重啟命令
~~~
pkill rsync
rsync --daemon
ps -ef |grep rsync
~~~
### 2.3.2 master上配置rsync客戶端(Master操作)
#### 2.3.2.1 master上配置rsync權限
在master(192.168.0.100)上配置rsync客戶端相關權限
~~~
echo 'rsync-aaaaaa' > /etc/rsync.password
chmod 600 /etc/rsync.password
cat /etc/rsync.password
ll /etc/rsync.password
~~~
#### 2.3.2.2 master上手工測試rsync同步情況
>[danger] 此步驟非常關鍵且重要,如果這里測試都不成功,后面的sersync配好也不會同步數據,同時當sersync同步出錯的時候我們也可以手工同步排錯。
**1. 分別創建待同步數據**
~~~
[root@li.centos5.5 /var/www]
# mkdir -p /var/www/html/bbs /var/www/html/blog # 創建待同步的目錄
[root@li.centos5.5 ~]
# touch /var/www/html/bbs/bbs.log /var/www/html/blog/blog.log # 在目錄下創建帶同步的文件
[root@li.centos5.5 /var/www]
# tree /var/www/html/ # 檢查目錄和文件創建情況
/var/www/html/
|-- bbs
| `-- bbg.log
`-- blog
`-- blog.log
2 directories, 2 files
~~~
**2. 執行同步命令(操作之前停用防火墻)**
~~~
rsync -avzP /var/www/html/bbs/ rsync_backup@192.168.0.101::bbs/ --password-file=/etc/rsync.password
rsync -avzP /var/www/html/bbs/ rsync_backup@192.168.0.102::bbs/ --password-file=/etc/rsync.password
rsync -avzP /var/www/html/blog/ rsync_backup@192.168.0.101::blog/ --password-file=/etc/rsync.password
rsync -avzP /var/www/html/blog/ rsync_backup@192.168.0.102::blog/ --password-file=/etc/rsync.password
~~~
>[danger] 提示:在后面進行部署sersync前,sersync主服務器上必須要確保手工可以把文件推送到S1,S2上,這樣后續sersync才能調用這些命令進行自動推送!!!
同步完后,可以到兩臺slave服務器上查看結果,如下:
~~~
[root@li-centos6.5 /var/www/html]
# tree /var/www/html # 檢查目錄和文件同步情況
├── bbs
│ └── bbg.log
└── blog
└── blog.log
2 directories, 2 files
[root@li-centos6.7 /var/www/html]
# tree /var/www/html. # 檢查目錄和文件同步情況
├── bbs
│ └── bbg.log
└── blog
└── blog.log
2 directories, 2 files
~~~
### 2.3.3 master上配置rsync客戶端
#### 2.3.3.1 下載sersync
在google code ( `http://code.google.com/p/sersync/downloads/list` )下載sersync的可執行文件版本,里面有配置文件和可執行文件,如下:
`cd /usr/local/src && wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz`
#### 2.3.3.2 安裝sersync
~~~
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync
[root@li.centos5.5 /usr/local]
# tree /usr/local/sersync/ # 查看
sersync/
|-- confxml.xml
`-- sersync2
0 directories, 2 files
~~~
#### 2.3.3.2 規范sersync目錄結構(非必須)
~~~
cd /usr/local/sersync/ && mkdir conf bin logs
mv confxml.xml ./conf/. && mv sersync2 ./bin/sersync
[root@li.centos5.5 /usr/local/sersync]
# tree
.
|-- bin
| `-- sersync2
|-- conf
| `-- confxml.xml
`-- logs
3 directories, 2 files
~~~
#### 2.3.3.3 配置sersync
~~~
# /bin/cp conf/confxml.xml conf/confxml.xml.luo.$(date +%F) # 備份配置文件
[root@li.centos5.5 /usr/local/sersync]
# ls conf/ # 查看配置文件備份情況
confxml.xml confxml.xml.luo.2015-10-29
~~~
初始化的配置文件內容如下:
~~~
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <head version="2.5">
3 <host hostip="localhost" port="8008"></host>
4 <debug start="false"/>
5 <fileSystem xfs="false"/>
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
11 </filter>
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
23 <sersync>
24 <localpath watch="/opt/tongbu"> # 定義本地要同步的目錄
25 <remote ip="127.0.0.1" name="tongbu1"/> # 定義要同步的服務器IP和模塊名(例如上述的blog、bbs)
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--defaul t every 60mins execute once-->
37 <crontab start="false" schedule="600"><!--600mins-->
40 <exclude expression="info/*"></exclude>
41 </crontabfilter>
42 </crontab>
43 <plugin start="false" name="command"/>
44 </sersync>
45
46 <plugin name="command">
47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongb u/mmm.sh suffix-->
48 <filter start="false">
49 <include expression="(.*)\.php"/>
50 <include expression="(.*)\.sh"/>
51 </filter>
52 </plugin>
53
54 <plugin name="socket">
55 <localpath watch="/opt/tongbu">
56 <deshost ip="192.168.138.20" port="8009"/>
57 </localpath>
58 </plugin>
59 <plugin name="refreshCDN">
60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passw d="xxxx"/>
62 <sendurl base="http://pic.xoyo.com/cms"/>
63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/ images"/>
64 </localpath>
65 </plugin>
66 </head>
~~~
**1) 修改24~28行的內容**
~~~
24 <localpath watch="/opt/tongbu"> # 定義本地要同步的目錄
25 <remote ip="127.0.0.1" name="tongbu1"/> # 定義要同步的服務器IP和模塊名(例如上述的blog、bbs)
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
~~~
**修改后如下:**
~~~
<localpath watch="/var/www/html/blog"> # 定義本地要同步的目錄
<remote ip="192.168.0.101" name="blog"/> # 定義要同步的服務器IP和模塊名(例如上述的blog、bbs)
<remote ip="192.168.0.102" name="blog"/>
</localpath>
<localpath watch="/var/www/html/bbs"> # 定義本地要同步的目錄
<remote ip="192.168.0.101" name="bbs"/> # 定義要同步的服務器IP和模塊名(例如上述的blog、bbs)
<remote ip="192.168.0.102" name="bbs"/>
</localpath>
~~~
**如下圖所示:**

**2) 修改31~34行(認證部分)**
~~~
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
~~~
**修改后的內容如下:**
~~~
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <!--slave服務器的用戶名-->
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
~~~
其實以上的配置文件就是在拼接同步語句`rsync -avzP /var/www/html/blog/ rsync_backup@192.168.0.102::blog/ --password-file=/etc/rsync.password` 這個命令的參數而已。
**如下圖所示:**

**3) 修改36~37行**
`36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->`
**修改后的內容如下:**
`36 <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->`
當同步失敗后,日志記錄到`/usr/local/sersync/logs/rsync_fail_log.sh`文件中,并且每60分鐘對失敗的log進行重新同步。
**修改后的完整配置文件如下:**
~~~
[root@luo.centos5.5 /usr/local/sersync]
# cat -n /usr/local/sersync/conf/confxml.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <head version="2.5">
3 <host hostip="localhost" port="8008"></host>
4 <debug start="false"/>
5 <fileSystem xfs="false"/>
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
22
23 <sersync>
24 <localpath watch="/var/www/html/blog">
25 <remote ip="192.168.0.101" name="blog"/>
26 <remote ip="192.168.0.102" name="blog"/>
27 </localpath>
28 <localpath watch="/var/www/html/bbs">
29 <remote ip="192.168.0.101" name="bbs"/>
30 <remote ip="192.168.0.102" name="bbs"/>
31 </localpath>
32 <rsync>
33 <commonParams params="-artuz"/>
34 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
35 <userDefinedPort start="false" port="874"/><!-- port=874 -->
36 <timeout start="true" time="100"/><!-- timeout=100 -->
37 <ssh start="false"/>
38 </rsync>
39 <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
40 <crontab start="false" schedule="600"><!--600mins-->
41 <crontabfilter start="false">
42 <exclude expression="*.php"></exclude>
43 <exclude expression="info/*"></exclude>
44 </crontabfilter>
45 </crontab>
46 <plugin start="false" name="command"/>
47 </sersync>
48
49 <plugin name="command">
50 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
51 <filter start="false">
52 <include expression="(.*)\.php"/>
53 <include expression="(.*)\.sh"/>
54 </filter>
55 </plugin>
56
57 <plugin name="socket">
58 <localpath watch="/opt/tongbu">
59 <deshost ip="192.168.138.20" port="8009"/>
60 </localpath>
61 </plugin>
62 <plugin name="refreshCDN">
63 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
64 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
65 <sendurl base="http://pic.xoyo.com/cms"/>
66 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
67 </localpath>
68 </plugin>
69 </head>
~~~
#### 2.3.3.4 開啟sersync守護進程同步數據
配置sersync環境變量
~~~
echo 'export PATH=/usr/local/sersync/bin:$PATH'>>/etc/profile
tail -1 /etc/profile
source /etc/profile
which sersync
~~~
**啟動sersync命令:**
`sersync -r -d -o /usr/local/sersync/conf/confxml.xml`
正確的配置啟動結果如下:
~~~
[root@luo.centos6.5 /usr/local/sersync/conf]
# ~/rsesync_start.sh
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: -r rsync all the local files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name: /usr/local/sersync/conf/bbs_confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) + 20(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 /var/www/html/bbs && rsync -artuz -R --delete ./ --timeout=100 rsync_backup@192.168.0.101::bbs --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /var/www/html/bbs
set the system param
~~~
測試結果:發現問題,多實例的情況下僅第一個模塊的路徑能同步,其他模塊下面的路徑不能同步。
**配置多實例的同步情況**(針對多個模塊同步的情況)
~~~
sersync –d –o /usr/local/serysnc/conf/blog_confxml.xml
sersync –d –o /usr/local/serysnc/conf/bbs_confxml.xml
… …
~~~
不同的config文件是將模塊分開放置,即:**2.3.3.3**步驟中的第一步中的代碼分開在幾個不同的文件中,這樣啟動的時候指定模塊同步。
**將上述命令,寫入/etc/rc.local中,隨系統自啟動**
~~~
/bin/cp /etc/rc.local /etc/rc.local_luo_$(date +%F)
cat >>/etc/rc.local<<EOF
# sersync data to 192.168.0.101,192.168.1.102
sersync –d –o /usr/local/serysnc/conf/blog_confxml.xml
sersync –d –o /usr/local/serysnc/conf/bbs_confxml.xml
EOF
[root@luo.centos6.5 /var/www/html]
# tail -3 /etc/rc.local
# sersync data to 192.168.0.101,192.168.1.102
sersync –d –o /usr/local/serysnc/conf/blog_confxml.xml
sersync –d –o /usr/local/serysnc/conf/bbs_confxml.xml
~~~
至此,已經完成了Master上對Slave1,Slave2上多實例(bbs,blog多模塊)的同步。
**Sersync同步參數說明**
|Sersync參數|說明|
|-|-|
| -r | -r參數作用是在開啟實時監控之前對主服務器目錄與遠程目標機目錄進行一次整體同步,如果需要將sersync運行前,主服務器目錄下已經存在的所有文件或目錄全部同步到遠端,則需要指定-r參數運行sersync,將本地與遠程整體同步一次**。說明:如果設置了過濾器,即在xml配置文件中,filter為true,則暫時不能使用-r參數進行整體同步。**|
|–o confxml.xml|不指定-o參數時,sersync可執行文件目錄下的默認配置文件confxml.xml,如果需要使用其他的配置文件,可以使用-o參數指定其他配置文件,通過-o參數,我們可以指定多個不同的配置文件,從而實現sersync多進行多實例的數據同步。|
|–n number|-n參數為指定默認的線程池的現充總數。|
例如:sersync –n 5 則指定線程總數為5,如果不指定。默認啟用線程池數量為10,如果cpu使用過高,可通過該參數調低,如果機器配置較高,可以通過該參數調高線程總數,提高同步效率
|-d|-d參數為后臺啟動服務,在通常情況下,使用-r參數對本地到遠程整體同步一遍后,在后臺運行此參數啟動守護進程實時同步,在第一次整體同步時,-d和-r參數經常會聯合使用。|
|-m pluginName|-m參數為不進行同步,只運行插件
例如 sersync –m command,則在監控到時間后,不對遠程目標服務器進行同步,而是直接運行command插件。|
# 三、sersync配置文件說明
## 3.1 XML配置文件說明
Sersync可選功能是通過xml配置文件來實現的,基本配置文件內容如下
**初始化配置文件:**
~~~
[root@luo.centos6.5 /usr/local/sersync]
# cat -n conf/confxml.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <head version="2.5">
3 <host hostip="localhost" port="8008"></host>
4 <debug start="false"/>
5 <fileSystem xfs="false"/>
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
22
23 <sersync>
24 <localpath watch="/opt/tongbu">
25 <remote ip="127.0.0.1" name="tongbu1"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
37 <crontab start="false" schedule="600"><!--600mins-->
38 <crontabfilter start="false">
39 <exclude expression="*.php"></exclude>
40 <exclude expression="info/*"></exclude>
41 </crontabfilter>
42 </crontab>
43 <plugin start="false" name="command"/>
44 </sersync>
45
46 <plugin name="command">
47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
48 <filter start="false">
49 <include expression="(.*)\.php"/>
50 <include expression="(.*)\.sh"/>
51 </filter>
52 </plugin>
53
54 <plugin name="socket">
55 <localpath watch="/opt/tongbu">
56 <deshost ip="192.168.138.20" port="8009"/>
57 </localpath>
58 </plugin>
59 <plugin name="refreshCDN">
60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
62 <sendurl base="http://pic.xoyo.com/cms"/>
63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
64 </localpath>
65 </plugin>
66 </head>
~~~
下面一一進行解釋說明:
xml配置文件的注釋不是 "#",而是 "<!—中間是注釋的內容書寫區域--> "
` 3 <host hostip="localhost" port="8008"></host>`
hostip和port是針對插件的保留字段,對于同步功能沒有任何作用,默認保留即可。
## 3.2 debug開啟開關
` 4 <debug start="false"/>`
設置為true值時,開啟debug模式,會在sersync正在運行的控制臺,打印inotify時間與rsync的同步命令。
## 3.3 xfs文件系統開關
`5 <fileSystem xfs="false"/>`
對于xfs文件系統的用戶,需要將這個選項開啟,才能使sersync正常工作。
### 3.4 filter文件過濾功能
對于sersync監控的文件,會默認過來系統的臨時文件(以"."開頭,以"~"結尾),除了這些文件意外,可以自定義其他需要過濾的文件。
~~~
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
~~~
將start設置為true后開啟過濾功能,在exclude標簽中,填寫正則表達式,默認給出兩個例子分別是過濾以".gz"結尾的文件與過濾監控目錄下的info路徑(監控路徑^/info /*),可以根據需要添加,但開啟的時候要提前測試,正則表達式如果出現錯誤,控制臺會有所提示,相比較使用rsync的exclude功能,被過濾的路徑不會加入監控,大大減少了rsync同步的通訊量。
## 3.5 inotify監控參數設定(調優)
對于inotify監控參數可以進行設置,根據項目的特點優化sersync
~~~
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
~~~
對于大多數的應用,可以嘗試吧createFile(監控文件時間選項)設置為false來提高性能,減少rsync通訊,因為拷貝文件到監控目錄會產生create事件與close_write事件,所以如果關閉create事件,只監控文件拷貝結束時的事件close_write,同樣可以實現文件的完整同步。
注意:要使得createFolder保持為true,如果將createFolder設為flase,則不會對產生的目錄進行監控,該目錄下的子文件與子目錄也不會被監控,除非有特殊需要,否則建議開啟。默認情況下對創建文件(目錄)事件與刪除文件(目錄)事件都進行監控,如果項目中不需要刪除遠程目標服務器的文件(目錄),則可以將delete參數設置為false,則不對刪除事件監控。
## 3.6 文件監控與遠程同步設置
~~~
24 <localpath watch="/opt/tongbu">
25 <remote ip="127.0.0.1" name="tongbu1"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
~~~
/opt/tongbu 目錄為sersync主服務器本地待同步的目錄。
ip=192.168.8.39,為服務器的IP地址,如果有多個從服務器依次列出來即可。
Name=tonngbu,這里的tongbu是slave服務器上的rsyncd.conf中的模塊名字。
## 3.7 rsync相關參數設置
~~~
29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
~~~
在commonParams項中,可以自定義rsync的同步參數,默認是-artuz。
auth start=false,設置為true的時候,使用rsync的認證模式傳送,需要配置user與passwordfile(--password-file=/etc/rsyncpassword),來使用。
userDefinedPort當遠程同步目標服務器的rsync端口不是默認端口的時候使用(-port=874)。
timeout設置rsync的timeout時間(-timeout=100)。
ssh start=false 如果改成true時表示ssh使用rsync –e ssh的方式進行傳輸。
## 3.8 失敗日志腳本配置
` 36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->`
如果文件或文件夾同步傳輸失敗,會送心傳送,再次失敗就會寫入 `/tmp/rsync_fail_log.sh`,然后每隔一段時間(timeToExecute進行設置),執行該腳本再重新傳送,然后清空該腳本,可以通過path來設置日志路徑。
## 3.9 crontab定期整體同步功能
~~~
37 <crontab start="false" schedule="600"><!--600mins-->
38 <crontabfilter start="false">
39 <exclude expression="*.php"></exclude>
40 <exclude expression="info/*"></exclude>
41 </crontabfilter>
42 </crontab>
~~~
crontab可以對健康服務與遠程目標主機每隔一段時間進行一次整體同步,可能由于一些原因兩次失敗重傳都失敗了,這個時候如果開啟了crontab功能,還可以進一步保證各服務器文件一直,如果文件兩比較大,crontab的時間間隔要設置大些,否則可能增加服務器之間通訊的開銷。
schedule參數時設置crontab的時間間隔,默認每600分鐘同步一次。
如果開啟了filter文件過濾功能,那么crontab整體同步也需要設置過濾,否則雖然實時同步的時候文件被過濾了,但crontab整體同步的時候,如果不單獨設置crontabfilter,還會將要過濾的文件同步到遠程從服務器,crontab的過濾正則與filter過濾不同,也給出了兩個實例分別對應于過濾文件與目錄。總之如果同時開啟了filter與crontab,則要開啟crontab的crontabfilter,并按示例設置使其與filter的過濾一一對應。
## 3.10 插件設置
` 43 <plugin start="false" name="command"/>`
當start=false處設置為true時,將文件同步到元辰服務器后會調用name參數指定的插件。參見插件設置。
# 四、sersync插件基本配置和使用
## 4.1插件部分xml內容
~~~
46 <plugin name="command">
47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
48 <filter start="false">
49 <include expression="(.*)\.php"/>
50 <include expression="(.*)\.sh"/>
51 </filter>
52 </plugin>
53
54 <plugin name="socket">
55 <localpath watch="/opt/tongbu">
56 <deshost ip="192.168.138.20" port="8009"/>
57 </localpath>
58 </plugin>
59 <plugin name="refreshCDN">
60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
62 <sendurl base="http://pic.xoyo.com/cms"/>
63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
64 </localpath>
65 </plugin>
~~~
如上所示,其中plugin標簽設置為true時,在同步文件或路徑到遠程服務器之后,會調用插件。通過name參數指定需要執行的插件,目前支持的有command refreshCDN socket http四種插件,其中 http插件目前由于兼容性原因已經去除,以后會重新加入。
以下模塊(command refreshCDN socket http)可以大度使用(發生文件愛你改變不同步只調用插件),只需在命令行下使用-m參數即可,如果需要作為插件與同步程序一起使用,詳見同步程序說明的插件設置。
## 4.2 command插件
~~~
46 <plugin name="command">
47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
48 <filter start="false">
49 <include expression="(.*)\.php"/>
50 <include expression="(.*)\.sh"/>
51 </filter>
52 </plugin>
~~~
當文件同步完成后,毀掉紅command插件,如同步文件是index.php,則index.php文件在改動之后,調用rsync同步到遠程服務器后,調用command插件,執行
`/bin/sh index.php suffix >/dev/null 2>&1`
如果suffix設置了,則會放在inotify事件test.php之后,如果ignoreError為true,則會添加 >/dev/null 2>&1,當然還可以設置command的filter,當filter為true時,include可以只對正則匹配到的文件調用command。
“refreshCDN”就是同步過程中將文件發送到目的服務器后刷新cdn接口,如果不想使用,則將start屬性設置為flase即可。
如果需要使用其他插件,則查看其他差價你的plugin標簽,將插件名稱改為xml中其他插件的名稱即可,該模塊根據chinaCND的協議進行設計,當有文件產生的時候,就像cdn接口發送需要刷新的路徑位置,刷新CDN模塊需要配置xml文件如下:
~~~
59 <plugin name="refreshCDN">
60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
62 <sendurl base="http://pic.xoyo.com/cms"/>
63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
64 </localpath>
~~~
其中,`localpath watch=/data0/htdocs/cms.xoyo.com/site/` 是需要監控的目錄,cdninfo標簽指定了cdn借口的域名。端口號以及用戶名和密碼。
Sendurl標簽是需要刷新的url的前綴。
regexurl屬性為true時,使用match屬性的正則語句匹配inotify返回的路徑信息,并將正則匹配到的部分作為url的一部分。
## 4.3 socket插件
socket插件,開啟該模塊,則向指定ip與端口發送inotify所產生的文件路徑信息。
## 4.4 http插件
http插件,可以向指定域名的主機post,inotify監控的事件。目前已經去除。
## 4.5 單獨運行插件
插件也可以單獨運行,即不對遠程目標主句進行同步,直接調用插件:
只調用command插件
`sersync –d –m command`
只調用refreshCDN插件
`sersync –d –m refreshCDN`
只調用socket插件
`sersync –d –m socket`
只調用http插件
`sersync –d –m http`
- Linux的安裝
- Linux的軟件安裝管理
- Linux零碎的命令
- 了解ssh
- 系統調優及安全設置
- Linux系統中的硬鏈接和軟連接
- Linux文件和目錄的屬性及權限
- 命令總結
- 文件目錄管理命令
- cat
- cd
- cp
- ls
- mkdir
- mv
- rm
- touch
- vi
- 硬件檢測內核shell命令
- echo
- alias
- dd
- diff
- date
- grep(重要)
- head
- rpm
- sed(重要)
- tree
- unalias
- yum
- seq
- su
- less
- more
- tail
- awk(重要)
- pwd
- history
- whoami
- find(重要)
- which
- vimdiff
- sudo
- wget
- ln
- chmod
- chown
- vim常用技巧
- scp
- 工作場景應用總結
- 自動刪除n天前日志
- 刪除一個目錄下的所有文件但保留一個文件
- Linux軟件安裝
- php安裝(apache版本)
- vsftpd安裝
- git安裝
- python安裝
- LNMP安裝
- LAMP安裝I
- LAMP安裝II
- svn安裝
- svn在Linux下的命令操作
- svn鉤子簡介
- svn代碼上傳流程
- Crond介紹
- sersync應用指南
- 其他
- 小結一
- 系統調優重新整理tmp
- linux禁止root用戶直接登錄sshd并修改默認端口