inotify + rsync,inotifyrsync

來源:互聯網
上載者:User

inotify + rsync,inotifyrsync

       Linux核心從2.6.13開始,引入了inotify機制。通過intofity機制,能夠對檔案系統的變化進行監控,如對檔案進行建立、刪除、修改等操作,可以及時通知應用程式進行相關事件的處理。這種響應處理機制,避免了頻繁的檔案輪詢任務,提高了任務的處理效率。

 

 

一、檢查安裝
檢查系統核心版本

# uname -a  

檢查系統是否支援inotify

# ls -lsart /proc/sys/fs/inotify  total 0  0 dr-xr-xr-x 0 root root 0 Sep 19 09:38 ..  0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_watches  0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_instances  0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_queued_events  0 dr-xr-xr-x 0 root root 0 Jan  1 13:51 .  

 

如果出現上面結果說明系統支援inotify

 

notify是一個API,需要通過開發應用程式進行調用,對於大多數使用者來講這有著許多不便,inotify-tools的出現彌補了這一不足。
inotify-tools是一套組件,它包括一個C庫和幾個命令列工具,這些命令列工具可用於通過命令列或指令碼對某檔案系統的事件進行監控。

 

下載安裝

#wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz    # tar -zvxf inotify-tools-3.14.tar.gz  # cd inotify-tools-3.14    [root@ inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify  [root@ inotify-tools-3.14]# make  [root@ inotify-tools-3.14]# make install  

  

或者使用yum

yum install inotify-tools


其它的一些notify工具(參考)

inotify 的實現有幾款軟體
1)inotify-tools,
2)sersync(金山周洋)
3)lsyncd

 

二、命令講解

inotify-tools提供的兩個命令列工具:

inotifywait:通過inotify API等待被監控檔案上的相應事件並返回監控結果
                   預設情況下,正常的結果返回至標準輸出,
                   診斷類的資訊則返回至標準錯誤輸出。
                   它可以在監控到對應監控對象上指定的事件後退出,也可以進行持久性的監控。

inotifywatch:通過inotify API收集被監控檔案或目錄的相關事件並輸出統計資訊。


inotifywait:

常用參數:  --timefmt 時間格式   %y年 %m月 %d日 %H小時 %M分鐘  --format  輸出格式   %T時間 %w路徑 %f檔案名稱 %e狀態 
-m --monitor     始終保持監聽狀態預設觸發事件即退出-r --recursive  遞迴查詢目錄  -q              列印出監控事件  

 

-e --event     定義監控的事件,可用參數:

Events:
access          file or directory contents were read         檔案讀取
modify           file or directory contents were written     檔案更改
attrib              file or directory attributes changed          檔案屬性更改
close_write    file or directory closed, after being opened in                 以唯讀模式開啟的檔案被關閉
                      writeable mode
close_nowrite    file or directory closed, after being opened in             以唯讀模式開啟的檔案被關閉
                          read-only mode
close             file or directory closed, regardless of read/write mode      檔案被關閉,不管它是如何開啟的
open              file or directory opened
moved_to       file or directory moved to watched directory         移入監聽目錄 即使是在同一目錄內移動,此事件也觸發
moved_from    file or directory moved from watched directory   移除監聽目錄 即使是在同一目錄內移動,此事件也觸發
move             file or directory moved to or from watched directory   包括moved_to和 moved_from
create           file or directory created within watched directory
delete           file or directory deleted within watched directory
delete_self    file or directory was deleted                                      檔案或目錄移除,之後不再監聽此檔案或目錄
unmount       file system containing file or directory unmounted

 

做個小實驗,監控一個目錄 

# inotifywait -rm ./test

在另一個終端對目錄進行操作

# touch /root/test/abc# rm /root/test/abc  

第一個終端顯示的 狀態改變

# inotifywait -rm ./testSetting up watches.  Beware: since -r was given, this may take a while!Watches established../test/ CREATE abc./test/ OPEN abc./test/ ATTRIB abc./test/ CLOSE_WRITE,CLOSE abc./test/ OPEN,ISDIR ./test/ CLOSE_NOWRITE,CLOSE,ISDIR ./test/ OPEN,ISDIR ./test/ CLOSE_NOWRITE,CLOSE,ISDIR ./test/ DELETE abc

  

 inotifywatch

inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]


參數:

-h,–help    # 輸出協助資訊-v,–verbose # 輸出詳細資料@             # 排除不需要監視的檔案,可以是相對路徑,也可以是絕對路徑。–fromfile    # 從檔案讀取需要監視的檔案或排除的檔案,一個檔案一行,排除的檔案以@開頭。-z,–zero    # 輸出表格的行和列,即使元素為空白–exclude     # 正則匹配需要排除的檔案,大小寫敏感。–excludei    # 正則匹配需要排除的檔案,忽略大小寫。-r,–recursive  # 監視一個目錄下的所有子目錄。-t,–timeout    # 設定逾時時間-e,–event      # 只監聽指定的事件。  監聽事件同上-a,–ascending  # 以指定事件升序排列。-d,–descending # 以指定事件降序排列

  同樣做個實驗,統計 /home 目錄發生事件的次數

# inotifywatch -v -e create -e modify -e delete -t 30 -r /home 

在新開啟的終端上,建立了4個檔案,修改了3個檔案內容,刪除了一個檔案。
等監控的30秒時間到了之後,他就會顯示出上面的事件次數報告!

# inotifywatch -v -e create -e modify -e delete -t 30 -r /homeEstablishing watches...Setting up watch(es) on /homeOK, /home is now being watched.Total of 3 watches.Finished establishing watches, now collecting statistics.Will listen for events for 60 seconds.total  modify  create  delete  filename8       3        4       1     /home/

  

 

三、實際操作

以下為生產環境 使用指令碼:

監控源目標檔案,發生變化立刻同步

#!/bin/shSRC=/var/www/channel/DST=/var/www/webroot/channel/INWT=/usr/local/bin/inotifywaitRSYNC=/usr/bin/rsync$INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do            rsync -aHqzt $SRC $DSTdone

  inotifywait 產生的資料

# inotifywait -mrq -e create,move,delete,modify ./test/
./test/ DELETE abc

目錄 事件 檔案名稱    
分別寫進 D E F變數裡,一旦有變數寫入立即執行 同步操作 

 

一個複雜的指令碼

按照事件類型 分格式寫入日誌

#!/bin/bashinotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format  '%T %w%f %e' --event delete,modify,create,attrib  /data/web | while read  date time file event  do      case $event in          MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)                  echo $event'-'$file'-'$date'-'$time >> /var/log/web_watch.log              ;;             MOVED_FROM|MOVED_FROM,ISDIR|DELETE|DELETE,ISDIR)                  echo $event'-'$file'-'$date'-'$time /var/log/web_watch.log              ;;      esac  done

inotifywait 產生的資料

# inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f %e' --event delete,modify,create,attrib ./test/

17/10/10 17:31 ./test/abc CREATE
17/10/10 17:31 ./test/abc ATTRIB

 

四、inotify 系統參數 設定

在/proc/sys/fs/inotify目錄下有三個檔案,對inotify機制有一定的限制
max_user_watches:設定inotifywait或inotifywatch命令可以監視的檔案數量(單進程)
max_user_instances:設定每個使用者可以啟動並執行inotifywait或inotifywatch命令的進程數
max_queued_events:設定inotify執行個體事件(event)隊列可容納的事件數目量。

 

五、inotify+rsync

Rsync(remote sync)遠程同步工具,通過rsync可以實現對遠程伺服器資料的增量備份同步,但rsync自身也有瓶頸,同步資料時,rsync採用核心演算法對遠程伺服器的目標檔案進行比對,只進行差異同步。

我們可以想象一下,如果伺服器的檔案數量達到了百萬甚至千萬量級,那麼檔案對比將是非常耗時的。而且發生變化的往往是其中很少的一部分,這是非常低效的方式。
inotify的出現,可以緩解rsync不足之處,取長補短

 

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.