rsync實時同步

 

實驗目的:例如在一個大的集羣架構中,下面有N個WEB節點,當站點需要更新操作時,如果一臺一臺的去更新,會大大降低系統/運維工程師的效率,然而,使用rsync構建所有節點之間的實時同步,其中有一臺同步端,當同步端下面的WEB站點內容發生變化時,所有被同步端同時也會得到自動更新,這樣就大大降低了系統/運維工程師比較枯燥而又無味的操作。
注:此同步不可用於數據庫直接的實時同步。

一、系統環境
1、同步端
[root@host3 ~]# cat /etc/redhat-release
CentOS release 5.5 (Final)
[root@host3 ~]# uname -a
Linux host3.zc.com 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@host3 ~]# ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
192.168.10.3

2、被同步端
[root@host4 ~]# cat /etc/redhat-release
CentOS release 5.5 (Final)
[root@host4 ~]# uname -a
Linux host4.zc.com 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@host4 ~]# ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
192.168.10.4

二、軟件環境
rsync
        rsync是一個開源快速備份工具,可以在不同主機之間鏡像同步整個目錄樹,支持增量備份、保持鏈接和權限,且採用優化的同步算法、傳輸前執行壓縮,因此非常適用於異地備份、鏡
像服務器等應用。
inotify-tools-3.14.tar.gz
        Inotify 是文件系統事件監控機制,計劃包含在即將發佈的 Linux 內核中作爲 dnotify
的有效替代。dnotify是較早內核支持的文件監控機制。Inotify一種強大的、細粒度的、異步的機制,它滿足各種各樣的文件監控需要,不僅限於安全和性能。

三、安裝配置rsync(CentOS5.5系統中默認已經安裝rsync,這裏我們使用系統默認rpm包安裝的rsync,也順道提供了源碼包的安裝方法)
1、源碼包
tar xzvf rsync-3.0.8.tar.gz
cd rsync-3.0.8
./configure
make
make install
mkdir /etc/rsyncd
vim rsyncd.conf

2、rpm包
同步端:
[root@host3 ~]# yum -y install gcc* xinetd
[root@host3 ~]# rpm -qa | grep rsync
rsync-2.6.8-3.1
[root@host3 ~]# cat /etc/rsyncd.conf                         #此文件爲自己建立的文件
uid = nobody                                                #運行rsync程序的用戶
gid = nobody                                                #運行rsync程序的組
use chroot = yes                                            #禁錮在源目錄
adress = 192.168.10.3                                        #監聽地址(本機ip)
port 873                                                    #監聽端口
log file = /var/log/rsyncd.log                                #日誌文件位置
pid file = /var/run/rsyncd.pid                                #存放進程ID的文件位置
hosts allow = 192.168.10.0/24                                #允許訪問的客戶機地址
[wwwroot]                                                    #
        path = /test/                                        #源目錄的實際路徑
        read only = yes                                        #是否爲只讀
        comment = Document Root of host1.benet.com             #描述
        dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z    #同時不再壓縮的文件類型
被同步端:
[root@host4 ~]# yum -y install xinetd
[root@host4 ~]# rpm -qa | grep rsync
rsync-2.6.8-3.1
[root@host4 ~]# cat /etc/rsyncd.conf
uid = nobody                                                #運行rsync程序的用戶
gid = nobody                                                #運行rsync程序的組
use chroot = yes                                            #禁錮在源目錄
adress = 192.168.10.4                                        #監聽地址(本機ip)
port 873                                                    #監聽端口
log file = /var/log/rsyncd.log                                #日誌文件位置
pid file = /var/run/rsyncd.pid                                #存放進程ID的文件位置
hosts allow = 192.168.10.0/24                                #允許訪問的客戶機地址
[wwwroot]                                                    #
        path = /test/                                        #源目錄的實際路徑
        read only = yes                                        #是否爲只讀
        comment = Document Root of host1.benet.com             #描述
        dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z    #同時不再壓縮的文件類型
[root@host4 ~]# sed -i 's/yes/no/' /etc/xinetd.d/rsync
[root@host4 ~]# /etc/init.d/xinetd restart
[root@host4 ~]# chkconfig xinetd on
[root@host4 ~]# netstat -anpt | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      3749/xinetd 

四、在同步端安裝inotify軟件
1、調整inotify內核參數
    在linux內核中,默認的inotify機制提供了三個調控參數:max_queue_envents        監控事件隊列
                                                        max_user_instances        最多監控實例數
                                                        max_user_watches        每個實例最多監控文件數(建議大於監控目標的文件數)
[root@host3 ~]# vi /etc/sysctl.conf     #末行添加
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@host3 ~]# cd /data/
[root@host3 data]# tar -zxf inotify-tools-3.14.tar.gz
[root@host3 data]# cd inotify-tools-3.14
[root@host3 inotify-tools-3.14]# ./configure
[root@host3 inotify-tools-3.14]# make
[root@host3 inotify-tools-3.14]# make install

2、編寫觸發式同步腳本
[root@host3 inotify-tools-3.14]# cd /script/
[root@host3 script]# cat inosync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /test/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
        if [ $(pgrep rsync | wc -l) -le 0 ];then
                rsync -azH  --delete  /test/ [email protected]:/test/
                rsync -e 'ssh -p 2000' -azH  --delete  /test/ [email protected]:/test/        #當ssh端口被修改之後,得添加'ssh -p 2000'指定端口
        fi
done
[root@host3 script]# chmod +x inosync.sh
[root@host3 script]# echo '/script/inosync.sh' >> /etc/rc.local
[root@host3 script]# sh inosync.sh &

四創建同步用戶
被監控端
[root@host4 /]# useradd rput
[root@host4 /]# passwd rput            密碼根據自己的安全要求設置
監控端
[root@host3 ~]# ssh-keygen
[root@host3 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

設置rput用戶對被同步目錄的權限
被監控端
[root@host4 ~]# setfacl -R -m u:rput:rwx /test/

5、測試
在同步端的/test目錄下創建一個目錄,然後再被同步端進行查看

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