wget https://sourceforge.net/projects/inotify-tools/files/inotify-tools/3.13/inotify-tools-3.13.tar.gz/download tar -zxvf inotify-tools-3.13.tar.gz ./configure make make install
举例 实时监控/root/test目录变化,并调用rsync同步w文件到远程目录脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/sh
# get the current path CURPATH=`pwd`
inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \ -e close_write /root/test | whileread date time dir file; do
FILECHANGE=${dir}${file} # convert absolute path to relative FILECHANGEREL=`echo"$FILECHANGE" | sed 's_'$CURPATH'/__'`
#rsync --progress --relative -vrae 'ssh -p 22' $FILECHANGEREL usernam@example.com:/backup/root/dir && \ ##同步的目录需注意,此处的$FILECHANGE是绝对路径,所以同步--relative参数是递归形势,不加此参数,所有文件包括子文件夹内的文件都会在同一级目录下 rsync --progress --relative -vzra --password-file=/etc/rsyncd.secrets $FILECHANGE rsync@192.168.3.2::test && \ echo"At ${time} on ${date}, file $FILECHANGE was backed up via rsync" done