sersync自動化同步部署
0. 前提:
部署rsync+sersync服務,實現實時監控目錄的變化,並實時同步變化檔案。
192.168.1.9:安裝rsync server服務。
192.168.1.10:安裝rsync,sersync服務。
監控192.168.1.10(/data/{www,bbs}) --> 192.168.1.9(/data/{www,bbs})
【原理】:
- 監控同步伺服器(192.168.1.10)上開啟sersync服務,監控路徑下檔案系統發生的變化;
- 呼叫rsync命令將變化的檔案同步到目標伺服器(192.168.1.9);
Name | IP | 軟體 |
---|---|---|
sersync服務(Master) | 192.168.1.10 | sersync2,rsync |
rsync服務(Slave) | 192.168.1.9 | rsync server |
1. 部署rsync server服務
1.1 配置rsync配置檔案
# vim /etc/rsyncd.conf uid = ftp gid = ftp use chroot = no max connections = 5000 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log motd file = /etc/rsyncd.motd secrets file =/etc/rsyncd.password auth users = rsync_backup port = 5678 ######################################### [www] path = /data/www comment = www ignore errors = yes read only = no hosts allow = 192.168.1.10 hosts deny = * ######################################## [bbs] path = /data/bbs comment = bbs ignore errors = yes read only = no hosts allow = 192.168.1.10 hosts deny = *
1.2 rsync配置檔案說明:
# 同步後文件uid/gid uid = ftp gid = ftp # 預設為true,修改為no,增加對目錄檔案軟連線的備份。涉及安全選項,建議關閉。 use chroot = no # 最大連線數 max connections = 36000 #超時時間 timeout = 600 # pid檔案檔案 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log # 每次連線都展示提示訊息的檔案位置 motd file = /etc/rsyncd.motd # 使用者名稱密碼檔案位置 secrets file =/etc/rsyncd.password # 使用者 auth users = rsync_backup # 埠號 port = 5678 ######################################### # 認證模組名稱 [www] # 同步儲存位置 path = /data/www # 備註說明 comment = www # 忽略無關錯誤 ignore errors = yes # 設定為讀寫許可權 read only = no # 執行的IP hosts allow = 192.168.1.10 # 禁止的IP hosts deny = * ######################################## [bbs] path = /data/bbs comment = bbs ignore errors = yes read only = no hosts allow = 192.168.1.10 hosts deny = *
1.3 建立密碼檔案修改許可權600
# vim /etc/rsyncd.password rsync_backup:hello # chmod 600 /etc/rsyncd.password
1.4 建立提示檔案(可有可無)
# cat /etc/rsyncd.motd ############################ ####### START RSYNC ######## ############################
1.5 建立相關待同步的目錄
# mkdir /data/{www,bbs}-pv # chown ftp:ftp /data/{www,bbs}
注意:同步目錄的所屬組和所屬使用者要和rsync配置檔案上的uid和gid一致。
1.6 啟動守護程序,並寫入開機自啟動
# rsync --daemon #指定配置檔案啟動 # rsync --daemon --config=/etc/rsyncd.conf # echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf">>/etc/rc.local
1.7 測試(192.168.1.10)
在192.168.1.10上建立密碼檔案,只需要寫入密碼即可,然後賦予600許可權。通過rsync進行同步測試檔案是否可以同步成功。在192.168.1.9服務上/data/{www,bbs}上檢視是否有同步的檔案。必須保證這一步成功,否則後續的操作無法成功。
# cat /etc/rsync.passwd hello # chmod 600 /etc/rsync.passwd # rsync -artuz --port=5678 anaconda-ks.cfg [email protected]::bbs --password-file=/etc/rsync.passwd ############################ ####### START RSYNC ######## ############################ # rsync -artuz --port=5678 anaconda-ks.cfg [email protected]::www --password-file=/etc/rsync.passwd ############################ ####### START RSYNC ######## ############################
1.8 rsync引數說明:
# 本地複製: rsync [選項] SRC... [DEST] # 通過遠端shell複製 下載資料:rsync [選項] [USER@]HOST:SRC...[DEST] 上傳資料:rsync [選項] SRC...[USER@]HOST:DEST # 通過rsync程序複製 下載資料: rsync [選項] [USER@]HOST::SRC...[DEST] rsync [選項] rsync:://[USER@]HOST[:PORT]/SRC...[DEST] 上傳資料: rsync [選項] SRC...[USER@]HOST::DEST rsync [選項] SRC...rsync://[USER@]HOST[:PORT]/DEST 選項: -v:顯示詳細資訊 -q:靜默模式,無錯誤資訊 -a:歸檔模式,保留檔案屬性 -r:遞迴模式 -b:如果目標路徑已經純質同名檔案,將舊的檔案重新命名為~filename,可以使用--suffix指定不同的備份字首。 --back-dir:將備份檔案儲存到指定目錄 -t:保留修改的時間屬性 -l:保留符合連結 -u:如果目標地址中的檔案比將要下載的檔案新,則不執行同步。也就是說,不會用就舊檔案覆蓋新的檔案。 -p:保留檔案許可權屬性 -H:保留硬連結 -A:保留ACL許可權 -X:保留檔案附加屬性 -o:保留檔案所有者屬性 -g:保留檔案所屬組屬性 --devices:保留裝置檔案 --specials:保留特殊檔案 -W:不做增量檢測,直接複製全部檔案 --delete:刪除那些僅在目錄路徑中存在的檔案。 -z:傳輸過程中對資料進行壓縮 --include=PATTERN 匹配不排除的檔案 --exclude=PATTERN 匹配需要排除的檔案 --progress:顯示資料傳輸的進度資訊 --partial:保留因故障未傳輸完成的檔案 --password-file=FILE:指定密碼檔案 --list-only:僅列出伺服器模組列表,需要rsync伺服器設定list=true
2. 部署sersync服務
2.1 安裝sersync服務
# tar -xvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/ # mv GNU-Linux-x86 sersync # tree sersync sersync ├── confxml.xml #配置檔案 └── sersync2 #二進位制啟動檔案 # cp confxml.xml{,.bak}
2.2 配置sersync
修改配置檔案:
<sersync> <localpath watch="/opt/tongbu"># 定義本地要同步的目錄 <remote ip="127.0.0.1" name="tongbu1"/> # 要同步到的機器,rsync server下的tongbu1模組 <!--<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="/tmp/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>
修改後:
<sersync> <localpath watch="/data/www"> <remote ip="192.168.1.9" name="www"/> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/> #開啟認證 <userDefinedPort start="true" port="5678"/><!-- port=874 --> #開啟埠 <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/usr/local/sersync/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>
2.3 多例項配置
# cp confxml.xml bbs_confxml.xml # cp confxml.xml www_confxml.xml #bbs_confxml.xml修改的配置檔案 <sersync> <localpath watch="/data/bbs"> <remote ip="192.168.1.9" name="bbs"/> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/> <userDefinedPort start="true" port="5678"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/usr/local/sersync/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> --------------------------------------------------------------------------------------------------- # www_confxml.xml修改的配置檔案: <sersync> <localpath watch="/data/www"> <remote ip="192.168.1.9" name="www"/> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/> <userDefinedPort start="true" port="5678"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/usr/local/sersync/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>
2.4 啟動sersync守護程序:
# usr/local/sersync/sersync2 -dro /usr/local/sersync/www_confxml.xml # /usr/local/sersync/sersync2 -dro /usr/local/sersync/bbs_confxml.xml 通過在/data/{bbs,www}中建立檔案,來測試是否可以進行同步。
引數說明:
-d:啟用守護程序模式
-r:在監控前,將監控目錄與遠端主機用rsync命令推送一遍
-n: 指定開啟守護執行緒的數量,預設為10個
-o:指定配置檔案,預設使用confxml.xml檔案
-m:單獨啟用其他模組,使用 -m refreshCDN 開啟重新整理CDN模組
-m:單獨啟用其他模組,使用 -m socket 開啟socket模組
-m:單獨啟用其他模組,使用 -m http 開啟http模組
不加-m引數,則預設執行同步程式
2.5 配置檔案引數說明
<?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5"> # hostip與port是針對外掛的保留欄位,對於同步功能沒有任何作用,保留預設即可; <host hostip="localhost" port="8008"></host> # 設定為true,表示開啟debug模式,會在sersync執行的控制檯列印inotify時間與rsync同步命令;對於排查錯誤可以開啟進行排查 <debug start="false"/> # 對於xfs檔案系統的使用者,需要將這個選項開啟,才能使用sersync正常工作; <fileSystem xfs="false"/> # 開啟表示對匹配的檔案進行過濾。 <filter start="false"> <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> # 設定為true後,如果刪除本地檔案也會同步刪除遠端的檔案,建議設定為false。 <delete start="true"/> # 如果為false,將不會對產生的目錄進行監控,該目錄下的子檔案也不會被監控。 <createFolder start="true"/> # createFile(監控檔案事件選項)設定為false來提高效能,減少rsync通訊;因為拷貝檔案到監控目錄會產生create事件與close_write事件,所以如果關閉create事件,只監控檔案拷貝結束時的時間close_write,同樣可以實現檔案完整同步; <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地址和模組名 <!--<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="/tmp/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/images"/> </localpath> </plugin> </head>