标签归档:rsync

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

#!/bin/bash
inotifywait -mrq /mnt/www/xxxx--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 [[ ! -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
  # echo $e
  # echo $f
done