知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
-m 是保持一直監聽
-r 是遞歸查看目錄
-q 是打印出事件~
-e create,move,delete,modify 監聽 創建 移動 刪除 寫入 事件
inotifywait -mrq --event create,delete,modify,move --format '%w %e' /your_path | while read w e; do if [ "$e" = "IGNORED" ]; then continue fi rsync -az --delete $w username@your_ip:$w done
#!/bin/sh # A slightly complex but actually useful example inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %f' \ -e close_write /home/billy | while read date time file; do rsync /home/billy/${file} rsync://billy@example.com/backup/${file} && \ echo "At ${time} on ${date}, file ${file} was backed up via rsync" done
[root@development ~]# cat inotify-rsync #!/bin/bash # $Id: chapter.storage.inotify.xml 334 2012-02-01 05:59:34Z netkiller $ # # Author neo<openunix@163.com> # # monitor path monitor_path=cms #inotifywait path INOTIFYWAIT=inotifywait # rsync image file function images { local file=$1 rsync -az --delete $file /tmp/images/$file # rsync ${file} ${rsync_url}/${file} } # rsync html file function html { local file=$1 rsync -az --delete $file /tmp/$file } $INOTIFYWAIT -mrq --event close_write --format '%w%f %e' $monitor_path | while read file event; do if [ "$event" = "CLOSE_WRITE,CLOSE" ]; then ext=$(echo $file | awk -F'.' '{print $2}') if [ $ext = 'jpg' ]; then images $file fi if [ $ext = 'html' ]; then html $file fi fi done &