首页 > BSD/linux > FreeBSD下使用csync2分布同步

FreeBSD下使用csync2分布同步

2010年7月21日 16hot 发表评论 阅读评论

1、简述

最近因项目需要,找了相关的开源同步解决方案。由于采用FreeBSD系统,有些Linux专用的程序,是不能使用的了。即使可以使用,配置起来太 麻烦,需要Linux虚拟环境。这样代价太高。rsync,mrsync,等都看了遍,最终选择了csync2。

官方网站: http://oss.linbit.com/csync2/

使用手册: http://oss.linbit.com/csync2/paper.pdf

官方发布的正式版是1.34,需要sqlite2的支持。如果想用sqlite3,就需要从他们svn更新最新源码回来。SVN里的版本已经支持 sqlite3了。

使用SVN获取最新源码:

$ svn co  http://svn.linbit.com/csync2/trunk csync2-trunk

由于SVN的源码文件里,没有configure文件,需要自己手工生成。源码目录里有个autogen.sh文件,需要修改下,才能执行。

$ cd csync2-trunk
$ vi autogen.sh

aclocal-1.7
autoheader
automake-1.7 –add-missing –copy
autoconf

改为

aclocal-1.9
autoheader
automake-1.9 –add-missing –copy
autoconf

具体版本号,视里面已经安装的aclocal和automake版本来设置。

然后执行:

$ autogen.sh

执行过程中,会提示一些信息,基本不用管它。这时候生成configure文件了。不过如果现在就执行./configure,还会报4437行错误。将那行注释掉就好了。

#AM_PATH_LIBGNUTLS(1.0.0,0 ,  AC_MSG_ERROR([[gnutls not found; install gnutls, gnutls-openssl and libtasn1 packages for your system or run configure with --disable-gnutls]]) )

2、安装

在安装之前先安装相关的依赖包。下面时我用ports安装csync2-1.34时自动安装上的依赖包:

$pkg_info -r csync2-1.34_8
Information for csync2-1.34_8:

Depends on:
Dependency: pkg-config-0.23_1
Dependency: libtasn1-2.6
Dependency: sqlite-2.8.17_1
Dependency: libiconv-1.13.1_1
Dependency: gettext-0.18_1
Dependency: popt-1.14_1
Dependency: librsync-0.9.7_2
Dependency: libgpg-error-1.7_1
Dependency: libgcrypt-1.4.5
Dependency: gnutls-2.8.6_1

由于SVN版本使用的是sqlite3,所以还需要安装sqlite3-3.6.23.1_1。这些软件包,在/usr/ports 中安装就可以了。

# setenv LOCALBASE /usr/local/
# env CPPFLAGS=”-I${LOCALBASE}/include” LDFLAGS=”-L${LOCALBASE}/lib” LIBGNUTLS_CONFIG=”${LOCALBASE}/bin/pkg-config gnutls” ./configure
# make
# make install

没有什么意外的话,就OK了。如果提示缺什么软件包,到ports里安装。

3、配置

下面是源码包里带的配置文件范例:

% cat csync2.cfg
# Csync2 Example Configuration File
# ———————————
#
# Please read the documentation:
# http://oss.linbit.com/csync2/paper.pdf

# group mygroup
# {
#     host host1 host2 (host3);
#     host host4@host4-eth2;
#
#     key /etc/csync2.key_mygroup;
#
#     #
#     # WARNING:
#     # You CANNOT use paths containing a symlink
#     # component in include/exclude options!
#     #
#     # Here is a real-life example:
#     # Suppose you have some 64bit Linux systems
#     # and /usr/lib/ocf is what you want to keep
#    # in sync. On 64bit Linux systems, /usr/lib
#     # is usually a symlink to /usr/lib64.
#     # This does not work:
#     #   include /usr/lib/ocf;
#     # But this does work:
#     #   include /usr/lib64/ocf;
#     #
#
#     include /etc/apache;
#     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;
#        # you can use do-local-only if the execution
#        # should be done locally only
#        # do-local-only;
#     }
#
#     # The backup-directory needs to be created first!
#     backup-directory /var/backups/csync2;
#     backup-generations 3;
#
#     auto none;
# }
#
# prefix homedir
# {
#     on host[12]: /export/users;
#     on *:        /home;
# }

下面是我的配置:

# vi /usr/local/etc/csync2.cfg

# Csync2 Example Configuration File
# ———————————
#
# Please read the documentation:
# http://oss.linbit.com/csync2/paper.pdf

