OS X / LINUX使用RSYNC拷贝远程服务器文件到本地

  • Post author:
  • Post category:linux



这篇博客翻译自

http://biodegradablegeek.com/category/linux/

下边部分的代码使用Rsync从远程服务器拷贝文件到本地机器,从而使本地和远程服务器的文件保持同步。这是一个备份web服务器重要目录到外部硬盘或者其他什么地方的有用技巧。

The following is used to copy files from a remote server to your local machine using Rsync to keep both synchronized. This is useful for things like backing up important directories on your web server to your external hard drive or elsewhere.

Rsync可以在本地和远程服务器之间文件增量备份,也就是说只是同步你的远程服务器上更新了的文件到本地服务器。

Rsync can incrementally synchronize files between two locations whether local or remote. This means only the updated files on your remote server are updated on your local machine.

从接下来的部分我假设你和我一样也是使用OS X, 但是这个脚本应该也能在Linux工作。 首先需要打开一个控制台(就是那个令人讨厌的黑框框),在命令行的提示符下,输入一下的代码:

From here on in I assume you’re using OS X but this should work on Linux as well. Press CMD+Space to open a Terminal (I use TotalTerminal.) At the command prompt, just use the following:
#!/bin/sh
rsync -a -e "ssh" --rsync-path="sudo rsync -vau " remoteusername@remote.host.com:/home .
rsync -a -e "ssh" --rsync-path="sudo rsync -vau " remoteusername@remote.host.com:/var .
rsync -a -e "ssh" --rsync-path="sudo rsync -vau " remoteusername@remote.host.com:/etc .

这个脚本执行的结果是拷贝你远程服务器的/home,/var, /etc目录到当前目录。

This will copy /home, /var, and /etc from your remote machine to the current directory.


可以把上述的代码拷贝到一个文件,可以命名成rsyncRemote.sh, 并使用下述的命令执行之可以实现相同的效果

# should under same directory with rsyncRemote.sh
chmod a+x rsyncRemote.sh
./rsyncRemote.sh