rsync 保持访问时间(atime),如何?

rsync 保持访问时间(atime),如何?

我在 Fedora 33 (GNOME) 上使用 rsync-3.2.3。

但如何保留atime文件和文件夹的访问时间 ( ) 呢?

我只能mtime用这个命令保留修改时间():

rsync -t

答案1

您可以要求保留atime(访问时间)来源带有该--noatime标志,但在安装有relatime(现代默认值)的文件系统上,或者noatime这已经不是绝对必要的

rsync -av --noatime src/ dstHost:dst/

我知道没有选项可以atime在目标上保留为rsync.如果您有权访问目标系统,您也许能够遍历复制的树。像这样的东西可以在 GNU/Linux 类型系统上工作

( cd src/ && find -type f -print0 ) |
    ssh dstHost 'cd dst && while IFS= read -r -d "" f; do  touch -a -d "@$(stat -c %Y "$f")" "$f"; done'

或者,如果您正在处理两个本地文件系统之间的副本

( cd src/ && find -type f -print0 ) |
    ( cd dst && while IFS= read -r -d "" f; do  touch -a -d "@$(stat -c %Y "$f")" "$f"; done )

基本上,这两个片段执行相同的操作:对于源中的每个文件,在目标中查找相应的文件并更新其atime以匹配其mtime.

答案2

从 rsync 3.2.0 版本开始,有两个影响 atimes 的标志:

  • --atimes, -U 保留访问(使用)时间
  • --open-noatime 避免更改打开文件的 atime

这些的完整描述是:

       --atimes, -U
              This  tells  rsync to set the access (use) times of the destina‐
              tion files to the same value as the source files.

              If repeated, it also sets the --open-noatime option,  which  can
              help you to make the sending and receiving systems have the same
              access times on the transferred files  without  needing  to  run
              rsync an extra time after a file is transferred.

              Note  that  some  older rsync versions (prior to 3.2.0) may have
              been built with a pre-release --atimes patch that does not imply
              --open-noatime when this option is repeated.

       --open-noatime
              This  tells rsync to open files with the O_NOATIME flag (on sys‐
              tems that support it) to avoid changing the access time  of  the
              files  that  are being transferred.  If your OS does not support
              the O_NOATIME flag then rsync will silently ignore this  option.
              Note  also  that  some filesystems are mounted to avoid updating
              the atime on read access even without the O_NOATIME  flag  being
              set.

因此,我对这些内容的解读(测试已证实)是,以下内容将阻止 rsync 更新 atime on src,并将复制 atime of srcto dest

rsync -UU <src> <dest>

相关内容