GaoWhen高H温

rsync + ssh 同步本地文件夹至服务器

之前有介绍过使用 dropbox来同步 vimwiki 随时随地的 !VimWiki。之前觉得这样就足够了,没必要放到网络上。不过最近越来越觉得个人知识管理 wiki 重要了,不过本机的文件之前没规划好,wiki 模版也一直没做,有点懒散。既然是知识,何不共享出来,也能敦促自己多学习、勤整理。不过觉得发布的时候有些麻烦,之前看到 明城 的 wiki 使用 rsync 同步的,于是我也折腾了下。以下是使用 rsync 和 ssh 同步本地文件到服务器(bluehost)的方法。其他服务器不清楚,bluehost 是默认安装了 rsync 的。

Mactonish 安装 rsync

获取 rysnc

首先去 rsync 首页 获取最新的源码包,当前是 3.0.7 版本。然后就是编译、安装,前提同样需要 c 编译器(如 Xcode)。

同步

以下另存为 rsync-example.sh 。
# 设置一个 lock 文件,防止多次运行
LOCK_FILE="/tmp/rsync_backup.lock"
LOG_FILE="/tmp/rsync_backup.log"
# rsync 路径
RSYNC="/usr/local/bin/rsync"

if [ -e $LOCK_FILE ]; then
	TIMESTAMP=$(date)
	echo "$TIMESTAMP: lock file exists, exit now" >> $LOG_FILE
	exit
fi

cat /dev/null > $LOG_FILE

touch $LOCK_FILE

# 需要自己定制的命令
rsync -avz --rsh="ssh -l user" local/folder user@remotehost:remote/folder

rm $LOCK_FILE

赋予它可执行权限

chmod +x rsync-example.sh

执行

./rsync-example.sh

不用这个脚本,直接用其中的命令 rsync -avz --rsh="ssh -l user" local/folder user@remotehost:remote/folder 也是一样。

参考资料

One Response to rsync + ssh 同步本地文件夹至服务器

  1. Shawphy says:

    你写的好抽象,看完基本没懂,还得继续参考别的文章区。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>