利用 inotifywait 實現自動增量文件拷貝


#!/bin/bash
src=/www/wwwroot/site0/images/
dest=/www/wwwroot/site1/images/
tool=/user/bin/mytool

inotifywait -mrq  --format '%w%f' -e create $src |  while read file
do
      sleep 1  # 從 create 到寫好,有一個過程,等一下更安全
      
      rpath=$dest/${file:26}  # 26 是 src 的字符串長度

      echo "create file $rpath"

      if test -d $file ; then
          mkdir $rpath
          echo "create $rpath"
      else
        cp $file $rpath
        $tool $rpath >>  /tmp/image_sync.log 2>&1
        echo "cp and process $rpath"
      fi
      echo "`date`  ${file} => ${rpath}" >> /tmp/image_sync.log 2>&1
done

監控 src 目錄,出現新文件時立即複製到 dest 目錄並使用 mytool 工具處理目標文件。

應用場景:

  1. src 目錄的文件增量拷貝到 dest 目錄並加密。
  2. 對 src 目錄進行自動同步到遠程機器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章