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
Can rsync verify contents before syncing
提问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 -rlptgoD
garbage 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 -c
or --checksum
is 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 --checksum
option on the rsync man page.
请参阅--checksum
rsync 手册页上的选项。
Also, the --size-only
option 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