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
root@192.168.169.113:/etc
# scp /etc/ csync.key
root@192.168.169.114:/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;

# }

 

3csync2相关命令介绍
# 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