Linux rsync 可以在同步前验证内容吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7745357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:39:43  来源:igfitidea点击:

Can rsync verify contents before syncing

linuxfilechecksumrsync

提问by Arjunan

Can Rsync be configured to verify the contents of the file before they are being synced. I have heard about checksum, but I came to know that checksum only does a sampling. I want to transfer a file only if it is contents are changed and not timestamp, is there a way to do it with any of the rsync modes. In my scenario, say file sample.text will be created every week and I want to sync it with a remote server only if the contents of sample.text are changed, since it is created every week, the time stamp would obviously change. But I want the transfer only on a content change.

可以将 Rsync 配置为在同步之前验证文件的内容。我听说过校验和,但我开始知道校验和只进行采样。我只想在文件内容更改而不是时间戳时传输文件,有没有办法使用任何 rsync 模式来完成它。在我的场景中,假设文件 sample.text 将每周创建一次,我想仅当 sample.text 的内容发生更改时才将其与远程服务器同步,因为它每周创建一次,时间戳显然会发生变化。但我只希望在内容更改时进行传输。

采纳答案by Nthalk

Yes:

是的:

$ man rsync | grep "\--checksum"
    -c, --checksum              skip based on checksum, not mod-time & size

Rsync is pretty complicated. I recommend cuddle time with the man page and experimentation with test data before using it for anything remotely important.

Rsync 相当复杂。我建议在将其用于任何远程重要的事情之前,先阅读手册页并尝试使用测试数据。

Most of the time, people use rsync -ac source dest.

大多数时候,人们使用rsync -ac source dest.

$ man rsync | grep "\--archive"
 -a, --archive               archive mode; same as -rlptgoD (no -H)

And that -rlptgoDgarbage means: recursive(r), copy symlinks as symlinks(l), preserve permissions(p), preserve times(t), preserve group(g), preserve owner(o), preserve device files(D, super-user only), preserve special files(also part of D).

-rlptgoD垃圾是指:递归r),复制符号链接作为符号链接l),保存权限p),保存时间t),保存组g),维护所有者o),保存设备文件D只有超级用户),保存特殊文件(也是 的一部分D)。

The -cor --checksumis really what you are looking for (skip based on checksum, not mod-time & size). Your supposition that rsync only samples mtime and size is wrong.

-c--checksum真的是你正在寻找(跳过基于校验,不MOD-时间和大小)。您认为 rsync 仅对 mtime 和 size 进行采样的假设是错误的。

回答by Alex Howansky

See the --checksumoption on the rsync man page.

请参阅--checksumrsync 手册页上的选项。

Also, the --size-onlyoption will be a faster choice if you know for sure that a change of contents also means a change of size.

此外,--size-only如果您确定内容的更改也意味着大小的更改,则该选项将是一个更快的选择。

回答by oldbowman

Example:

例子:

#!/bin/bash
echo > diff.txt
rsync -rvcn --delete /source/ /destination/ > diff.txt
second_row=$(sed -n '2p' diff.txt)
if [ "$second_row" = "" ]; then
    echo there was no change
else 
    echo there was change   
fi