#!/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 目录进行自动同步到远程机器