Sersync實現觸發式文件同步php
序言:若是咱們後端有多臺網站服務器或者文件服務器,並且沒有好的文件同步機制,那麼當咱們升級程序或者更新文件的時候,就須要每臺服務器或者目錄都要更新,這樣很容易出問題,並很容易致使兩邊的文件不一致,從而出現不少莫民其妙的問題。所以咱們須要使用好的文件同步方式來實現幾個後端服務器文件的同步,目前普遍採用的方案是使用rsync+inotify的方式來實現文件的觸發更新。原理是採用inotify來對文件進行監控,當監控到文件有文件發生改變的時候,就會調用rsync實現觸發式實時同步!本文就來詳細介紹金山的一個居於inotify+rsync進行二次開發實現文件同步的小工具sersync,可以很方便的實現文件觸發式同步html
1、inotify簡介node
Inotify 是基於inode級別的文件系統監控技術,是一種強大的、細粒度的、異步的機制,它知足各類各樣的文件監控須要,不只限於安全和性能,內核要求2.6.13以上,inotify能監控很是多的文件系統事件,經過監控這些事件來監控文件是否發生變動,而後經過rsync來更新發生變動的文件,Inotify 能夠監視的文件系統事件包括:c++
· IN_ACCESS,即文件被訪問算法
· IN_MODIFY,文件被 writeexpress
· IN_ATTRIB,文件屬性被修改,如 chmod、chown、touch 等後端
· IN_CLOSE_WRITE,可寫文件被 close安全
· IN_CLOSE_NOWRITE,不可寫文件被 closebash
· IN_OPEN,文件被 open服務器
· IN_MOVED_FROM,文件被移走,如 mv
· IN_MOVED_TO,文件被移來,如 mv、cp
· IN_CREATE,建立新文件
· IN_DELETE,文件被刪除,如 rm
· IN_DELETE_SELF,自刪除,即一個可執行文件在執行時刪除本身
· IN_MOVE_SELF,自移動,即一個可執行文件在執行時移動本身
· IN_UNMOUNT,宿主文件系統被 umount
· IN_CLOSE,文件被關閉,等同於(IN_CLOSE_WRITE |IN_CLOSE_NOWRITE)
· IN_MOVE,文件被移動,等同於(IN_MOVED_FROM | IN_MOVED_TO)
備註:上面的文件也包括目錄。
2、Rsync簡介
rsync,remote synchronize顧名思意就知道它是一款實現遠程同步功能的軟件,它在同步文件的同時,能夠保持原來文件的權限、時間、軟硬連接等附加信息。rsync是用 「rsync 算法」提供了一個客戶機和遠程文件服務器的文件同步的快速方法,並且能夠經過ssh方式來傳輸文件,這樣其保密性也很是好,另外它仍是免費的軟件。
rsync 包括以下的一些特性:
能更新整個目錄和樹和文件系統;
有選擇性的保持符號鏈鏈、硬連接、文件屬於、權限、設備以及時間等;
對於安裝來講,無任何特殊權限要求;
對於多個文件來講,內部流水線減小文件等待的延時;
能用rsh、ssh 或直接端口作爲傳輸入端口;
支持匿名rsync 同步文件,是理想的鏡像工具;
3、sersync簡介
sersync利用inotify與rsync對服務器進行實時同步,其中inotify用於監控文件系統事件,rsync是目前普遍使用的同步算法,其優勢是隻對文件不一樣的部分進行操做,因此其優點大大超過使用掛接文件系統的方式進行鏡像同步。由金山的周洋開發完成,是目前使用較多的文件同步工具之一。該工具和其餘的工具相比有以下優勢:
sersync是使用c++編寫,因爲只同步發生更改的文件,所以比其餘同步工具更節約時間、帶寬;
安裝方便、配置簡單;
使用多線程進行同步,可以保證多個服務器實時保持同步狀態;
自帶出錯處理機制,經過失敗隊列對出錯的文件從新出錯,若是仍舊失敗,則每10個小時對同步失敗的文件從新同步;
自帶crontab功能,只需在xml配置文件中開啓,便可按您的要求,隔一段時間總體同步一次;
自帶socket與http協議擴展,你能夠方便的進行二次開發;
4、 sersync實現觸發式文件同步實戰
1. 服務器文件同步圖
2. 從服務器的安裝配置
a) 安裝rsync
yum -y install rsync
b) 配置rsync
Rsync配置文件的配置
cat /etc/rsyncd.conf
# Rsync configuration file
uid = root
gid = root
port = 873
max connections = 20000
use chroot = yes
timeout = 200
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log format = %t %a %m %f %b
auth users = root
secrets file = /etc/rsyncd.secret
[test]
path = /data/test/
comment = "test directory file"
list = yes
read only = no
ignore errors = yes
hosts allow = 192.168.3.205
hosts deny = *
Rsync密碼文件的配置
cat /etc/rsyncd.secret
root:abc123@#$
chmod 600 /etc/rsyncd.secret
須要設置密碼文件爲600權限,否則同步的時候會報password file must not be other-accessible錯誤!
c) Rsync的啓動
Rsync的啓動有兩種方式,兩種方式哪種均可以,讀者能夠自行選擇:
一種是採用daemon的方式啓動:
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
另一種是xinetd集成啓動方式:
chkconfig xined on
chkconfig rsync on
/etc/init.d/xinetd start
d) 驗證rsync是否啓動
查看進程和端口是否存在
[root@test_machine test]# ps aux | greprsync
root 3275 0.0 0.0 65516 472? Ss 13:44 0:00/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
root 3306 0.0 0.1 71812 936? S 14:49 0:00/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
[root@test_machine test]# netstat -lnp |grep rsync
tcp 0 00.0.0.0:873 0.0.0.0:* LISTEN 3275/rsync
tcp 0 0:::873 :::* LISTEN 3275/rsync
3. 主服務器的安裝配置
u 安裝rsync
yum -y install rsync
u 安裝sersync
下載安裝文件
wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
解壓並拷貝到安裝目錄
tar xzvfsersync2.5_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync
u 配置sersync
Sersync的配置
cat confxml.xml
<?xml version="1.0"encoding="ISO-8859-1"?>
<head version="2.5">
<hosthostip="localhost" port="8008"></host>
<debugstart="false"/>
<fileSystemxfs="false"/>
<filterstart="false">
<excludeexpression="(.*)\.svn"></exclude>
<excludeexpression="(.*)\.gz"></exclude>
<excludeexpression="^info/*"></exclude>
<excludeexpression="^static/*"></exclude>
</filter>
<inotify>
<deletestart="true"/>
<createFolderstart="true"/>
<createFilestart="false"/>
<closeWritestart="true"/>
<moveFromstart="true"/>
<moveTostart="true"/>
<attribstart="true"/>
<modifystart="true"/>
</inotify>
<sersync>
<localpathwatch="/data/test">
<!—-設置監控的目錄-->
<remoteip="192.168.3.203" name="test"/>
<!—設置從服務器的IP-->
<!--<remoteip="192.168.8.39" name="tongbu"/>-->
<!--<remoteip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParamsparams="-artuz"/>
<authstart="true" users="root"passwordfile="/etc/rsyncd.secret"/>
<!—-設置同步的用戶名和密碼文件-->
<userDefinedPortstart="true" port="873"/><!-- port=874 -->
<!—-設置rsync的端口,要和從那邊開啓的端口一致-->
<timeoutstart="false" time="100"/><!-- timeout=100 -->
<sshstart="false"/>
</rsync>
<failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->
<crontabstart="true" schedule="300"><!--600mins-->
<!—-設置300分鐘所有同步一次-->
<crontabfilterstart="false">
<excludeexpression="*.php"></exclude>
<excludeexpression="info/*"></exclude>
</crontabfilter>
</crontab>
<pluginstart="false" name="command"/>
</sersync>
<pluginname="command">
<paramprefix="/bin/sh" suffix=""ignoreError="true"/> <!--prefix /opt/tongbu/mmm.shsuffix-->
<filterstart="false">
<includeexpression="(.*)\.php"/>
<includeexpression="(.*)\.sh"/>
</filter>
</plugin>
</head>
密碼文件的配置
cat /etc/rsyncd.secret
abc123@#$
主服務器的密碼配置文件不須要用戶,若是添加用戶的話同步的時候會報
rsync error: error starting client-server protocol (code5) at main.c(1296) [sender=2.6.8]錯誤
u 啓動sersync
/usr/local/sersync/sersync2 -d -r -o/usr/local/sersync/confxml.xml
將上面的命令添加進/etc/rc.local,之後重啓系統之後才能正常同步
u 腳本監控sersync
由於有的時候sersync腳本會自動關掉,所以須要寫一個腳本自動的去檢測該進程是否存在,不存在就啓動,腳本內容以下:
cat /var/script/check_sersync.sh
#!/bin/bash
#Purpose: Check sersync whether it is alive
#Author: Carl Zhang
SERSYNC="/usr/local/sersync/sersync2"
CONF_FILE="/usr/local/sersync/confxml.xml"
STATUS=$(ps aux |grep 'sersync2'|grep -v'grep'|wc -l)
if [ $STATUS -eq 0 ];
then
$SERSYNC-d -r -o $CONF_FILE &
else
exit0;
fi
腳本寫好之後,添加到計劃任務中去
*/5 * * * * /var/script/check_sersync.sh> /dev/null 2>&1
總結:經過以上幾步之後,你從服務器的/data/test就可以從主服務器實時更新,升級的時候只升級主服務器的文件,從服務器也會自動同步過去,減小你的工做量以及出錯的機率!
轉自:http://blog.chinaunix.net/uid-20639775-id-3011124.html