業內比較可靠的檔案同步方案大致有三種:rsync+inotify-tools、Openduckbill+inotify-tools,以及 rsync+sersync。前兩者基於腳本語言編寫,規範程度與執行效率相對弱一些。
sersync 以 C++ 開發,基於 boost 1.43.0、inotify API 與 rsync 指令,主要用於伺服器同步與 Web 鏡像。它能過濾 Linux 檔案系統產生的臨時檔與重複操作,因此搭配 rsync 時能省下可觀的執行時間與網路資源,更適合線上使用。
用 rsync+crontab 定時同步時:主伺服器開啟 rsync 守護行程,鏡像伺服器執行 rsync 客戶端,定時去抓資料。
用 rsync+sersync 即時同步時剛好相反:推送端執行 sersync,接收端執行 rsync 守護行程。sersync 會呼叫 rsync 指令把檔案推過去。實務上通常把 sersync 當主伺服器、rsync 當鏡像伺服器。
本文目標:把 sersync 推送端 /data 下的變動即時同步到 rsync 接收端 /data。
環境:
操作系統:CentOS 6.3 x86
rsync:CentOS 自帶 yum 源
sersync:sersync2.5_32bit
sersync 推送端:192.168.100.74
rsync 接收端:192.168.100.29
# vi /etc/sysconfig/selinux
SELINUX=disabled
# setenforce 0
# service iptables stop
1、安裝(CentOS 6.3 預設已有):
# yum install rsync -y
# yum install xinetd -y
2、啟動相依服務:
# /etc/init.d/xinetd start
# chkconfig xinetd on
3、撰寫 /etc/rsyncd.conf:
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
port = 873
address = 192.168.100.29
[data] # rsync 模組名,sersync 設定時會用到
path = /data
comment = mirror for test
ignore errors
read only = no
list = no
auth users = user
secrets file = /etc/rsync.pas # 密碼檔必須 600 權限,否則傳輸會報錯
hosts allow = *
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
4、建立同步目錄與密碼檔:
# mkdir /data
# echo "user:123456" > /etc/rsync.pas
# chmod 600 /etc/rsync.pas
# rsync --daemon --config=/etc/rsyncd.conf
注意接收端的密碼檔格式是「使用者:密碼」,而推送端只有密碼,兩邊不要寫錯。
5、重啟 xinetd 使設定生效並設為開機自啟:
# /etc/init.d/xinetd restart
# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local
1、下載(64 位平台請改用 64 位版本):
# wget https://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz
2、建立目錄結構並解開:
# mkdir /usr/local/sersync
# mkdir /usr/local/sersync/conf
# mkdir /usr/local/sersync/bin
# mkdir /usr/local/sersync/log
# tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz
# cd GNU-Linux-x86/
# cp confxml.xml /usr/local/sersync/conf
# cp sersync2 /usr/local/sersync/bin
3、建立 rsync 連線密碼檔(只有密碼,不含使用者):
# echo "123456" >/etc/rsync.pas
# chmod 600 /etc/rsync.pas
4、編輯 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 改為 true 並填寫正則表達式
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
</filter>
<inotify>
<delete start="true" />
<createFolder start="true" />
<createFile start="true" />
<closeWrite start="true" />
<moveFrom start="true" />
<moveTo start="true" />
<attrib start="true" />
<modify start="true" />
</inotify>
<sersync>
<localpath watch="/data">
<remote ip="192.168.100.29" name="data" /> # name 就是 rsyncd.conf 裡的模組名
</localpath>
<rsync>
<commonParams params="-auvzP" />
<auth start="true" users="user" passwordfile="/etc/rsync.pas" />
<userDefinedPort start="false" port="874" />
<timeout start="true" time="100" />
<ssh start="false" />
</rsync>
<failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60" />
<crontab start="false" schedule="600"></crontab>
<plugin start="false" name="command" />
</sersync>
</head>
幾個實務上常用的開關:failLog 會記錄同步失敗的項目並定期重跑,正式環境建議開啟;crontab 是 rsync+sersync 之外的補充,可定期做一次全量比對;plugin 則能在同步後觸發自訂腳本,例如快取刷新。
5、建立同步目錄並設定環境變數:
# mkdir /data
# echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
# source /etc/profile
6、啟動:
# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
重啟的寫法:
# killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
設為開機自啟:
# echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local
推送端:
# cd /data
# touch 1 2 3 4 5
# echo "test sersync" > 1
接收端:
# cd /data
# ls
1 2 3 4 5
# cat 1
test sersync
同步成功。
最後提一個實務上很重要的細節:如果有人在接收端本地新增或修改了同步目錄下的檔案,後續 sersync 推送時不會覆蓋那些檔案——sersync 只推送「來源端發生變動」的項目。這既是優點也是盲點:它避免了誤刪,但也代表接收端若被獨立修改,兩邊會悄悄分岔。因此接收端目錄建議設為唯讀或收回寫入權限。
-------------大功告成----------------
作者:51cto博客 一路向北