sh 监视文件更改并同步两个目录(通过ssh远程)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 监视文件更改并同步两个目录(通过ssh远程)相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#
# Sync local directory to remote directory.
#
# Modified by: Leo Mao
# Modified from: https://gist.github.com/evgenius/6019316
#
# Requires Linux, bash, inotifywait and rsync.
#
# To avoid executing the command multiple times when a sequence of
# events happen, the script waits one second after the change - if
# more changes happen, the timeout is extended by a second again.
#
# Example usage:
#    
#    watchsync.sh . host:/remote/dir
#
# Released to Public Domain. Use it as you like.

EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO"
RSYNC_OPTIONS="-Cait"

if [ -z "$2" ]; then
  echo "Usage: $0 /path/to/localdir [user@]hostname:/path/to/dir"
  exit -1;
fi

localdir=$1
remotedir=$2

run() {
  rsync $RSYNC_OPTIONS $localdir $remotedir
}

run

inotifywait -q -e "$EVENTS" -m -r --format '%:e %f' $1 | (
  WAITING="";
  while true; do
    LINE="";
    read -t 1 LINE;
    if test -z "$LINE"; then
      if test ! -z "$WAITING"; then
        echo "CHANGE";
        WAITING="";
      fi;
    else
      WAITING=1;
    fi;
  done) | (
  while true; do
    read TMP;
    run
  done
)

以上是关于sh 监视文件更改并同步两个目录(通过ssh远程)的主要内容,如果未能解决你的问题,请参考以下文章

Linux下通过ssh来执行rsync同步远程文件

sh 通过ssh快速远程目录rsync

GIT入门笔记(14)- 链接到远程仓库

rsync工具介绍,rsync常用选项,rsync通过ssh同步

无密码通过ssh执行rsync

更改ssh远程登录端口.sh