rsync+inotify

inotify + rsync實現linux文件實時同步,使用觸發同步機制
公司一套系統的同步使用的donotify,不能實現子目錄的實時同步,通過查資料,發現inotify可以實現子目錄的實時同步,以下爲筆記。

一、介紹
Inotify 是文件系統事件監控機制,作爲 dnotify 的有效替代。dnotify 是較早內核支持的文件監控機制。Inotify 是一種強大的、細粒度的、異步的機制,它滿足各種各樣的文件監控需要,不僅限於安全和性能。

inotify 可以監視的文件系統事件包括:
IN_ACCESS,即文件被訪問
IN_MODIFY,文件被 write
IN_ATTRIB,文件屬性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可寫文件被 close
IN_CLOSE_NOWRITE,不可寫文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移來,如 mv、cp
IN_CREATE,創建新文件
IN_DELETE,文件被刪除,如 rm
IN_DELETE_SELF,自刪除,即一個可執行文件在執行時刪除自己
IN_MOVE_SELF,自移動,即一個可執行文件在執行時移動自己
IN_UNMOUNT,宿主文件系統被 umount
IN_CLOSE,文件被關閉,等同於(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移動,等同於(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所說的文件也包括目錄。 


二、爲能在shell下使用inotify特性,需要安裝inotify-tools

1、inotify-tools:The general purpose of this package is to allow inotify's features to be used from within shell scripts.

下載地址:[url]http://inotify-tools.sourceforge.net/[/url]

編譯安裝
./configure
make
make install
完成後,注意查看manpage,man inotify 、 man inotifywait


  • inotifywait 僅執行阻塞,等待 inotify 事件。您可以監控任何一組文件和目錄,或監控整個目錄樹(目錄、子目錄、子目錄的子目錄等等)。在 shell 腳本中使用 inotifywait
  • inotifywatch 收集關於被監視的文件系統的統計數據,包括每個 inotify 事件發生多少次。

  • 2、inotify的系統相關參數:
     /proc interfaces
           The following interfaces can be used to limit the amount of kernel memory consumed by inotify:

           /proc/sys/fs/inotify/max_queued_events
                  The value in this file is used when an application calls inotify_init(2) to set an upper  limit  on  the number  of  events  that  can be queued to the corresponding inotify instance.  Events in excess of this limit are dropped, but an IN_Q_OVERFLOW event is always generated.

           /proc/sys/fs/inotify/max_user_instances
                  This specifies an upper limit the number of inotify instances that can be created per real user ID.

           /proc/sys/fs/inotify/max_user_watches
                  This specifies a limit the number of watches that can be associated with each inotify instance.


    3、inotifywait 相關的參數(更多,查看manpage):
    inotifywait
    This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entire directory trees.
    -m, --monitor
                  Instead  of  exiting  after receiving a single event, execute indefinitely.  The default behaviour is to exit after the first event occurs.
    -r, --recursive
                  Watch all subdirectories of any directories passed as arguments.  Watches will be set up recursively  to an  unlimited  depth.   Symbolic  links  are  not 

    traversed.  Newly created subdirectories will also be watched.
    -q, --quiet
                  If specified ce, the program will be less verbose.  Specifically, it will not state when it  has  completed establishing all inotify watches.
     -e <event>, --event <event>
                  Listen for specific event(s) ly.  The events which can be listened for are listed in the  EVENTS  section.  This option can be specified more than ce.  If omitted, all events are listened for. use“,”separate multi events


    三、使用
    1.查看是否支持inotify,從kernel 2.6.13開始正式併入內核,RHEL5已經支持。
    看看是否有 /proc/sys/fs/inotify/目錄,以確定內核是否支持inotify
    [root@RHEL5 Rsync]# ll /proc/sys/fs/inotify
    total 0
    -rw-r--r-- 1 root root 0 Oct  9 09:36 max_queued_events
    -rw-r--r-- 1 root root 0 Oct  9 09:36 max_user_instances
    -rw-r--r-- 1 root root 0 Oct  9 09:36 max_user_watches

    2.關於遞歸:
    inotifywait
    This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entire directory trees.


    3.使用:
    #!/bin/sh
    src=/opt/webmail
    des=/tmp
    ip=192.168.7.192

    /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' \
     -e modify,delete,create,attrib \
    ${src} \
    | while read  file
            do
                    rsync -avz --delete --progress ${src} root@${ip}:${des} &&
                    echo "${file} was rsynced"
                    echo "---------------------------------------------------------------------------"
            done
    注:
    當要排出同步某個目錄時,爲rsync添加--exculde=PATTERN參數,注意,路徑是相對路徑。詳細查看man rsync
    當要排除都某個目錄的事件監控的處理時,爲inotifywait添加--exclude或--excludei參數。詳細查看man inotifywait

    另:
    /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' \
     -e modify,delete,create,attrib \
    ${src} \
    上面的命令返回的值類似於:
    10/03/09 15:31 /wwwpic/1
    這3個返回值做爲參數傳給read,關於此處,有人是這樣寫的:
    inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F;do
    細化了返回值。



    說明: 當文件系統發現指定目錄下有如上的條件的時候就觸發相應的指令,是一種主動告之的而非我用循環比較目錄下的文件的異動,該程序在運行時,更改目錄內的文件時系統內核會發送一個信號,這個信號會觸發運行rsync命令,這時會同步源目錄和目標目錄。
    --timefmt:指定輸出時的輸出格式
       --format:  '%T %w%f'指定輸出的格式,上面的輸出類似於:12/10/08 06:34 /opt/webmail/dovecot-1.1.2/src/test/1


    小腳本,同步到多臺主機:
     
     
    使用rsync+inotify配置觸發式(實時)遠程同步
    使用rsync+inotify配置觸發式(實時)遠程同步
     
    2008-11-01 TsengYia#126.com
     
    ################################################################
    系統環境:RHEL5 [ 2.6.18-8.el5xen ]
    軟件環境:
        rsync-2.6.8-3.1
        nfs-utils-1.0.9-16.el5
        portmap-4.0-65.2.2.1
     
    目標功能:
        源主機H1: 192.168.1.11/24
        目標主機H2: 192.168.1.12/24
        
        將H1主機中的開發數據(/var/devel/目錄),上傳同步至H2主機的/backup/devel/h1/目錄。——當源數據有文件或目錄更新時,即時啓動rsync同步進程。[基於安全性考慮,建議只在內部網絡中使用]
     
    ################################################################
        除inotify-tools(需要2.6.13以上內核的inotify功能支持)以外,其他軟件均使用RHEL5系統自帶的rpm包安裝。
     
    一、配置目標主機H2(發佈NFS可寫共享)
        
    shell> mkdir -p /backup/devel/h1/
    shell> vi /etc/exports
    /backup/devel/h1    192.168.1.11(rw,no_root_squash)
    shell> service portmap start
    shell> service nfs start
    shell> chkconfig portmap 
    shell> chkconfig nfs
     
      如有必要,可以結合防火牆規則控制訪問權限
    shell> iptables -I INPUT -p tcp --dport 111 -j DROP
    shell> iptables -I INPUT -p tcp --dport 111 -s 192.168.1.11 -j ACCEPT
    shell> iptables -I INPUT -p udp --dport 111 -j DROP
    shell> iptables -I INPUT -p udp --dport 111 -s 192.168.1.11 -j ACCEPT
     

    二、配置源主機H1(上傳備份發起端)
     
        1、安裝inotify-tools工具包
    shell> tar zxvf inotify-tools-3.13.tar.gz -C /usr/src/
    shell> cd /usr/src/inotify-tools-3.13
    shell> ./configure
    shell> make
    shell> make install
    —— 可以使用man inotify、man inotifywait、man inotifywatch查看相關手冊頁。
     
        2、掛載H2發佈的備份目錄
    shell> service portmap start
    shell> chkconfig portmap
    shell> mkdir -p /media/h2nfsdir/
    shell> vi /etc/fstab
    192.168.0.12:/backup/devel/h1    /media/h2nfsdir    nfs    defaults,noexec    0 0
    shell> mount /media/h2nfsdir
     
        3、編寫觸發同步腳本
    shell> vi /opt/h1-h2_inosync.sh
    #!/bin/sh
    SRC=/var/devel/
    DST=/media/h2nfsdir/
    INWT=/usr/local/bin/inotifywait
    RSYNC=/usr/bin/rsync
    $INWT -mrq -e create,move,delete,modify $SRC | while read D E F ; do
        $RSYNC -aHqz --delete $SRC $DST
    done
    shell> chkmod +x /opt/h1-h2_inosync.sh
     
        4、每次開機自動運行監控腳本
    shell> echo "/opt/h1-h2_inosync.sh &" >> /etc/rc.local
    shell> /opt/h1-h2_inosync.sh &
     

    三、測試實時同步
        在源主機H1上,修改/var/devel/目錄中的內容(如增、刪、改文件,添加、移除目錄等),
        ——同時在目標主機H2上,觀察備份目錄/backup/devel/h1/中內容的變化。
     
    ############################## The End ##################################
    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章