标签归档:inotify

高效的简洁的inotify+rsync同步代码

#!/bin/bash
inotifywait -mrq /mnt/www/xxx --format "%w%f %e" -e modify,attrib,close_write,moved_to,moved_from,move,create,delete,delete_self,unmount | while read line
do
  # echo $line
  e=`echo ${line##* }`
  f=`echo ${line% *}`
  if [[ "$f" =~ "/mnt/www/xxx/patchs/" ]] || [[ "$f" =~ "/mnt/www/xxx/release/releases/" ]]; then
    # 排除两个不需要同步的目录
    continue
  fi
  if [[ ! -e "$f" ]]; then
    continue
  fi
  if [[ "$e" =~ "MOVED_TO" ]] || [[ "$e" =~ "CLOSE_WRITE" ]] || [[ "$e" =~ "ATTRIB" ]] || [[ "$e" =~ "CREATE,ISDIR" ]]; then
    rsync -a --delete-after $f f22@host1:$f
    rsync -a --delete-after $f f22@host2:$f
  elif [[ "$e" =~ "DELETE" ]] || [[ "$e" =~ "MOVED_FROM" ]]; then
    rsync --delete -a $f f22@host1:$f
    rsync --delete -a $f f22@host2:$f
  fi
done