csync2雙向同步

上個實驗實現了單向同步,這個實驗的目標是實現雙向同步
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/
[email protected]'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/
[email protected]'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/
[email protected]'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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章