標籤:style blog http color 使用 os
一個網站,除了有資料庫,還有很多別的檔案,比如使用者上傳的圖片,你的網站代碼之類,光有資料庫而沒有這些檔案你的網站也沒法跑起來。
和資料庫比起來,網站檔案要大得多,而且檔案數目也多得多,要做到即時同步必須考慮“增量檔案同步”,一般傳統的方法是使用 inotify + rsync 寫指令碼來操作。
我們這裡採用 Sersync (https://code.google.com/p/sersync/) 來做網站資料同步 。
配置 Slave 上的 WWW 同步
採用 Rsync 做資料同步
uid=rootgid=rootuse chroot=nomax connections=10pid file=/var/run/rsyncd.pidlock file=/var/run/rsync.locklog file=/var/log/rsyncd.logmunge symlinks = nohosts allow=這裡填寫 Master 的 IP 位址hosts deny=*list=falseread only=noignore errors[WWW]path=/var/WWW ## 網站資料所在的位置
啟動 rsync 服務: rsync –daemon
啟動 Master 上的 Sersync2 做資料同步
同步目錄的配置 www.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"> <exclude expression="^BZFSHOP/Asset/*"></exclude> <exclude expression="^BZFSHOP/Runtime/*"></exclude> <exclude expression="^BZFSHOP/Trash/*"></exclude> <exclude expression="^BZFSHOP/Data/cache/*"></exclude> <exclude expression="^BZFSHOP/Data/temp/*"></exclude> </filter> <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="true"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="false"/> </inotify> <sersync> <localpath watch="/var/WWW"> <remote ip="Slave的IP" name="WWW"/> </localpath> <rsync> <commonParams params="-artu"/> <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/www_rsync_fail_log.sh" timeToExecute="3"/><!--default every 3mins 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> </head>
開機檔案監控同步
## 第一次啟動加上 -r 參數做首次同步,之後就不要再加 -r 參數了./sersync2 -n 2 -d -r -o www.xml
後記
完整的對 資料庫 和 所有檔案 做了即時同步之後,我們的業務就有了保障。一旦主機房出現嚴重問題,我們可以使用備份資料立即繼續中斷的業務,最重要的是,業務資料幾乎沒有任何的丟失。
不像之前,每天一個備份,如果發生嚴重故障,只能從“昨天”開始了,中間資料丟失會帶來巨大的麻煩。
如果需要更加安全,可以考慮 3 備份或者 N 備份,在 N 個城市同時做即時備份資料,一旦發生多點災難一樣可以迅速恢複業務,當然需要的成本也更高。