#!/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 工具處理目標文件。
應用場景:
- src 目錄的文件增量拷貝到 dest 目錄並加密。
- 對 src 目錄進行自動同步到遠程機器