龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 操作系统 > LINUX系统 >

Inotify+Rsync实现linux文件实时同步(2)

时间:2014-05-31 15:21来源:网络整理 作者:网络 点击:
分享到:
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.使用:

  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
"${src} 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细化了返回值。

精彩图集

赞助商链接