rsync與Sersync2的配置過程

sersync(客戶端進程監 控指定目錄更新事件的變化,觸發式把變化數據推送到服務端)
rsync服務器:[email protected]
sersync2服務器 rsync客戶端:[email protected]
 
#建立 rsync 服務器的配置文件/etc/rsyncd.conf
[root@sukaka ~]# cat /etc/rsyncd.conf
uid=root                                            #服務器端傳輸文件時,要發哪個用戶和用戶組來執行,默認是nobody。 如果用nobody 用戶和用戶組,可能遇到權限問題,有些文件從服務器上拉不下來。爲了方便,用了root 。不過可以在定義要同步的目錄時定義的模塊中指定用戶來解決權限的問題。
gid=root
max connections=100
use chroot=no                                       #用chroot,在傳輸文件之前,服務器守護程序在將chroot 到文件系統中的目錄中,這樣做的好處是可能保護系統被安裝漏洞侵襲的可能。缺點是需要超級用戶權限。另外對符號鏈接文件,將會排除在外。也就是說,你在rsync服務器上,如果有符號鏈接,你在備份服務器上運行客戶端的同步數據時,只會把符號鏈接名同步下來,並不會同步符號鏈接的內容;這個需要自己來嘗試。
log file=/var/log/rsyncd.log                        #日誌
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
motd file=/etc/rsyncd.motd                          #歡迎文件位置
#transfer log
transfer logging = yes                              #啓用傳輸日誌
log format = %t %a %m %f %b                         #傳輸日誌的格式
syslog facility = local3                            #日誌等級
timeout = 300                                       #超時時間
[share]                                             #模塊名稱
path=/root                                          #需要同步的目錄
comment = my htdocs                                 #備註
secrets file = /etc/rsyncd.pass                     #安全文件,格式用戶名:密碼不要多加任何字符 權限600
ignore errors                      
read only = no                                      #只讀
hosts allow=10.0.0.0/24,192.168.68.0/24             #允許的IP範圍
 
 

 

[root@sukaka ~]# cat /etc/rsyncd.pass   #rsync服務器  設置格式user:password不要有多餘字符
root:test                
[root@sukaka1 ~]# cat /etc/rsync.pass    #客戶端只需要有密碼 現在的客戶端就是sersync2的服務端
test
#啓動rsync服務
[root@sukaka ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@sukaka ~]# netstat -anlp |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2584/xinetd
tcp 0 0 :::873 :::* LISTEN 2784/rsync
 
[root@sukaka1 test]# ls
sersync.tar.gz
[root@sukaka1 test]# tar zxf sersync2.5_32bit_binary_stable_final.tar.gz               解壓
[root@sukaka1 test]# ls
GNU-Linux-x86 sersync.tar.gz serysnc svndata
[root@sukaka1 test]# cd GNU-Linux-x86/
[root@sukaka1 GNU-Linux-x86]# ls
confxml.xml sersync2
[root@sukaka1 GNU-Linux-x86]# mkdir /etc/sersync2 -p                                     創建serync2的目錄
[root@sukaka1 GNU-Linux-x86]# cp confxml.xml /etc/sersync2/
[root@sukaka1 GNU-Linux-x86]# cp sersync2 /usr/bin/
[root@sukaka1 GNU-Linux-x86]# cat /etc/sersync2/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="/root">#本地需要同步的目錄
<remote ip="192.168.68.212" name="share"/>#遠程同步的地址和同步的模塊
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<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>
<failLog path="/var/log/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/p_w_picpaths"/>
</localpath>
</plugin>
</head>
 
