我可以使用 rsync 使用两个冲突文件中的较晚一个来镜像两个目录吗?

我可以使用 rsync 使用两个冲突文件中的较晚一个来镜像两个目录吗?

我一直在使用 rsync 通过以下命令非常成功地备份我的“主”硬盘

rsync -aivr --delete --progress /src/my-files/ /dest/my-files

但我随身携带外置驱动器,并且可能会修改上面的文件。上面的命令将清除这些更改。我也不能只交换 src 和 dest,因为我可能在想要保留的两个驱动器上进行了更改。

我想要的行为是这样的......

# rsync walks the src drive
# if a file is present in src that is not present in dest, then copy the file to dest
# if a file is present both in src and in dest, then compare modification times
#     and overwrite using the later one.
# if a file is present on dest but is not on src, then copy the file from dest to src.

我可以使用 rsync 来获得上述行为吗?

答案1

rsync是一种单向操作。你可以尝试用它实现双向复制,但它很脆弱并且容易出错。但是,您可以使用开关阻止rsync删除目标上的较新文件--update

   -u, --update
          This forces rsync to skip any files which exist on the destination 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  destina-
          tion has a file, the transfer would occur regardless of the timestamps. 

相关内容