天天看點

Rsync+Inotify-client 實作實時同步

        在前面的博文實踐記錄之-Rsync鏡像備份介紹了鏡像備份工具Rsync的安裝和使用.但在大資料時代,rsync的不足之處也暴露出來.

        首先.rsync本身實作不了實時備份.靠系統的crontab實作的話也受限于1分鐘.是以這就導緻了服務端和用戶端資料可能出現不一緻,更無法在應用故障時做到資料的完全恢複。其次,rsync在備份時,要掃描所有檔案,這樣效率就特别低,特别在資料量很大的時候.

        不過,結合Inotify可以很好的解決Rsync在這方面的缺陷.基本實作原理是這樣:通過使用shell腳本,擷取inotifywait輸出,然後借助inotifywait的監控檔案或目錄實時變化去通知rsync做相應的推送或者拉取操作!

        Inotify-client的安裝和使用檢視另一篇博文實踐記錄之-系統監控工具Inotify-tool.

        下面實踐下Rsync+Inotify-client 實作實時同步.

        實驗環境:

       Master:  Rsync用戶端+Inotify服務 IP:192.168.1.33   hostname:RsyncClient-Inotify

       Slave: Rsync服務端 IP:192.168.1.34   hostname:RsyncServer

       本實驗隻設定一台Slave ,現實操作可以有多台Slave ,更有保障些;

一、在 SLAVE 機器上部署 rsync 服務端程式

1.安裝rsync:  

[root@RsyncClient-Inotify ~]# yum -y install rsync      

2.添加rsync配置檔案

[root@RsyncServer ~]# vi /etc/rsyncd.conf      

   内容如下:

uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = *
auth users = rsync
secrets file = /etc/rsync.password
[snbo]
path = /test      

3、建立 rsync.password 作為使用者密碼檔案

[root@RsyncServer ~]# touch /etc/rsync.password
[root@RsyncServer ~]# chmod 600 /etc/rsync.password 
[root@RsyncServer ~]# vi /etc/rsync.password 
[root@RsyncServer ~]# cat /etc/rsync.password 
rsync:123456      

        上面的rsync服務的配置檔案,表明允許192.168.1.0網段的主機通路,rsync同步子產品名為[snbo],将同步過來的檔案放入對應path指定的目錄/test,如果有多台目标伺服器,則每一台都需要進行類似的rsync服務端配置,上面的uid和gid需要換成你伺服器的相應的同步使用者。

4、建立同步目錄

[root@RsyncServer ~]# mkdir /test
[root@RsyncServer ~]# chown root:root /test      

5、以守護程序方式啟動rsync服務

[root@RsyncServer ~]# rsync --daemon      

6、檢視rsync服務狀态

[root@RsyncServer ~]# lsof -i:873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   1087 root    3u  IPv4   9007      0t0  TCP *:rsync (LISTEN)
rsync   1087 root    5u  IPv6   9008      0t0  TCP *:rsync (LISTEN)      

7、為rsync添加開機自啟動

在/etc/rc.local檔案裡添加一行以下内容:

/usr/bin/rsync --daemon  --config=/etc/rsyncd.conf

補充:

      重新開機rsync的組合指令 :

  pkill rsync  #關閉rsync服務

  rsync --daemon #啟動rsync服務

  ps -ef | grep rsync   或 lsof -i:873  #檢查是否啟動

二、在Master 上配置rsync用戶端

1、安裝Rsync并配置相關權限

在Master上配置rsync用戶端相關權限認證(rsync.password):

[root@RsyncClient-Inotify ~]# yum -y install rsync
[root@RsyncClient-Inotify ~]# vi /etc/rsync.password
[root@RsyncClient-Inotify ~]# chmod 600 /etc/rsync.password
[root@RsyncClient-Inotify ~]# cat /etc/rsync.password 
123456      

2、Master上手動測試rsync的同步情況

要確定這一步能成功執行,否則後面的Inotify配置好了也同步不了.

1)建立待同步資料

[root@RsyncClient-Inotify ~]# mkdir /test
[root@RsyncClient-Inotify ~]# touch /test/{a,b,c,d}
[root@RsyncClient-Inotify ~]# ll /test/
total 0
-rw-r--r-- 1 root root 0 Jul 12 06:50 a
-rw-r--r-- 1 root root 0 Jul 12 06:50 b
-rw-r--r-- 1 root root 0 Jul 12 06:50 c
-rw-r--r-- 1 root root 0 Jul 12 06:50 d      

2)執行同步指令

