rsync+inotify


前提條件:已配置好rsync客戶端和rsync --daemon服務端,數據可以正常同步,linux內核必須是2.6.13以上,如圖:

[root@A-linux ~]# rsync -avzP /tmp/ rsync_backup@10.0.0.4::data --password-file=/etc/rsync.password  #<===首先檢查同步是否正常
sending incremental file list
./
hosts
166 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)
ssh-hBbdsD1500/
ssh-hBbdsD1500/agent.1500

sent 250 bytes received 38 bytes 576.00 bytes/sec
total size is 166 speedup is 0.58

[root@C-linux data]# ll   #<===服務端查看同步結果
總用量 8
-rw-r--r-- 1 rsync rsync 166 1月 31 10:36 hosts
drwx------ 2 rsync rsync 4096 1月 31 13:27 ssh-hBbdsD1500

接着,在客戶端安裝inotify軟件(這里是源碼編譯安裝inotify-tools-3.14.tar.gz)

[root@A-linux tools]# tar xf inotify-tools-3.14.tar.gz 
[root@A-linux tools]# ./configure --prefix=/usr/local/inotify-tools-3.14 
[root@A-linux tools]# make && make install 
[root@A-linux tools]# ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify
[root@A-linux tools]# cd ../

inotify幾個主要的內核參數(可根據實際調整參數)

[root@A-linux inotify]# ll -l /proc/sys/fs/inotify/            #<===以下文件中的默認值需要調大
total 0
-rw-r--r-- 1 root root 0 Sep 27 17:57 max_queued_events        #<===監控時件最大數量,需調整此文件默認大小
-rw-r--r-- 1 root root 0 Sep 27 17:57 max_user_instances       #<===用戶的實例
-rw-r--r-- 1 root root 0 Sep 27 17:57 max_user_watches         #<===用戶實例可監控的最大目錄及文件數量
[root@A-linux inotify ~]# echo 327679 > /proc/sys/fs/inotify/max_queued_events  
[root@A-linux inotify ~]# echo 30000000 > /proc/sys/fs/inotify/max_user_watches

inotify軟件包主要的2個命令

[root@A-linux tools]# cd /usr/local/inotify
[root@A-linux inotify]# tree ./bin
./bin
├── inotifywait          #<===等待文件發生變化,是inotify核心命令
└── inotifywatch         #<===用於收集文件系統的統計數據,例如發生了多少次inotify事件,某文件被訪問了多少次等等,一般用不上

inotifywait 命令的參數

-m  :表示始終監控,否則應該是監控到了一次就退出監控了
-r  :遞歸監控,監控目錄中的任何文件,包括子目錄。遞歸監控可能會超出max_user_watches的值,需要適當調整該值
@<file>  :如果是對目錄進行遞歸監控,則該選項用於排除遞歸目錄中不被監控的文件。file是相對路徑還是絕對路徑由監控目錄是相對還是絕對來決定
-q  :--quiet的意思,靜默監控,這樣就不會輸出一些無關的信息
-e  :指定監控的事件。一般監控的就 delete、create、attrib、modify、close_write
--exclude <pattern>  :通過模式匹配來指定不被監控的文件,區分大小寫
--excludei <pattern> :通過模式匹配來指定不被監控的文件,不區分大小寫
--timefmt  :監控到事件觸發后,輸出的時間格式,可指定可不指定該選項,一般設置為[--timefmt '%Y/%m/%d %H:%M:%S']
--format   :用戶自定義的輸出格式,如[--format '%w%f %e%T']
  %w  :產生事件的監控路徑,不一定就是發生事件的具體文件,例如遞歸監控一個目錄,該目錄下的某文件產生事件,將輸出該目錄而非其內具體的文件
  %f  :如果監控的是一個目錄,則輸出產生事件的具體文件名。其他所有情況都輸出空字符串
  %e  :產生的事件名稱
  %T  :以"--timefmt"定義的時間格式輸出當前時間,要求同時定義"--timefmt"

inotifywait -e 指定可監控的事件

access  :文件被訪問
modify  :文件被寫入,內容被修改
attrib  :元數據被修改。包括權限、時間戳、擴展屬性等等
close_write  :打開的文件被關閉,是為了寫文件而打開文件,之后被關閉的事件
close_nowrite :read only模式下文件被關閉,即只能是為了讀取而打開文件,讀取結束后關閉文件的事件
close :是close_write和close_nowrite的結合,無論是何種方式打開文件,只要關閉都屬於該事件
open  :文件被打開
moved_to  :向監控目錄下移入了文件或目錄,也可以是監控目錄內部的移動
moved_from :將監控目錄下文件或目錄移動到其他地方,也可以是在監控目錄內部的移動
move :是moved_to和moved_from的結合
moved_self :被監控的文件或目錄發生了移動,移動結束后將不再監控此文件或目錄
create :在被監控的目錄中創建了文件或目錄
delete :刪除了被監控目錄中的某文件或目錄
delete_self :被監控的文件或目錄被刪除,刪除之后不再監控此文件或目錄
umount :掛載在被監控目錄上的文件系統被umount,umount后不再監控此目錄
isdir :監控目錄相關操作

測試inotify監控實例(監控A-linux主機中/data目錄下子目錄及文件的變化)

監控單個事件
[root@A-linux inotify]# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data     #<==僅僅監控目錄中被刪除文件或目錄

同時監控多個事件
[root@A-linux inotify]# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,close_write /data 

使用inotify腳本實時監控目錄內子目錄和文件的變化

[root@A-linux ~]# cat inotify.sh 
#!/bin/bash

###########################################################
# description: inotify+rsync best practice #
# author : 駿馬金龍 #
# blog : http://www.cnblogs.com/f-ck-need-u/ #
###########################################################

watch_dir=/data
push_to=10.0.0.4

# First to do is initial sync
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.password

/usr/local/inotify/bin/inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir \
--exclude=".*.swp" >>/etc/inotifywait.log &

while true;do
if [ -s "/etc/inotifywait.log" ];then
grep -i -E "delete|moved_from" /etc/inotifywait.log >> /etc/inotify_away.log
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.password
if [ $? -ne 0 ];then
echo "$watch_dir sync to $push_to failed at `date +"%F %T"`,please check it by manual" |\
mail -s "inotify+Rsync error has occurred" root@localhost
fi
cat /dev/null > /etc/inotifywait.log
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.password
else
sleep 1
fi
done

腳本可以放進 /etc/rc.local 中開機執行,或者放在后台執行

[root@A-linux ~]# echo '/bin/sh /root/inotify.sh &' >>/etc/rc.local 
[root@A-linux ~]# tail -1 /etc/rc.local 
/bin/sh /root/inotify.sh &

小結:

(1)當同步的目錄數據量不大時,建議使用 rsync+inotify
(2)當同步的目錄數據量很大時(幾百G甚至1T以上)文件很多時,建議使用 rsync+sersync
(3)生產環境直接用 rsync+sersync,而不使用 rsync+inotify

博客參考 

http://www.cnblogs.com/f-ck-need-u/p/7220193.html

www.cnblogs.com/kevingrace/p/6001252.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM