csync2+sqlite實現數據的高效實時的增量備份
前言,測試時共兩臺機器:
192.168.169.112 bbs1.cheabc.com
192.168.169.113 bbs2.cheabc.com
環境:centos5 +php+ningx+mysql
一、安裝配置csync2
1、相關軟件包下載
wget ftp://ftp.pbone.net/mirror/download.fedora.redhat.com/pub/fedora/epel/5/x86_64/librsync-0.9.7-13.el5.i386.rpm
wget ftp://ftp.pbone.net/mirror/ftp.freshrpms.net/pub/freshrpms/pub/dag/redhat/el5/en/i386/RPMS.dag/libtasn1-0.3.9-1.el5.rf.i386.rpm
wget ftp://ftp.pbone.net/mirror/ftp.freshrpms.net/pub/freshrpms/pub/freshrpms/redhat/testing/EL5/cluster/i386/sqlite2-2.8.17-1.el5/sqlite2-2.8.17-1.el5.i386.rpm
wget ftp://ftp.pbone.net/mirror/ftp.freshrpms.net/pub/freshrpms/pub/freshrpms/redhat/testing/EL5/cluster/i386/csync2-1.34-4.el5/csync2-1.33-4.el5.i386.rpm
wget
ftp://ftp.pbone.net/mirror/ftp.freshrpms.net/pub/freshrpms/pub/dag/redhat/el4/en/i386/RPMS.dag/inotify-tools-3.13-1.el4.rf.i386.rpm
其它下載
ftp://ftp.gnupg.org/gcrypt/libgpg-error/libgpg-error-1.7.tar.gz
ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-1.4.4.tar.gz
ftp://ftp.gnutls.org/pub/gnutls/libtasn1/libtasn1-2.1.tar.gz
http://www.sqlite.org/sqlite-2.8.17.tar.gz
http://internode.dl.sourceforge.net/sourceforge/librsync/librsync-0.9.7.tar.gz
ftp://ftp.gnu.org/pub/gnu/gnutls/gnutls-2.6.6.tar.bz2
http://oss.linbit.com/csync2/csync2-1.34.tar.gz
http://jaist.dl.sourceforge.net/sourceforge/inotify-tools/inotify-tools-3.13.tar.gz
2、軟件安裝
Rpm 正常安裝
echo "csync2 30865/tcp" >> /etc/services 添加爲服務
1)# vi /etc/xinetd.d/csync2
service csync2
{
disable = no
protocol = tcp
socket_type = stream
wait = no
user = root
server = /usr/sbin/csync2
server_args = -i
}
# chkconfig xinetd on
# service xinetd start
# csync2 -k /etc/csync2.bbs.key //生成密解
# scp /etc/ csync.key [email protected]:/etc
# scp /etc/ csync.key [email protected]:/etc
【注】分別將key文件複製到集羣的另外一臺機器上。
# vi /etc/hosts //配置主機名,分別添加到2臺機器的hosts文件中.
192.168.169.112 bbs1.cheabc.com
192.168.169.113 bbs2.cheabc.com
2) vi /etc/csync2.cfg
# Csync2 Example Configuration File
# ---------------------------------
#
# Please read the documentation:
# http://oss.linbit.com/csync2/paper.pdf
nossl * *;
group mygroup
{
auto first;
# host host1 host2 (host3);
host bbs1.cheabc.com bbs2.cheabc.com; //定義組成員
# host host4@host4-eth2;
key /etc/csync.key;
# include /etc/hosts;
include /data0/htdocs/blog; //需要同步的目錄
exclude *.swp; //需要排除的目錄。
exclude /data0/htdocs/www/data/sessions; //需要排除同步的緩存目錄。
# include /data0/htdocs/www/uc/data/avatar;
# include %homedir%/bob;
# exclude %homedir%/bob/temp;
# exclude *~ .*; //排除同步.開頭的文件
# action
# {
# pattern /etc/apache/httpd.conf;
# pattern /etc/apache/sites-available/*;
# exec "/usr/sbin/apache2ctl graceful"; //可以匹配相關配置文件,並執行重啓腳本
# logfile "/var/log/csync2_action.log";
# do-local;
# }
backup-directory /usr/local/webserver/backup; //防錯備份目錄,根據自己的需求設置。
backup-generations 0;
#auto none;
}
#
# prefix homedir
# {
# on host[12]: /export/users;
# on *: /home;
# }
3、csync2相關命令介紹
# csync2 –vvv –T 測試csync配置是否正確,可以看到相關SQL執行過程.
# csync2 –xv 執行同步命令
# csync2 –xvvv 執行同步命令,並顯示出詳細的信息.
二、利用inotify實現數據的實時同步更新.
Rpm –ivh inotify-tools-3.13-1.el4.rf.i386.rpm
2、配置inotify觸發同步腳本.
# cd /usr/local/sbin
# vi csync2_bbs.sh
#!/bin/bash
src=/data0/htdocs/blog
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' \
--exclude "\*.swp" \
-e close_write,modify,delete,create,attrib \
${src} \
| while read file
do
#csync2 -x >/dev/null 2>&1
csync2 -xv
echo "${src} was csynced....."
done
# chmod a+x csync2_bbs.sh
# ./csync2_bbs.sh //分別在三臺機器上創建並執行此腳本,並將其加入/etc/rc.local中。
3.測試同步是否正常
在三臺機器中的任一臺創建或者刪除一個文件,然後查看其它機器是否創建或刪除。
如果遇到問題就用csync2 –xv命令手動調試並,根據錯誤信息作調整。
三、相關參考及注意事項.
1、相關參考資料:
http://oss.linbit.com/csync2/paper.pdf
http://zhenhuiliang.blogspot.com/2006/04/csync2-is-so-cool.html
http://bbs.linuxtone.org/viewthread.php?action=printable&tid=2707