上个实验实现了单向同步,这个实验的目标是实现双向同步
csync2可以支持双向同步




环境:RHEL6.0 x86-64
主机:momo 192.168.0.178
    desktop115 192.168.0.115

软件下载:
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


实验:


1.安装软件 (以下步骤在每台主机上操作)

[root@momo ~]# lftp 192.168.0.254
lftp 192.168.0.254:~> mget pub/docs/csync2/*
lftp 192.168.0.254:/> quit
[root@momo ~]# yum install -y gcc gcc-c++ libgpg-error libgpg-error-devel libgcrypt libgcrypt-devel    
#下面在安装源码时会涉及到了一些依赖性
[root@momo ~]# tar zxf libtasn1-2.1.tar.gz
[root@momo ~]# cd libtasn1-2.1
[root@momo libtasn1-2.1]# ./configure && make && make install
[root@momo libtasn1-2.1]# cd
[root@momo ~]# tar zxf sqlite-2.8.17.tar.gz
[root@momo ~]# cd sqlite-2.8.17
[root@momo sqlite-2.8.17]# ./configure && make && make install
[root@momo sqlite-2.8.17]# cd
[root@momo ~]# tar zxf librsync-0.9.7.tar.gz
[root@momo ~]# cd librsync-0.9.7
[root@momo librsync-0.9.7]# ./configure && make && make install
[root@momo librsync-0.9.7]# cd
[root@momo ~]# tar jxf gnutls-2.6.6.tar.bz2
[root@momo ~]# cd gnutls-2.6.6
[root@momo gnutls-2.6.6]# ./configure && make && make install    #默认安装到/usr/local/lib
[root@momo gnutls-2.6.6]# cd
[root@momo ~]# echo "/usr/local/lib" >> /etc/ld.so.conf
[root@momo ~]# ldconfig
[root@momo ~]# tar zxf inotify-tools-3.13.tar.gz
[root@momo ~]# cd inotify-tools-3.13
[root@momo inotify-tools-3.13]# ./configure && make && make install
[root@momo inotify-tools-3.13]# cd
[root@momo ~]# yum install byacc flex -y
[root@momo ~]# tar zxf csync2-1.34.tar.gz
[root@momo ~]# cd csync2-1.34
[root@momo csync2-1.34]# ./configure && make && make install
[root@momo csync2-1.34]# make cert        #第二台机子从这步开始不做,直接拷贝本机的
[root@momo csync2-1.34]# csync2 -k /etc/csync2.key    #生成key,要在此主机上不停操作,以让其顺利产生key文件
[root@momo csync2-1.34]# cd
[root@momo ~]# cat /etc/csync2.key     #查看生成的key文件
FeXdtSkshXKhauyg2QvfUh0yXZ7Oy35Nz2hfPmFlGBjM4DTIuYf3bBiQppp5HXgg
[root@momo ~]# scp /etc/csync2* 192.168.0.115:/etc/    #将所有拷贝到第二台机子上


2. 配置csync2
[root@momo ~]# ldconfig /usr/local/lib
[root@momo ~]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lib
[root@momo ~]# yum install xinetd -y
[root@momo ~]# cd /etc/xinetd.d/
[root@momo xinetd.d]# cat cvs
# default: off
# description: The CVS service can record the history of your source \
#              files. CVS stores all the versions of a file in a single \
#              file in a clever way that only stores the differences \
#              between versions.
service cvspserver
{
    disable            = yes
    port            = 2401
    socket_type        = stream
    protocol        = tcp
    wait            = no
    user            = root
    passenv            = PATH
    server            = /usr/bin/cvs
    env            = HOME=/var/cvs
    server_args        = -f --allow-root=/var/cvs pserver
#    bind            = 127.0.0.1
}
[root@momo xinetd.d]# cp cvs csync2
[root@momo xinetd.d]# vim csync2
service csync2
{
        disable                 = no
        port                    = 30865
        socket_type             = stream
        protocol                = tcp
        flags                   = REUSE
        wait                    = no
        user                    = root
        group                   = root
        server                  = /usr/local/sbin/csync2
        server_args             = -i
}
[root@momo xinetd.d]# which csync2
/usr/local/sbin/csync2
[root@momo xinetd.d]# vi /etc/services
csync2          30865/tcp
[root@momo xinetd.d]# vi /etc/csync2.cfg
group mygroup
 {
        host momo.example.com desktop115.example.com;    #每台主机完整的主机名,配置另一台机器时反过来写;我做实验时,并没有反过来,也可以
        key /etc/csync2.key;    #需要同步的目录
        include /opt/rsync;
        exclude *~ .*;              #排除以 “.”开头的文件
        #backup-directory /var/csync2;  #防错备份目录,若需要可根据自己的需求设置,每台主机上都要有;在此可省
        #backup-generations 3;
        auto younger;           #同步以最新的文件为标准更新
 }
[root@momo xinetd.d]# vim /etc/hosts
192.168.0.115 desktop115.example.com
192.168.0.178 momo.example.com
-------------------------------------
[root@desktop115 ~]# ldconfig /usr/local/lib
[root@desktop115 ~]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lib
[root@desktop115 ~]# vi /etc/hosts
192.168.0.115 desktop115.example.com
192.168.0.178 momo.example.com
-------------------------------------
[root@momo xinetd.d]# scp /etc/csync2.cfg 192.168.0.115:/etc/
root@192.168.0.115's password:
csync2.cfg                                    100%  737     0.7KB/s   00:00    
---------------------------------------------------------------------------
[root@desktop115 ~]# cd /etc/
[root@desktop115 etc]# ll csync2.cfg
-rw-r--r-- 1 root root 737 May 19 09:53 csync2.cfg
[root@desktop115 etc]# cat csync2.cfg
 group mygroup
 {
     host momo.example.com desktop115.example.com;
     key /etc/csync2.key;
         include /opt/rsync;
     exclude *~ .*;
     auto younger;
 }
[root@desktop115 etc]# ll -d /opt/rsync/
drwxr-xr-x 4 root root 4096 May 13 15:58 /opt/rsync/
[root@desktop115 etc]# vi /etc/services
csync2          30865/tcp
---------------------------------------------------------------------------           
[root@momo xinetd.d]# scp csync2 192.168.0.115:/etc/xinetd.d/
root@192.168.0.115's password:
csync2                                        100%  206     0.2KB/s   00:00    
------------------------------------------------------------------------------
[root@desktop115 etc]# vi /etc/xinetd.d/csync2
 service csync2
{
        disable                 = no
        port                    = 30865
        socket_type             = stream
        protocol                = tcp
        flags                   = REUSE
        wait                    = no
        user                    = root
        group                   = root
        server                  = /usr/local/sbin/csync2
        server_args             = -i
}
------------------------------------------------------------------------------
[root@momo xinetd.d]# /etc/init.d/xinetd start
Starting xinetd:                                           [  OK  ]
[root@momo xinetd.d]# netstat -antlp      
tcp        0      0 :::30865                    :::*                        LISTEN      3327/xinetd
[root@momo rsync]# date
Sat May 19 09:59:04 CST 2012
------------------------------------------------------------------------------
[root@desktop115 etc]# /etc/init.d/xinetd start
Starting xinetd:                                           [  OK  ]
[root@desktop115 etc]# netstat -antlp             
tcp        0      0 :::30865                    :::*                        LISTEN      1223/xinetd     
[root@desktop115 rsync]# date
Sat May 19 09:59:07 CST 2012
------------------------------------------------------------------------------
[root@momo xinetd.d]# cd /opt/rsync/
[root@momo rsync]# ls
config-2.6.32-71.el6.x86_64  initramfs-2.6.32-71.el6.x86_64.img
file                         vmlinuz-2.6.32-71.el6.x86_64
[root@momo rsync]# touch zb
[root@momo rsync]# vim zb
[root@momo rsync]# cat zb
dnxjsndxjesncjndj
[root@momo rsync]# csync2 -vvv -T    #测试csync配置是否正确,可以看到相关SQL执行过程.
................................
Finished with 0 errors.
[root@momo rsync]# csync2 -xv    #执行同步命令
...............................
Finished with 0 errors.
------------------------------------------------------------------------------
[root@desktop115 etc]# cd /opt/rsync/
[root@desktop115 rsync]# ls
config-2.6.32-71.el6.x86_64  initramfs-2.6.32-71.el6.x86_64.img
file                         vmlinuz-2.6.32-71.el6.x86_64
[root@desktop115 rsync]# rm -rf *
[root@desktop115 rsync]# csync2 -vvv -T    #测试csync配置是否正确,可以看到相关SQL执行过程.
...............................
Finished with 0 errors.
[root@desktop115 rsync]# csync2 -xv     #执行同步命令
...............................
Finished with 0 errors.
[root@desktop115 rsync]# ls
------------------------------------------------------------------------------
[root@momo rsync]# ls
[root@momo xinetd.d]# cd


3.配置inotify触发脚本
[root@momo ~]# vi csync2.sh
#!/bin/bash
src=/opt/rsync
/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
done
[root@momo ~]# chmod +x csync2.sh
[root@momo ~]# mv csync2.sh /usr/local/sbin/
[root@momo ~]# scp /usr/local/sbin/csync2.sh 192.168.0.115:/usr/local/sbin/
root@192.168.0.115's password:
csync2.sh                                     100%  234     0.2KB/s   00:00  
[root@momo ~]# csync2.sh &    #打入后台
[1] 3401

------------------------------------------------------------------------------
[root@desktop115 rsync]# ll /usr/local/sbin/csync2.sh
-rwxr-xr-x 1 root root 234 May 19 10:04 /usr/local/sbin/csync2.sh
[root@desktop115 rsync]# cd
[root@desktop115 ~]# csync2.sh &    #打入后台
[1] 1264
[root@desktop115 ~]# cd /opt/rsync/
[root@desktop115 rsync]# ls
[root@desktop115 rsync]# touch file
[root@desktop115 rsync]# echo hello >file
[root@desktop115 rsync]# cat file
hello
------------------------------------------------------------------------------
[root@momo ~]# cd /opt/rsync/
[root@momo rsync]# ls
file
[root@momo rsync]# cat file
hello
[root@momo rsync]# rm -rf file
[root@momo rsync]# ls
----------------------------
[root@desktop115 rsync]# ls
----------------------------


4.测试测试同步是否正常
在两台机器中的任一台创建或者删除一个文件,然后查看其它机器是否创建或删除。
如果遇到问题就用csync2 -xv命令手动调试并,根据错误信息作调整。




注:csync2相关命令介绍
csync2 -vvv -T 测试csync配置是否正确,可以看到相关SQL执行过程.
csync2 -xv 执行同步命令
csync2 -xvvv 执行同步命令,并显示出详细的信息.

附加:
1.实验需要两台主机,一模一样的配置
2.在第一个步骤,安装包时,查看系统是否有,并查看版本,依赖性足够用就可
3.因为实验是在6.0,一些包系统自带,而5.4没有这些包,就需要编译了。但是此实验还是用的源码包编译,因为其中有几个系统自带版本在这个实验中配置有错误,所以直接用源码包

应该注意的问题:
缓存相关的目录尽量不要用csync2去处理,这个程序目录尽量交给NFS处理.
http://oss.linbit.com/csync2/paper.pdf