Robocopy 如何处理文件系统链接(符号链接、硬链接和联结)?

马可·埃克斯坦

删除 Robocopy 备份是否安全?我读到有些人不小心删除了备份之外的文件,因为它包含链接/连接。我可以防止这种情况吗?

马可·埃克斯坦

的Robocopy文档控制的Robocopy的有关文件系统的链接行为列表选项:

/sl 不要遵循符号链接,而是创建链接的副本。
/xj 排除连接点,默认情况下通常包含这些连接点。
/xjd 排除目录的连接点。
/xjf 排除文件的连接点。

我创建了一个批处理脚本来了解 Robocopy 在使用和不使用这些选项的情况下如何工作。它使用mlink. 使用mlink,您可以创建:

  • 文件和目录的符号链接。
  • 文件的硬链接。尝试创建指向目录的硬链接会导致错误。
  • 连接点,它们是指向目录的链接。尝试创建到文件的联结会成功,但联结将被破坏。

符号链接可以是相对的或绝对的,这取决于您传递目标参数的方式。硬链接和连接点始终是绝对的,即使您传递了一个相对目标。

@echo off
set home=C:\my-directory
cd %home%
mkdir robocopy-source
cd robocopy-source
mkdir a
mkdir b
copy NUL b\foo.txt
cd a
mklink /j junction-dir-absolute %home%\robocopy-source\b\
mklink /d symlink-dir-absolute %home%\robocopy-source\b\
mklink /d symlink-dir-relative ..\b\
mklink /h hardlink-file-absolute %home%\robocopy-source\b\foo.txt
mklink symlink-file-absolute %home%\robocopy-source\b\foo.txt
mklink symlink-file-relative ..\b\foo.txt
cd %home%
robocopy robocopy-source robocopy-target /mir /dcopy:dat
robocopy robocopy-source robocopy-target-sl /mir /dcopy:dat /sl
robocopy robocopy-source robocopy-target-xj /mir /dcopy:dat /xj
robocopy robocopy-source robocopy-target-xjd /mir /dcopy:dat /xjd
robocopy robocopy-source robocopy-target-xjf /mir /dcopy:dat /xjf

使用Link Shell Extension,我们现在可以看到发生了什么:

小路 信息
robocopy-source\a\junction-dir-absolute %home%\robocopy-source\b\ 的链接
robocopy-source\a\symlink-dir-absolute %home%\robocopy-source\b\ 的链接
robocopy-source\a\symlink-dir-relative 链接到 ..\b\
robocopy-source\a\hardlink-file-absolute %home%\robocopy-source\b\foo.txt 的链接(和它本身)
robocopy-source\a\symlink-file-absolute 链接到 C:\Users\Marco\Desktop\Linktest\robocopy-source\b\foo.txt
robocopy-source\a\symlink-file-relative ..\b\foo.txt 的链接
robocopy-target\a\junction-dir-absolute 复制内容为 %home%\robocopy-source\b\ 的目录
robocopy-target\a\symlink-dir-absolute 复制内容为 %home%\robocopy-source\b\ 的目录
robocopy-target\a\symlink-dir-relative 复制内容为 %home%\robocopy-source\b\ 的目录
robocopy-target\a\hardlink-file-absolute 空的文件
robocopy-target\a\symlink-file-absolute 空的文件
robocopy-target\a\symlink-file-relative 空的文件
robocopy-target-sl\a\junction-dir-absolute 复制内容为 %home%\robocopy-source\b\ 的目录
robocopy-target-sl\a\symlink-dir-absolute %home%\robocopy-source\b\ 的链接
robocopy-target-sl\a\symlink-dir-relative 链接到 ..\b\
robocopy-target-sl\a\hardlink-file-absolute 空的文件
robocopy-target-sl\a\symlink-file-absolute %home%\robocopy-source\b\foo.txt 的链接
robocopy-target-sl\a\symlink-file-relative ..\b\foo.txt 的链接
robocopy-target-xj\a\junction-dir-absolute 不存在
robocopy-target-xj\a\symlink-dir-absolute 不存在
robocopy-target-xj\a\symlink-dir-relative 不存在
robocopy-target-xj\a\hardlink-file-absolute 空的文件
robocopy-目标-xj\a\symlink-file-absolute 不存在
robocopy-target-xj\a\symlink-file-relative 不存在
robocopy-target-xjd\a\junction-dir-absolute 不存在
robocopy-target-xjd\a\symlink-dir-absolute 不存在
robocopy-target-xjd\a\symlink-dir-relative 不存在
robocopy-target-xjd\a\hardlink-file-absolute 空的文件
robocopy-target-xjd\a\symlink-file-absolute 空的文件
robocopy-target-xjd\a\symlink-file-relative 空的文件
robocopy-target-xjf\a\junction-dir-absolute 复制内容为 %home%\robocopy-source\b\ 的目录
robocopy-target-xjf\a\symlink-dir-absolute 复制内容为 %home%\robocopy-source\b\ 的目录
robocopy-target-xjf\a\symlink-dir-relative 复制内容为 %home%\robocopy-source\b\ 的目录
robocopy-target-xjf\a\hardlink-file-absolute 空的文件
robocopy-target-xjf\a\symlink-file-absolute Does not exist
robocopy-target-xjf\a\symlink-file-relative Does not exist