[root@sukaka1 GNU-Linux-x86]# /usr/bin/sersync2 -r -d -o confxml.xml      啓動sersync2 -rdo參數下面有具體的解釋
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 confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
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: 22 = 12(Thread pool nums) + 10(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 /root && rsync -artuz -R --delete ./ 192.168.68.212::zhanglei >/dev/null 2>&1
run the sersync:
watch path is: /root
[root@sukaka1 GNU-Linux-x86]# cd ~
[root@sukaka1 ~]# ls
test
[root@sukaka ~]# ls                                           #遠程rsync服務器
[root@sukaka1 ~]# touch 1 2 3 4 5 6 7 8           #回到sersync2服務器創建文件
[root@sukaka1 ~]# ls
1 2 3 4 5 6 7 8 test
[root@sukaka ~]# ls                                            #這時可以看到遠程rsync服務器目錄下也有了同樣的文件,說明時 時同步成功
1 2 3 4 5 6 7 8
 
sersync2完全安裝配置說明
    當前版本的sersync依賴於rsync進行同步。如下圖所示,在同步主服務器上開啓sersync,將監控路徑中的文件同步到目標服務器,因此需要在主服務器配置sersync,在同步目標服務器配置rsync。對於rsync配置,在google上可以找到很多資料,以下只是一些必要的配置和使用說明,僅供參考,請根據您的實際情況修改。
    如圖所示,需要在同步主服務器上配置sersync,在同步目標服務器配置rsync,並在目標服務器開啓rsync守候進程,這樣在主服務器產生的文 件,就會被sersync實時同步到多個目標服務器。在centos系統下默認已經安裝了rsync,只需進行配置,並開啓rsync守候進程即可。
 
配置同步目標服務器rsync
在多臺目標服務器上配置如下:
vi /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[tongbu1]
path=/opt/tongbu1
comment = xoyo video files
ignore errors = yes
read only = no
hosts allow = 192.168.0.100/24
hosts deny = *
    上面配置文件,表明允許主服務器(假設ip爲192.168.0.100)訪問,rsync同步模塊名爲[tongbu1],將同步過來的文件放入path指定的目錄/opt/tongbu1。如果有多臺從服務器,則每一臺都需要進行類似的rsync配置,上面的uid gid要換成您服務器的相應用戶,主意rysnc要有對被同步目錄的操作權限。配置好之後,使用如下命令,開啓rsync守護進程:
rsync --daemon在主服務器上安裝配置sresync
google code下載sersync的可執行文件版本,裏面有配置文件與可執行文件,這裏用sersync2.5進行說明,新版本配置形式類似。
 
1.在主服務器上開啓sersync守護進程,使sersync在後臺運行,開啓實時同步。
./sersync -d
過程如下:
[root@localhost GNU-Linux-x86]# ls
confxml.xml sersync2
[root@localhost GNU-Linux-x86]# ./sersync2 -d
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
daemon thread num: 10
parse xml config file
host ip : localhost     host port: 8008
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)
please according your cpu ,use -n param to adjust the cpu rate
run the sersync:
watch path is: /opt/tongbu
表明,sersync已經開啓,可以在本地監控路徑下建立文件,查看遠程是否同步成功。
 
2.在開啓實時監控的之前對主服務器目錄與遠程目標機目錄進行一次整體同步
 
./sersync -r如果需要將sersync運行前,已經存在的所有文件或目錄全部同步到遠程,要以-r參數運行sersync,將本地與遠程整體同步一次。
如果設置了過濾器,即在xml文件中,filter爲true,則暫時不能使用-r參數進行整體同步。-r參數將會無效。
 
3.查看啓動參數幫助
./sersync --help
 
4.指定配置文件
./sersync -o XXXX.xml對於sersync使用可執行文件目錄下的默認配置文件confxml.xml,如果需要使用另一個配置文件,可以使用-o參數指定其它配置文件。
 
5.指定默認的線程池的線程總數
./sersync -n num例如 ./sersync -n 5 則指定線程總數爲5,如果不指定,默認啓動線程池數量是10,如果cpu使用過高,可以通過這個參數調低,如果機器配置較高,可以用-n跳高線程總數。
 
6.不進行同步,只運行插件
./sersync -m pluginName例如./sersync -m command,則在監控到文件事件後,不對遠程目標服務器進行同步,而是直接運行command插件。
 
7.多個參數可以配合使用
./sersync -n 8 -o abc.xml -r -d表示,設置線程池工作線程爲8個,指定abc.xml作爲配置文件,在實時監控前作一次整體同步,以守護進程方式在後臺運行。
 
8.通常情況下,對本地到遠程整體同步一遍後,在後臺運行實時同步。
./sersync -d
 
XML文件說明
sersync可選功能是通過xml配置文件來實現的,基本配置文件如下:
 
view sourceprint?01 <?xml version="1.0" encoding="ISO-8859-1"?> 
02 <head version="2.5"> 
03      <host hostip="localhost" port="8008"></host> 
04      <filter start="false"> 
05          <exclude expression="(.*)\.gz"></exclude> 
06          <exclude expression="^info/*"></exclude> 
07      </filter> 
08      <inotify> 
09          <delete start="true"/> 
10          <createFolder start="true"/> 
11 <createFile start="true"/> 
12      </inotify> 
13      <debug start="false"/> 
14      <sersync> 
15          <localpath watch="/opt/tongbu"> 
16              <remote ip="192.168.0.104" name="tongbu1"/> 
17              <!--<remote ip="192.168.8.39" name="tongbu"/>-->
18              <!--<remote ip="192.168.8.40" name="tongbu"/>-->
19          </localpath> 
20          <rsync> 
21             <commonParams params="-artuz"/> 
22              <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> 
23              <userDefinedPort start="false" port="874"/><!-- port=874 -->
24              <timeout start="false" time="100"/><!-- timeout=100 -->
25              <ssh start="false"/> 
26          </rsync> 
27          <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60min execute once-->
28          <crontab start="false" schedule="600"><!--600mins-->
29              <crontabfilter start="false"> 
30                  <exclude expression="*.gz"></exclude> 
31                  <exclude expression="info/*"></exclude> 
32              </crontabfilter> 
33          </crontab> 
34          <plugin start="false" name="command"/> 
35      </sersync>
 
