如何检查 Linux 中目录的上次修改时间

如何检查 Linux 中目录的上次修改时间

我正在尝试编写一个备份解决方案,它将使用硬链接和 rsync 拍摄文件夹的快照,每小时运行一次并拍摄快照。我已经整理了大部分内容,只是缺少一些小细节。

我已经在计划一个系统,该系统将根据备份之间的间隔和时间长短,以指数退避的方式清除旧备份。因此,保存每小时备份不会占用太多磁盘空间。(每个备份的时间除以它之后的备份...)

不过,我将在一台通常不会更改备份的机器上运行此程序(夜间、人们度假时等),因此,我想考虑到这一点。即使只是为了省电。

有什么方法可以知道整个 /home/ 目录的最后修改时间吗?

最好不要完全遍历目录,因为这会有点违背要点。

有没有更好的方法可以做到这一点?

答案1

Rsync 可以使用标志来实现此功能-u or --update

来自 rsync 手册页:

-u, --update
              This  forces rsync to skip any files which exist on the destinaâ
              tion and have a modified time that  is  newer  than  the  source
              file.   (If an existing destination file has a modify time equal
              to the source fileâs, it will be updated if the sizes  are  
              different.)

              In  the current implementation of --update, a difference of file
              format between the sender and receiver is always  considered  to
              be important enough for an update, no matter what date is on the
              objects.  In other words, if the source has  a  directory  or  a
              symlink  where  the  destination  has a file, the transfer would
              occur regardless of the timestamps.  This might  change  in  the
              future  (feel free to comment on this on the mailing list if you
              have an opinion).

如果您不想使用 rsync,我可以想到另外两种方法来获取此信息。但这两种方法都需要您深入研究文件系统。

首先是比较丑陋的方式:

使用 statstat --format=%y <file>并解析输出的时间。您必须做一些非常丑陋的事情才能使其正常工作。

第二种更巧妙的方法:

用于find <path> -mtime -<number_of_days>获取在 number_of_days 前窗口内修改的文件列表。


回复:省电

这需要一些黑客攻击,而且不是 100% 可靠 - 从某种意义上说,您可能会错过需要备份的内容。但您可以执行以下类似操作,并且在某些窗口期间不针对主驱动器运行:

if [ `date +%k` -lt 22] && [`date +%k` -gt 8] && [ `date +%u` -le 5 ]
then
     #Set some flags for run control.
fi 

第一个日期测试检查是否在晚上 10 点之前,第二个日期测试检查是否在早上 8 点之后,最后一个日期测试检查星期几是否小于或等于 5(date +%u输出星期几从 1-7,其中 1 表示星期一,因此 6 和 7 表示星期六和星期日)

至于假期,我唯一能想到的办法就是为每个假期维护一个文件,其中包含一年中的天数,然后进行匹配date +%j

相关内容