rsync --inplace 是写入整个文件,还是只写入需要更新的部分?(用于 btrfs+rsync 备份)

Pet*_*etr 25 linux backup rsync btrfs

我正在阅读几篇指南,介绍如何将 btrfs 快照与 rsync 结合使用,以创建具有历史记录的高效备份解决方案。然而,这一切都取决于是rsync --inplace只修改实际更改的文件部分,还是按顺序覆盖整个文件。如果它写入整个文件,那么似乎 btrfs 将始终创建该文件的新副本,这会使该想法的效率大大降低。

dat*_*ess 37

如果您通过 rsync 两个本地路径,它将默认使用“--whole-file”,而不是增量传输。所以,你要找的是“--no-whole-file”。如果您要求'-c',您还可以获得增量传输。

以下是您可以验证的方法:

$ mkdir a b
$ dd if=/dev/zero of=a/1 bs=1k count=64
$ dd if=/dev/zero of=a/2 bs=1k count=64
$ dd if=/dev/zero of=a/3 bs=1k count=64
$ rsync -av a/ b/
sending incremental file list
./
1
2
3

sent 196831 bytes  received 72 bytes  393806.00 bytes/sec
total size is 196608  speedup is 1.00
Run Code Online (Sandbox Code Playgroud)

然后触摸一个文件并重新同步

$ touch a/1
$ rsync -av --inplace a/ b/
sending incremental file list
1

sent 65662 bytes  received 31 bytes  131386.00 bytes/sec
total size is 196608  speedup is 2.99
Run Code Online (Sandbox Code Playgroud)

您可以使用“ls -li”验证它是否重新使用了 inode,但请注意它发送了整个 64K 字节。再试一次 --no-whole-file

$ touch a/1
$ rsync -av --inplace --no-whole-file a/ b/
sending incremental file list
1

sent 494 bytes  received 595 bytes  2178.00 bytes/sec
total size is 196608  speedup is 180.54
Run Code Online (Sandbox Code Playgroud)

现在您只发送了 494 个字节。您可以使用 strace 进一步验证是否写入了任何文件,但这表明它至少使用了增量传输。

请注意(请参阅注释),对于本地文件系统,--whole-file假定为(请参阅 rsync 的手册页)。另一方面,--no-whole-file假设跨网络,因此--inplace其自身将表现为--inplace --no-whole-file

  • @Geremia 如果两条路径都是本地的,则不会。我的例子表明,对于我在 2013 年使用的 rsync 版本,```--inplace``` 并不意味着 ```--no-whole-file```,但欢迎你重复这个实验使用您自己的 rsync 版本。 (2认同)

小智 16

这是我猜的明确答案,引用了手册的正确部分:

   --inplace

          [...]

          This option is useful for transferring large files
          with  block-based  changes  or  appended data, and
          also on systems that are disk bound,  not  network
          bound.   It  can  also  help  keep a copy-on-write
                                               *************
          filesystem snapshot from diverging the entire con?
          *******************
          tents of a file that only has minor changes.
Run Code Online (Sandbox Code Playgroud)


小智 5

rsync 的增量传输算法处理是传输整个文件还是仅传输不同的部分。这是在两台机器之间同步文件以节省带宽时的默认行为。这可以用--whole-file(或-W)覆盖以强制rsync传输整个文件。

--inplace处理rsync在传输过程中是否会创建一个临时文件。默认行为是创建一个临时文件。这提供了一种安全措施,因为如果传输中断,目标机器中的现有文件保持完整/不变。--inplace覆盖此行为并告诉rsync直接更新现有文件。这样,如果传输中断,您将面临目标计算机中文件不一致的风险。


小智 3

从手册页:

This  option  changes  how  rsync transfers a file when its data
needs to be updated: instead of the default method of creating a
new  copy  of  the file and moving it into place when it is com-
plete, rsync instead writes the updated  data  directly  to  the
destination file.
Run Code Online (Sandbox Code Playgroud)

这让我相信它会完整地写入文件——我想 rsync 几乎不可能以任何其他方式工作。

  • 在确定哪些部分需要更新后,它可以直接[搜索](https://en.wikipedia.org/wiki/Fseek#fseek)到这些部分并更新它们,而不是写入整个文件。 (2认同)