group mygroup
{
host fb1.isyi.com fb50.isyi.com fb51.isyi.com;

key /opt/etc/isyi.key;
include /opt/www/test;

action
{
pattern /opt/www/test/*;
exec “echo ok >> /tmp/csync2.log”;
}
auto younger;
}

配置好后,还需要修改/etc/hosts文件,将主机名和IP设置好。这个配置,三台服务器都需要配置一样。

192.168.56.1  fb1.isyi.com
192.168.56.50 fb50.isyi.com
192.168.56.51 fb51.isyi.com

生成key文件,这个key文件,需要复制到三台服务器,保证一样,否则不能同步:

# cd /opt/etc/
# csync2 -k isyi.key

生成CERT文件,需要在三台服务器分别生成:

setenv etc_path “/usr/local/etc”
openssl genrsa -out ${etc_path}/csync2_ssl_key.pem 1024
openssl req -batch -new -key ${etc_path}/csync2_ssl_key.pem -out ${etc_path}/csync2_ssl_cert.csr
openssl x509 -req -days 2000 -in ${etc_path}/csync2_ssl_cert.csr -signkey ${etc_path}/csync2_ssl_key.pem -out ${etc_path}/csync2_ssl_cert.pem

4、启动服务

分别在三台服务器执行:

# /usr/local/sbin/csync2 -ii -vv&

5、执行同步

在主服务器,需要向其他服务器同步的服务器执行:

# /usr/local/sbin/csync2 -xvv

6、附录

6.1、csync2的help

% ./csync2

csync2 SNAPSHOT – cluster synchronization tool, 2nd generation
LINBIT Information Technologies GmbH <http://www.linbit.com>
Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
This program is free software under the terms of the GNU GPL.

Usage: ./csync2 [-v..] [-C config-name] \
[-D database-dir] [-N hostname] [-p port] ..

With file parameters:
-h [-r] file..      Add (recursive) hints for check to db
-c [-r] file..      Check files and maybe add to dirty db
-u [-d] [-r] file.. Updates files if listed in dirty db
-o [-r] file..      Create list of files in compare-mode
-f [-r] file..      Force this file in sync (resolve conflict)
-m file..       Mark files in database as dirty

Simple mode:
-x [-d] [[-r] file..]   Run checks for all given files and update
remote hosts.

Without file parameters:
-c  Check all hints in db and eventually mark files as dirty
-u [-d] Update (transfer dirty files to peers and mark as clear)

-H  List all pending hints from status db
-L  List all file-entries from status db
-M  List all dirty files from status db

-S myname peername  List file-entries from status db for this
synchronization pair.

-T              Test if everything is in sync with all peers.

-T filename         Test if this file is in sync with all peers.

-T myname peername  Test if this synchronization pair is in sync.

-T myname peer file Test only this file in this sync pair.

-TT As -T, but print the unified diffs.

The modes -H, -L, -M and -S return 2 if the requested db is empty.
The mode -T returns 2 if both hosts are in sync.

-i  Run in inetd server mode.
-ii Run in stand-alone server mode.
-iii    Run in stand-alone server mode (one connect only).

-R  Remove files from database which do not match config entries.

Modifiers:
-r  Recursive operation over subdirectories
-d  Dry-run on all remote update operations

-B  Do not block everything into big SQL transactions. This
slows down csync2 but allows multiple csync2 processes to
access the database at the same time. Use e.g. when slow
lines are used or huge files are transferred.

-A  Open database in asynchronous mode. This will cause data
corruption if the operating system crashes or the computer
loses power.

-I  Init-run. Use with care and read the documentation first!
You usually do not need this option unless you are
initializing groups with really large file lists.

-X  Also add removals to dirty db when doing a -TI run.
-U  Don’t mark all other peers as dirty when doing a -TI run.

-G Group1,Group2,Group3,…
Only use this groups from config-file.

-P peer1,peer1,…
Only update this peers (still mark all as dirty).
Only show files for this peers in -o (compare) mode.

-F  Add new entries to dirty database with force flag set.

-t  Print timestamps to debug output (e.g. for profiling).

-s filename
Print timestamps also to this file.

-W fd   Write a list of directories in which relevant files can be
found to the specified file descriptor (when doing a -c run).
The directory names in this output are zero-terminated.

Creating key file:
./csync2 -k filename

Csync2 will refuse to do anything when a /var/etc/csync2.lock file is found.

分类: BSD/linux 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
订阅评论
1+3= (必填)

Spam Protection by WP-SpamFree