下面做逐行的進行解釋說明:
<host hostip="localhost" port="8008"></host>hostip與port是針對插件的保留字段,對於同步功能沒有任何作用,保留默認即可。
 
filter文件過濾功能
對於sersync監控的文件,會默認過濾系統的臨時文件(以“.”開頭,以“~”結尾),除了這些文件外,可以自定義其他需要過濾的文件。
 
<filter start="true">
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
</filter>將start設置爲 true,在exclude標籤中,填寫正則表達式,默認給出兩個例子分別是過濾以”.gz”結尾的文件與過濾監控目錄下的info路徑(監控路徑/info /*),可以根據需要添加,但開啓的時候,自己測試一下,正則表達式如果出現錯誤,控制檯會有提示。相比較使用rsync 的exclude功能,被過濾的路徑,不會加入監控,大大減少rsync的通訊量。
 
inotify監控參數設定(優化)
對於inotify監控參數可以進行設置,根據您項目的特點優化srsync。
 
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
</inotify>對於大多數應用,可以嘗試把createFile(監控文件事件選項)設置爲false來提高性能,減少 rsync通訊。因爲拷貝文件到監控目錄會產生create事件與close_write事件,所以如果關閉create事件,只監控文件拷貝結束時的事件close_write,同樣可以實現文件完整同步。
注意:強將createFolder保持爲true,如果將createFolder設爲false,則不會對產生的目錄進行監控,該目錄下的子文件與子目錄也不會被監控。所以除非特殊需要,請開啓。默認情況下對創建文件(目錄)事件與刪除文件(目錄)事件都進行監控,如果項目中不需要刪除遠程目標服務器的文件(目錄),則可以將delete 參數設置爲false,則不對刪除事件進行監控。
 
Debug開啓
 
<debug start="false"/>設置爲true,開啓debug模式,會在sersync正在運行的控制檯,打印inotify事件與rsync同步命令。
 
XFS文件系統
 
<fileSystem xfs="false"/>對於xfs文件系統的用戶,需要將這個選項開啓,才能使sersync正常工作.
 
文件監控與遠程同步設置
 
<localpath watch="/opt/tongbu">
<remote ip="192.168.0.104" name="tongbu1"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>詳見sersync2 完全安裝配置說明(一) —-基本功能使用
 
Rsync參數配置
 
<rsync>
<commonParams params="-artuz"/>
<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>commonParams可以用戶自定義rsync參數,默認是-artuz
auth start=”false” 設置爲true的時候,使用rsync的認證模式傳送,需要配置user與passwrodfile(–password-file=/etc/rsync.pas),來使用。userDefinedPort 當遠程同步目標服務器的rsync端口不是默認端口的時候使用(–port=874)。timeout設置rsync的timeout時間(–timeout=100)。ssh 使用rsync -e ssh的方式進行傳輸。
失敗日誌腳步配置
 
 
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>對於失敗的傳輸,會進行重新傳送,再次失敗就會寫入rsync_fail_log,然後每隔一段時間(timeToExecute進行設置)執行該腳本再次重新傳送,然後清空該腳本。可以通過path來設置日誌路徑。
 
Crontab定期整體同步功能
 
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.gz"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>crontab可以對監控路徑與遠程目標主機每隔一段時間進行一次整體同步,可能由於一些原因兩次失敗重傳都失敗了,這個時候如果開啓了crontab功 能,還可以進一步保證各個服務器文件一致,如果文件量比較大,crontab的時間間隔要設的大一些,否則可能增加通訊開銷。schedule這個參數是設置crontab的時間間隔,默認是600分鐘
如果開啓了filter文件過濾功能,那麼crontab整體同步也需要設置過濾,否則雖然實時同步的時候文件被過濾了,但crontab整體同步的時候 如果不單獨設置crontabfilter,還會將需過濾的文件同步到遠程,crontab的過濾正則與filter過濾的不同,也給出了兩個實例分別對 應與過濾文件與目錄。總之如果同時開啓了filter與crontab,則要開啓crontab的crontabfilter,並按示例設置使其與filter的過濾一一對應。
 
插件設置
 
<plugin start="false" name="command"/>當設置爲true的時候,將文件同步到遠程服務器後會調用name參數指定的插件。詳見請看插件設置。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章