二十9、rsync+inotity實時監控同步工具

1、場景應用:                           git

        客戶經過url訪問資源(查詢,下載等),併發量是很是高的,因此運用負載均衡分擔web服務器的壓力,在後端鏈接不一樣的NFS備份服務器,一樣也是分擔壓力;那麼在同步nfs服務器上的資源的時候,咱們須要實時的同步到備份服務器上,這樣用戶才能使用這些資源,傳統的定時任務,咱們知道最快1分鐘,同步一次,這是沒法忍受的,因此咱們用inotity進行實時的同步。github

2、inotify+rsync組合的起源web

         Rsync遠程同步工具能夠進行數據的同步,可是在數據量很是龐大的今天,若是要實現兩邊的數據一致,rsync是支持的,那麼就要進行數據的對比,可是在對比中發現,變化的數據只是一小部分,並且對比又是比較耗時,因此這裏就出現了rsync的瓶頸,inotify的出現,緩解rsync的不足之處,實現實時同步。express

3、Inotify的工做機制vim

        Inotify是一種強大的,細粒度的,異步的文件系統事件監控機制。後端

       

 4、啓發:inotify能夠監控目錄的變化,那麼變化後,既然能夠觸發同步rsync,那麼一樣能夠觸發發送郵件、打電話等,用處多多!!!bash

 5、安裝inotify服務器

   1.首先inotify的實現軟件有不少,這裏說兩種:併發

      1)inotify自身    簡單                          2)sercync 國內軟件開發,功能強大,能夠作過濾負載均衡

      在性能上intofy大於sersyrc

   2.安裝前置條件:

     1)版本必須大於2.6.13

     2)有/proc/sys/fs/inotify

 

  3.源碼下載,源碼安裝:

  •      mkdir -p /home/oldboy/tools/
  •       cd  /home/oldboy/tools/
  •       wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --no-check-certificate
  •       tar xf inotify-tools-3.14.tar.gz
  •      cd inotify-tools-3.14
  •      ./configure --prefix=/usr/local/inotify-tools-3.14(安裝出錯,由於沒有安裝yum install gcc)-----》配置參數的安裝和安裝目錄的指定
  •       [root@djw1 inotify-tools-3.14]# echo $?      0   ====》檢查沒有出錯了!!!!
  •       make&&make install  -->編譯成機器認識的語言 make成功在進行make install 安裝
  •       ls  -s   /usr/local/inotify-tools-3.14   /usr/local/inotify   ---->軟連接

 6、配置參數 :cd /usr/local/inotify

   

     1)bin   inotify執行命令     2)include  inotify頭文件    3)lib  動態連接的庫文件(系統調用)    4)share 幫助文檔

       有兩個命令要注意;inotifywait(監聽事件)、inotifywatch

       inotifywait配置參數:建議用的時候熟練,多多看幫忙文檔,以下:

文檔參數:

 [root@djw1 inotify]# ./bin/inotifywait --help

inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
        -h|--help       Show this help text.
        @<file>         Exclude the specified file from being watched.
        --exclude <pattern>
                        Exclude all events on files matching the
                        extended regular expression <pattern>.
        --excludei <pattern>
                        Like --exclude but case insensitive.
        -m|--monitor    Keep listening for events forever.  Without
                        this option, inotifywait will exit after one
                        event is received.
        -d|--daemon     Same as --monitor, except run in the background
                        logging events to a file specified by --outfile.
                        Implies --syslog.
        -r|--recursive  Watch directories recursively.
        --fromfile <file>
                        Read files to watch from <file> or `-' for stdin.
        -o|--outfile <file>
                        Print events to <file> rather than stdout.
        -s|--syslog     Send errors to syslog rather than stderr.
        -q|--quiet      Print less (only print events).
        -qq             Print nothing (not even events).
        --format <fmt>  Print using a specified printf-like format
                        string; read the man page for more details.
        --timefmt <fmt> strftime-compatible format string for use with
                        %T in --format string.
        -c|--csv        Print events in CSV format.
        -t|--timeout <seconds>
                        When listening for a single event, time out after
                        waiting for an event for <seconds> seconds.
                        If <seconds> is 0, inotifywait will never time out.
        -e|--event <event1> [ -e|--event <event2> ... ]
                Listen for specific event(s).  If omitted, all events are
                listened for.
Exit status:
        0  -  An event you asked to watch for was received.
        1  -  An event you did not ask to watch for was received
              (usually delete_self or unmount), or some error occurred.
        2  -  The --timeout option was given and no events occurred
              in the specified interval of time.
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
        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
7、人工測試監聽事件:inotifywait
     命令:   /usr/local/inotify/bin/inotifywait  -mrq --timefmt '%Y/%m/%d %H:%M' --format '%T %w%f' -e create,delete,close_write  /backup
 

這裏要注意:close_write 監控的是寫入文件的內容,只要寫入就會被監聽到,可是若是創造文件,也是一種寫入,因此有create存在,就會有兩個事件,以下:

 8、簡單腳本:

[root@djw1 scripts]# vim inotify.sh  
#!/bin/bash
path="/usr/local/inotify/bin/inotifywait"
$path -mrq --format '%w%f' -e create,delete,close_write  /backup |\
while read line
do
  rsync -az $line  rsync_backup@192.168.0.103::oldboy --password-file=/etc/rsync.password
done

測試成功!!!

 9、inotity的缺點:

   1)併發不能大於200個文件(10-100k的文件)

相關文章
相關標籤/搜索