[root@RsyncClient-Inotify ~]# rsync -avzP /test  [email protected]::snbo --password-file=/etc/rsync.password 
sending incremental file list
test/
test/a
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
test/b
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=2/5)
test/c
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=1/5)
test/d
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=0/5)
sent 213 bytes  received 88 bytes  46.31 bytes/sec
total size is 0  speedup is 0.00      

檢視RsyncServer的同步目錄:

[root@RsyncServer ~]# ll /test/
total 0
-rw-r--r-- 1 root root 0 Jul 12 06:50 a
-rw-r--r-- 1 root root 0 Jul 12 06:50 b
-rw-r--r-- 1 root root 0 Jul 12 06:50 c
-rw-r--r-- 1 root root 0 Jul 12 06:50 d      

三、在M1 上配置inotify

1、檢視 M1的 核心是否支援inotify

[root@RsyncClient-Inotify ~]# uname -r
2.6.32-358.el6.x86_64
[root@RsyncClient-Inotify ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jul 12 07:12 max_queued_events
-rw-r--r-- 1 root root 0 Jul 12 07:12 max_user_instances
-rw-r--r-- 1 root root 0 Jul 12 07:12 max_user_watches      

2、安裝inotify-tool

[root@RsyncClient-Inotify ~]# yum install make  gcc gcc-c++
[root@RsyncClient-Inotify ~]#  wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@RsyncClient-Inotify ~]#  tar xzf inotify-tools-3.13.tar.gz
[root@RsyncClient-Inotify ~]#  cd inotify-tools-3.13
[root@RsyncClient-Inotify inotify-tools-3.13]# ./configure
[root@RsyncClient-Inotify inotify-tools-3.13]# make && make install      

3、檢視inotify提供的工具

[root@RsyncClient-Inotify ~]# ll /usr/local/bin/
total 80
-rwxr-xr-x 1 root root 38582 Jul  9 15:25 inotifywait
-rwxr-xr-x 1 root root 40353 Jul  9 15:25 inotifywatch      

 Inotify-client的詳細使用檢視另一篇博文實踐記錄之-系統監控工具Inotify-tool.

四、rsync和inotify-tools做結合

通過腳本,把rsync和inotify做一個結合.實作rsync的實時備份!

現貼出腳本如下:

[root@RsyncClient-Inotify ~]# cat auto_rsync.sh 
#!/bin/bash
dir='/test/'
des=snbo
host=192.168.1.34
user=rsync
rsyncall='/usr/bin/rsync -rpgovz --delete --progress'
/usr/local/bin/inotifywait -mrq --timefmt '%Y%m%d %H:%M:%S' --format '%T %w %w%f %e' -e modify,delete,create,attrib $dir | while read DATE TIME DIR FILE EVENT;
do
$rsyncall $dir $user@$host::$des --password-file=/etc/rsync.password && echo $DATE $TIME $FILE was Rsynced:$EVENT >>/var/log/rsync-snbo.log
done      

将auto_rsync.sh加入開機啟動:

[root@RsyncClient-Inotify ~]# mv auto_rsync.sh /usr/sbin/
[root@RsyncClient-Inotify ~]# vi /etc/rc.local 
加入下面一行
sh /usr/sbin/auto_rsync.sh  >>/var/log/rsync.log &      

測試:

兩台主機提前同步過時間,好作對比.

網絡同步時間指令:ntpdate cn.pool.ntp.org && hwclock -w
建立檔案:
[root@RsyncClient-Inotify ~]# touch /test/{1,2,3,4}
[root@RsyncClient-Inotify ~]# ll --full-time /test/
total 0
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 1
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 2
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 3
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 4
檢視RsyncServer目錄檔案變化
[root@RsyncServer ~]# ll --full-time /test/
total 0
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.254956293 +0800 1
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.254956293 +0800 2
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.254956293 +0800 3
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.294956574 +0800 4      

對比檔案的時間,可見同步的效率很高.

測試較大檔案同步:

[root@RsyncClient-Inotify ~]# dd if=/dev/zero of=/test/file count=1000000
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 10.5061 s, 48.7 MB/s
[root@RsyncClient-Inotify ~]# ll --full-time /test/
total 500004
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 1
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 2
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 3
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 4
-rw-r--r-- 1 root root 512000000 2014-07-16 12:05:26.566868352 +0800 file
[root@RsyncServer ~]# ll --full-time /test/
total 500004
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.254956293 +0800 1
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.254956293 +0800 2
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.254956293 +0800 3
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.294956574 +0800 4
-rw-r--r-- 1 root root 512000000 2014-07-16 12:06:05.143956010 +0800 file      

繼續閱讀