0%

使用rsync远程备份数据

使用rsync利器远程备份数据。

关于rsync,来自man的信息:

Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

简单来说:rsync是一个将数据从一个地方复制或备份到另一个地方的工具。这个工具效率很高,因为在复制或备份过程中,它会比较源目录和目的目录的内容,只复制和备份不同的文件。

其用法为:

1
$rsync [OPTION]  SRC  DEST

我们希望利用ssh定时备份指定目录的数据,先启动守护程序:

1
crontab -e

在其中增加一行:

1
30 12 * * * sshpass -p "passwd" rsync -az /the/source/direcotry username@xxx.xxx.xxx.xxx:/the/dest/directory

我们希望每天中午12:30备份本地
/the/source/direcotry 目录到ip地址为xxx.xxx.xxx.xxx 的远程服务器/the/dest/directory/目录中,ssh登录用户名为username,密码为passwd

为了自动登录ssh,我们使用了sshpass传递ssh登录密码,这是一个安全缺陷。选项a表示拷贝过程中保留日期时间、文件权限等信息,选项z表示拷贝过程中压缩文件。