在前面的博文實踐記錄之-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的組合命令 :
二、在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
對比文件的時間,可見同步的效率很高.
測試較大文件同步:
在/test目錄下創建一500M大小的文件
[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
同步時間爲30幾秒.