博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rsync同步和备份文件到本地
阅读量:7231 次
发布时间:2019-06-29

本文共 2566 字,大约阅读时间需要 8 分钟。

转载请注明文章出处:

rsync是主机间同步和备份的神器。相对于ftpscp等工具,rsync功能更强大,同步/传输效率更高,实属服务器的必备工具。

最近使用rsync时发现一个问题:PC和移动硬盘之间用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 will not use deltas but will transmit the full file in its entirety if it - as a single process - is responsible for the source and destination files. It can transmit deltas when there is a separate client and server process running on the source and destination machines.

The reason that rsync will not send deltas when it is the only process is that in order to determine whether it needs to send a delta it needs to read the source and destination files. By the time it's done that it might as well have just copied the file directly.

翻译过来是:主机间通过网络同步文件,每个主机各运行一个rsync进程分别本机内的文件hash,然后通过网络传输差异部分;主机内的同步只有一个进程,rsync认为与其先对比文件再复制差异部分,不如直接进行复制来得快,故而选择传送整个文件。

仔细想一下,rsync的行为是合理的:主机间通讯的瓶颈在网络带宽,先计算差异部分再传效率高;同主机内是硬盘对拷,速度是网络速度的十来倍,直接拷贝比一般比先对比再传输更快,直接复制整个文件是很好的选择。

写了个脚本测试rsync的行为:

<pre>#!/bin/bash

echo "make test file"
dd if=/dev/zero of=testfile bs=1024k count=512
echo "cp test file"
cp testfile syncfile
echo "make changes to test file"
echo '1234567890' >> testfile
echo "rsync file in local..."
rsync -avh -P testfile syncfile

echo ""

echo "restore sync file"
dd if=/dev/zero of=syncfile bs=1024k count=512
echo "rsync file via network"
rsync -avh -P testfile localhost:~/syncfile
</pre>

测试脚本输出结果如下:

; alt="" width="665" height="444" class="aligncenter size-large wp-image-3233" />

结果和预期的一致:本机内同步时,直接全量复制;走SSH协议后,仅发送差异部分,显著提高效率。

rsync的做法没毛病,但仅做过小部分修改的大文件,同主机内全量拷贝也很伤人。解决办法是用测试脚本内的模拟网络传输。Linux系统的主机基本都内置SSHD,写命令时加上localhost和代表网络的冒号即可;Windows 10的1809版本上,OpenSSH已经成为系统的内置组建,安装和使用也省心。此外有CygwinBitvise SSH Server等可供选择,安装好后也同步大文件也不再是问题。

另一个需要注意的问题是跨分区或设备进行同步时,文件系统应当互相兼容,否则可能会出现问题。例如从NTFS文件系统向(ex)FAT优盘同步文件,使用常用的-avhP选项,每次同步都会将所有文件复制一遍。问题在于两个文件系统支持的功能不同,FAT不支持-l-p等功能,加上这些选项会让rsync判断为两个不同的文件,从而再次复制。针对这种情况,要使用-cvrhP选项。

参考

你可能感兴趣的文章
mysql中key 、primary key 、unique key 与index区别
查看>>
zabbix使用企业微信发送告警信息
查看>>
zabbix4.0离线快速编译安装(编译安装方法)
查看>>
[Java开发之路](7)RandomAccessFile类详解
查看>>
Linux中的tty与pts
查看>>
Java socket示例(demo)TCP/IP
查看>>
error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches
查看>>
计算UITextView的滑动高度
查看>>
AngularJs的UI组件ui-Bootstrap分享(四)——Datepicker Popup
查看>>
Java虚拟机------垃圾收集器
查看>>
UVA 1376 Animal Run 最短路
查看>>
oracle12c之 单机12.1.0.1打补丁
查看>>
封装了集中常用的文件读的方法
查看>>
51Nod-1080 两个数的平方和【暴力法】
查看>>
Web开发实用网站资源
查看>>
“深入理解”—交换排序算法
查看>>
ng-cordova 手机拍照或从相册选择图片
查看>>
ARM 汇编指令集 特点之一:条件执行后缀
查看>>
软工第五次作业--原型设计(结对)
查看>>
优化PartialRenderFormMixin性能
查看>>