I conclude that unless you have used the /sl option, deleting a backup created with Robocopy should be safe. If you did use the /sl option however, absolute symbolic links could get you into trouble, e.g. when using the del command.

Further observations:

  • A hard link always yields an empty file.
  • Option /xjd skips links to directories. Other than the docs imply, it does not only skip junctions, but also symbolic links!
  • Option /xjf skips links to files. Other than the docs imply, it does not skip junctions for files (which do not exist anyway), but symbolic links!
  • Option /xj combines /xjd and /xjf.

I have reported the issue with the docs.

Bonus info regarding shortcuts (.lnk files)

Shortcuts are treated by Robocopy like regular files, so they will generally point outside of your backup directory. However, they are usually treated like regular files by other command line programs, too. I have checked this for del /s /q and rmdir /s /q in the command prompt and rm -rf in the MINGW64 shell, all of which will not follow the shortcuts and thus will not lead to unintended deletions.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

RoboCopy日志文件分析

来自分类Dev

Robocopy偶尔忽略文件

来自分类Dev

覆盖文件选项 robocopy

来自分类Dev

符号链接在文件系统与硬链接中如何表示?

来自分类Dev

使RoboCopy仅复制完成的文件

来自分类Dev

使用Robocopy获取文件列表

来自分类Dev

robocopy是否在文件的本地副本?

来自分类Dev

Robocopy长文件名

来自分类Dev

xcopy和robocopy之间的区别

来自分类Dev

如何从Powershell脚本启动Robocopy

来自分类Dev

Robocopy,如何只记录错误?

来自分类Dev

Robocopy如何确定文件是否“已更改”?

来自分类Dev

如何让Robocopy首先删除多余的文件

来自分类Dev

以ADMIN身份运行批处理并使用robocopy

来自分类Dev

Robocopy 批处理脚本不断重启

来自分类Dev

Time Machine如何处理硬链接

来自分类Dev

Robocopy镜像擦除排除目录中的文件

来自分类Dev

Robocopy对目标文件夹的误解

来自分类Dev

Robocopy替换相同日期的文件移动

来自分类Dev

Robocopy:跳过/失败文件的日志名称

来自分类Dev

Robocopy在什么时候覆盖文件?

来自分类Dev

Robocopy / S创建空文件夹

来自分类Dev

Robocopy:复制没有目录结构的文件

来自分类Dev

robocopy或xcopy:复制目录和文件

来自分类Dev

仅使用Robocopy打印文件名

来自分类Dev

robocopy 忽略修改过的文件

来自分类Dev

Robocopy 移动旧文件而不是覆盖

来自分类Dev

具有Robocopy和参数传递的Powershell

来自分类Dev

如何告诉robocopy忽略时间戳?