1、rsync+inotify組合的起源
Rsync(remote sync)遠端同步工具,通過Rsync可以實作對遠端伺服器資料的增量備份同步,但Rsync自身也有瓶頸,同步資料時,Rsync采用核心算法對遠端伺服器的檔案進行對比,隻進行差異同步。我們可以想象一下,如果伺服器的檔案數量達到了百萬甚至千萬量級,那麼檔案對比将是非常耗時的。而且發生變化的往往是其中很少的一部分,這是非常低效的方式。inotify的出現,可以緩解Rsync不足之處,取長補短。
2、inotify簡介
inotify是一種強大的、細粒度的、異步的檔案系統事件監控機制,Linux核心從2.6.13起,加入inotify支援,通過inotify可以監控檔案系統中添加、删除、移動等各種事件,利用這個核心接口,第三方軟體就可以監控檔案系統下檔案的各種變化情況,而inotify-tools正式實施這樣監控的軟體。
3、Rsync+inotify資料實時同步原理圖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCM581dvRWYoNHLwEzX5xCMx8FesU2cfdGLwATMfRHLGZkRGZkRfJ3bs92YskmNhVTYykVNQJVMRhXVEF1X0hXZ0xiNx8VZ6l2cssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL0EjN5cDN2QDO3EzM4UTNx8CXwITNwkTMwIzLcNXZnFWbp9CXvwVbvNmLvR3YxUjL0M3Lc9CX6MHc0RHaiojIsJye.png)
4、inotify實施準備
前提是rsync daemon服務配置成功,可以在用戶端推送拉取資料,然後才能配置inotify服務。
Rsync daemon服務配置:
https://blog.51cto.com/13716812/2396183
5、inotify開始安裝
1)檢視系統是否支援inotify
[root@inotify /]# uname -r
2.6.32-431.el6.x86_64
[root@inotify /]# ll /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 11月 27 17:43 max_queued_events
-rw-r--r-- 1 root root 0 11月 27 17:43 max_user_instances
-rw-r--r-- 1 root root 0 11月 27 17:43 max_user_watches
2)下載下傳inotify源碼包
[root@inotify tools]# pwd
/home/oldboy/tools
[root@inotify tools]# wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@inotify tools]# ll
總用量 768
-rw-r--r-- 1 root root 389473 1月 1 2008 inotify-tools-3.13.tar.gz
3)編譯安裝inotify
[root@inotify tools]#tar zxf inotify-tools-3.13.tar.gz
[root@inotify tools]#cd inotify-tools-3.13
[root@inotify inotify-tools-3.13]#yum -y install gcc gcc-c++
[root@inotify inotify-tools-3.13]#./configure --prefix=/usr/local/inotify-tools-3.13
[root@inotify inotify-tools-3.13]#make && make install
[root@inotify inotify-tools-3.13]# cd ../
[root@inotify tools]# ln -s /usr/local/inotify-tools-3.13 /usr/local/inotify
[root@inotify tools]# ll /usr/local/inotify
lrwxrwxrwx 1 root root 29 11月 27 18:10 /usr/local/inotify -> /usr/local/inotify-tools-3.13
參數:
--prefix=PATH 指定編譯安裝的路徑
提示:更多的編譯參數可以使用./configure –h檢視。
編譯成功後會生成4個目錄,分别是:
[root@inotify inotify]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Nov 27 18:04 bin #inotify執行指令(二進制)
drwxr-xr-x 3 root root 4096 Nov 27 18:04 include #inotify程式所需用的頭檔案
drwxr-xr-x 2 root root 4096 Nov 27 18:04 lib #動态連結的庫檔案
drwxr-xr-x 4 root root 4096 Nov 27 18:04 share #幫助文檔
[root@inotify inotify]# tree
.
|-- bin
| |-- inotifywait
| `-- inotifywatch
|-- include
| `-- inotifytools
| |-- inotify-nosys.h
| |-- inotify.h
| `-- inotifytools.h
|-- lib
| |-- libinotifytools.a
| |-- libinotifytools.la
| |-- libinotifytools.so -> libinotifytools.so.0.4.1
| |-- libinotifytools.so.0 -> libinotifytools.so.0.4.1
| `-- libinotifytools.so.0.4.1
`-- share
|-- doc
| `-- inotify-tools
`-- man
`-- man1
|-- inotifywait.1
`-- inotifywatch.1
9 directories, 12 files
[root@inotify inotify]# ./bin/inotifywait --help
inotifywait 3.13
-r|--recursive Watch directories recursively. #遞歸查詢目錄
-q|--quiet Print less (only print events). #列印很少資訊,僅僅列印監控事件的資訊
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received. #始終保持事件監聽狀态
--excludei <pattern>
Like --exclude but case insensitive. #排除檔案或目錄時,不區分大小寫
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string. #指定時間輸出的格式
--format <fmt> Print using a specified printf-like format
string; read the man page for more details. #列印使用指定的輸出類似格式字元串
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for. #通過此參數可以指定需要監控的事件,如下所示:
Events:
access file or directory contents were read #檔案或目錄被讀取
modify file or directory contents were written #檔案或目錄内容被修改
attrib file or directory attributes changed #檔案或目錄屬性被改變
close file or directory closed, regardless of read/write mode #檔案或目錄封閉,無論讀/寫模式
open file or directory opened #檔案或目錄被打開
moved_to file or directory moved to watched directory #檔案或目錄被移動至另一個目錄
move file or directory moved to or from watched directory #檔案或目錄被移動另一個目錄或從另一個目錄移動至目前目錄
create file or directory created within watched directory #檔案或目錄被建立在目前目錄
delete file or directory deleted within watched directory #檔案或目錄被删除
unmount file system containing file or directory unmounted #檔案系統被解除安裝
4)人工測試監控事件
① 測試create(建立)
#監聽指令(目前目錄建立檔案或目錄)
[root@inotify inotify]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
28/11/18 14:28 /data/a.log
28/11/18 14:28 /data/wahaha.txt
#另外連接配接一個終端,建立檔案或目錄看監聽是否有變化(編輯檔案監聽也會有變化的)
[root@inotify data]# vim a.log
[root@inotify data]# touch wahaha.txt
② 測試delete(删除)
#監聽指令(删除)
[root@inotify inotify]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /data
28/11/18 14:36 /data/a.log
28/11/18 14:36 /data/wahaha.txt
28/11/18 14:37 /data/inotify.log
#另外連接配接一個終端,删除檔案或目錄看監聽是否有變化
[root@inotify data]# ls
a.log wahaha.txt
[root@inotify data]# rm a.log
rm:是否删除普通檔案 "a.log"?y
[root@inotify data]# rm wahaha.txt
rm:是否删除普通空檔案 "wahaha.txt"?y
[root@inotify data]# touch inotify.log
③ close_write(寫)
#監聽指令(寫入檔案)
[root@inotify inotify]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T%w%f' -e create,delete,close_write /data
28/11/18 14:50/data/inotify.log
#另外連接配接一個終端,寫入檔案看監聽是否有變化
[root@inotify data]# echo 'www' >> inotify.log
[root@inotify data]# cat inotify.log
1aaaa11
www
5)編寫inotify監控腳本
[root@inotify scripts]# vim rsync+inotify.sh
#!/bin/bash
#qq:1431975440
#rsync+inotify
serverID=10.90.3.105
back_path=/data
rsync_module=oldboy
rsync_user=rsync_backup
rsync_passwd=/etc/rsync.password
inotify_path=/opt/inotify/bin/inotifywait
#測試rsync資料傳輸是否正确
/usr/bin/rsync -avz /dev/null $rsync_user@$serverID::$rsync_module --password-file=${rsync_passwd} >/dev/null 2>&1
[ $? -ne 0 ] && echo "Rsync error ,Please check rsync." exit 2
#監控檔案變化及傳輸資料
$inotify_path -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,close_write,attrib $back_path \
| while read file
do
/usr/bin/rsync -avz --progress --delete $back_path/ --timeout=100 $rsync_user@$serverID::$rsync_module --password-file=${rsync_passwd} >/dev/null 2>&1
done
exit 0
6)執行inotify監控腳本(加&符,背景執行)
[root@inotify scripts]# sh rsync+inotify.sh &
[2] 24109
7)檢視rsync+inotify.sh腳本是否執行成功
[root@inotify scripts]# ps -ef|grep rsync+inotify
root 17208 17096 0 May17 pts/2 00:00:00 vim rsync+inotify.sh
root 24109 17096 0 11:04 pts/2 00:00:00 sh rsync+inotify.sh
root 24112 24109 0 11:04 pts/2 00:00:00 sh rsync+inotify.sh
root 24114 17096 0 11:04 pts/2 00:00:00 grep --color=auto rsync+inotify
8)編寫監控rsync+inotify腳本程序,隻保留一個程序即可
編寫監控rsync+inotify腳本程序狀态,如果腳本沒有啟動則啟動腳本,如果腳本程序過多,則删除程序,隻留一個程序即可。
[root@inotify scripts]# vim check_rsync_inotify_status.sh
#!/bin/bash
if [ `ps -ef|grep -v grep|grep -v vim|grep rsync+inotify.sh|wc -l` -ne 1 ];then
for n in `ps -ef|grep rsync+inotify.sh|grep -v grep|awk '{print $2}'`;do kill -9 $n;done
/usr/bin/sh /server/scripts/rsync+inotify.sh &
fi
9)把監控腳本check_rsync_inotify_status.sh放在定時任務
把監控腳本放在定時任務當中,每分鐘執行一次。
[root@inotify scripts]# crontab -e
#check rsync+inotify status
* * * * * /usr/bin/sh /server/scripts/check_rsync_inotify_status.sh
10)檢查rsync+inotify程序
[root@inotify scripts]# ps -ef|grep -v grep|grep rsync+inotify
root 24815 1 0 13:31 ? 00:00:00 /usr/bin/sh /server/scripts/rsync+inotify.sh
root 24818 24815 0 13:31 ? 00:00:00 /usr/bin/sh /server/scripts/rsync+inotify.sh
11)測試資料實時同步
① 建立新檔案和新目錄
檢視rsync服務端資料:(空)
[root@rsync-server oldboy]# pwd
/oldboy
[root@rsync-server oldboy]# ll
total 0
檢視rsync用戶端(inotify)伺服器檔案或目錄,并且建立新檔案或目錄
[root@inotify data]# pwd
/data
[root@inotify data]# ll
total 0
[root@inotify data]# touch oldboy{01..03}
[root@inotify data]# ls
oldboy01 oldboy02 oldboy03
[root@inotify data]# mkdir data_{a..c}
[root@inotify data]# ll
total 0
drwxr-xr-x. 2 root root 6 May 20 13:39 data_a
drwxr-xr-x. 2 root root 6 May 20 13:39 data_b
drwxr-xr-x. 2 root root 6 May 20 13:39 data_c
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy01
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy02
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy03
檢視rsync服務端是否有建立的檔案和目錄
[root@rsync-server oldboy]# pwd
/oldboy
[root@rsync-server oldboy]# ll
total 0
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_a
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_b
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_c
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy01
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy02
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy03
② 删除檔案和目錄,修改檔案指令和目錄名
在rsync用戶端(inotify)伺服器上删除和修改檔案或目錄
[root@inotify data]# rm -f oldboy01
[root@inotify data]# mv oldboy02 inotify02
[root@inotify data]# rm -rf data_a
[root@inotify data]# mv data_b accp
[root@inotify data]# ll
total 0
drwxr-xr-x. 2 root root 6 May 20 13:39 accp
drwxr-xr-x. 2 root root 6 May 20 13:39 data_c
-rw-r--r--. 1 root root 0 May 20 13:38 inotify02
-rw-r--r--. 1 root root 0 May 20 13:38 oldboy03
在rsync服務端上檢視資料是否有變化
[root@rsync-server oldboy]# ll
total 0
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 accp
drwxr-xr-x. 2 rsync rsync 6 May 20 13:39 data_c
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 inotify02
-rw-r--r--. 1 rsync rsync 0 May 20 13:38 oldboy03
③ 向檔案中添加資料
在rsync用戶端(inotify)檔案中添加資料
[root@inotify data]# echo wahahahahaha > oldboy03
[root@inotify data]# echo oldboy > inotify02
[root@inotify data]# cat oldboy03
wahahahahaha
[root@inotify data]# cat inotify02
oldboy
[root@rsync-server oldboy]# ls
accp data_c inotify02 oldboy03
[root@rsync-server oldboy]# cat oldboy03
wahahahahaha
[root@rsync-server oldboy]# cat inotify02
oldboy