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

发布于 2024-12-09 09:47:02 字数 207 浏览 0 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘味果▽酱 2024-12-16 09:47:02

是的:

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

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

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

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

-rlptgoD 垃圾意味着:递归 (r),将符号链接复制为符号链接 (l< /code>)、保留权限 (p)、保留时间 (t)、保留组 (g)、保留所有者 (o)、保留设备文件 (D ,仅限超级用户),保留特殊文件(也是D的一部分)。

-c--checksum 确实是您正在寻找的(根据校验和跳过,而不是 mod-time 和大小)。您认为 rsync 仅对 mtime 和 size 进行采样的假设是错误的。

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.

Most of the time, people use 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).

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.

苄①跕圉湢 2024-12-16 09:47:02

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

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

See the --checksum option on the rsync man page.

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.

下雨或天晴 2024-12-16 09:47:02

例子:

#!/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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文