检测目录中是否有某些内容被修改,如果是,则备份 - 否则不执行任何操作

发布于 2024-09-26 15:51:11 字数 371 浏览 5 评论 0原文

我有一个“数据”目录,我通过 shell 脚本定期同步到远程 NAS。

但是,我想让这变得更有效率。我想在运行 rsync 之前检测“数据”中是否发生了变化。这样我就不会不必要地唤醒 NAS 上的驱动器。

我正在考虑修改 shell 脚本以获取 Data 中文件的最新修改时间(通过使用递归查找),并在每次同步 Data 时将其写入文件。

每次同步之前,shell 脚本可以将“Data”的当前时间戳与同步“Data”时的前一个时间戳进行比较。如果当前时间戳较新,则 rsync,否则不执行任何操作。

我的问题是,是否有更有效的方法来确定自上次 rsync 以来“Data”目录是否被修改?请注意,Data 有很多很多层子目录。

I have a "Data" directory, that I rsync to a remote NAS periodically via a shell script.

However, I'd like to make this more efficient. I'd like to detect if something has changed in "Data" before running rsync. This is so that I don't wake up the drives on the NAS unnecessarily.

I was thinking of modifying the shell script to get the latest modified time of the files in Data (by using a recursive find), and write that to a file every time Data is rsynced.

Before every sync, the shell script can compare the current timestamp of "Data" with the previous timestamp when "Data" was sync'd. If the current timestamp is newer, then rsync, otherwise do nothing.

My question is, is there a more efficient way to figure out if the "Data" directory is modified since the last rsync? Note that Data has many, many, layers of sub-directories.

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

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

发布评论

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

评论(3

梦初启 2024-10-03 15:51:11

如果我理解正确的话,您只是想查看是否有任何文件被修改,以便您可以确定是否继续执行脚本的 rsync 部分?

确定数据上次同步的时间是一项非常简单的任务,特别是如果您每晚执行此操作。一旦您发现一个文件的 mtime 大于上次同步的时间,您就知道必须继续进行完整的 rsync。

find 内置了此功能:

# find all files modified in the last 24 hours
find -mtime 1

If I understand correctly, you just want to see if any files have been modified so you can figure out whether to proceed to the rsync portion of your script?

It's a pretty simple task to figure out when the data was last synced, especially if you do this nightly. As soon as you find one file with mtime greater than the time of the last sync, you know you have to proceed to the full rsync.

find has this functionality built in:

# find all files modified in the last 24 hours
find -mtime 1
抚你发端 2024-10-03 15:51:11

Rsync 已经做到了这一点。不存在不需要检查 inode 的 mtime 和 ctime 属性的按需解决方案。

但是,您可以创建一个守护程序,使用 inotify 来跟踪发生的更改,并每隔一段时间或在您认为发生了足够的事件以证明调用 rsync 的合理性时触发 rsync。

Rsync already does this. There is no on-demand solution that doesn't require checking the mtime and ctime properties of the inodes.

However you could create a daemon that uses inotify to track changes as they occur, and fire rsync at intervals, or whenever you feel sufficient events have occurred to justify calling rsync.

薯片软お妹 2024-10-03 15:51:11

我会使用 find 命令,但这样做:当 rsync 运行时,触摸一个文件,例如“rsyncranflag”。然后你可以运行

find Data -newer rsyncranflag

这将明确说明自上次rsync以来是否有任何文件被更改(取决于mtime的准确性)。

I would use the find command, but do it this way: When the rsync runs, touch a file, like "rsyncranflag". Then you can run

find Data -newer rsyncranflag

That will say definitively whether any files were changed since the last rsync (subject to the accuracy of mtime).

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