安装

rsync 是 remote sync 的意思,本文讨论的使用场景还是在本地拷贝文件,用来替代 cp 和 mv。如果需要通过 rsync 同步其他服务器中的文件,请看文末的引用文章。

rsync - 3.2.3-3 - rsync is a program that allows files to be copied to and from remote machines in much the same way as rcp. It has many more options than rcp, and uses the rsync remote-update protocol to greatly speed up file transfers when the destination file already exists. The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network link.

安装的 3.2.3-3 版本。

opkg install rsync

报错,提示有依赖问题。

root@dkRouter:~# opkg install rsync
Installing rsync (3.2.3-3) to root...
Downloading https://openwrt.proxy.ustclug.org/snapshots/packages/x86_64/packages/rsync_3.2.3-3_x86_64.ipk
Installing libpopt0 (1.16-2) to root...
Downloading https://openwrt.proxy.ustclug.org/snapshots/packages/x86_64/base/libpopt0_1.16-2_x86_64.ipk
Collected errors:
 * check_data_file_clashes: Package libpopt0 wants to install file /usr/lib/libpopt.so.0
    But that file is already provided by package  * libpopt
 * check_data_file_clashes: Package libpopt0 wants to install file /usr/lib/libpopt.so.0.0.0
    But that file is already provided by package  * libpopt
 * opkg_install_cmd: Cannot install package rsync.

我们在安装时增加 --nodeps flag(代表Do not follow dependencies,不跟踪依赖),再次尝试安装。

root@dkRouter:~# opkg install rsync --nodeps
Installing rsync (3.2.3-3) to root...
Downloading https://openwrt.proxy.ustclug.org/snapshots/packages/x86_64/packages/rsync_3.2.3-3_x86_64.ipk
Configuring rsync.

成功安装。

使用基本教程

rsync 基本用法:rsync -r /file/source/path /file/destination/path

root@dkRouter:~# rsync -anv /root/rsync.log /home/dk/
sending incremental file list
rsync.log

sent 66 bytes  received 19 bytes  170.00 bytes/sec
total size is 2,168  speedup is 25.51 (DRY RUN)
  • -a 参数是比-r 参数更合理、更建议使用的参数,能够复制原始文件的元信息(比如拥有者、创建时间、最近一次修改时间等),-r只能递归复制
  • -n 是测试,执行后这条复制命令不会被真的执行,相当于一次演示
  • -v,啰嗦模式输出,输出相关日志

平常使用的建议使用 -av 参数。

另外需要注意的是,rsync 默认拷贝以点开头的隐藏文件(比如/root/.bashrc这样子的文件),如果不需要拷贝隐藏文件需要使用--exclude 参数排除掉

可以在命令后面跟上 --exclude=".*"

另外还有一点比较值得注意,同步文件时,假如source是一个文件夹,那么最后一个文件夹带不带斜杠在同步到目标路径后会有一些区别。

情形1: rsync -a /file/source/path1 /file/destination/path2

情形1中,最终在path2中形成的文件夹结构是 /file/destination/path2/path1

情形2: rsync -a /file/source/path1/ /file/destination/path2

情形2中,最终将 path1 文件夹里面的所有文件都拷贝到了 path2中。形成的文件夹结构就是 /file/destination/path2

高阶用法

感谢阮一峰老师的 rsync 用法教程

end.