Robocopy creates infinite subfolder duplication when a destination folder contains a symbolic link or junction pointing back to the source directory. This occurs because Robocopy follows the link, copies the contents into the linked folder, and repeats the process indefinitely. To prevent this, you must identify and remove these recursive links before running your backup command.
The Root Cause: Recursive Symbolic Links
The primary reason for infinite duplication is not a bug in Robocopy itself, but rather how it handles reparse points. By default, Robocopy does not follow symbolic links (symlinks) or directory junctions unless explicitly instructed to do so with the/SL flag. However, if you have previously created a junction or symlink inside your destination that points to the source, a subtle cycle can form.
Consider a scenario where you are backing up C:\Data to D:\Backup. If there is a folder named D:\Backup\DataLink that is actually a junction pointing to C:\Data, and you run a command that includes the root of the destination in its scan, complications arise. More commonly, users accidentally create a junction within the destination structure that points to a parent or sibling directory, creating a loop. When Robocopy traverses the destination, it sees the link, enters it, finds the source files again, and attempts to copy them, potentially recreating the link or duplicating content depending on the specific flags used.
Another common trigger is using the /E or /S flags in combination with a destination that already contains a mirror of the source via a different mechanism. If you use /MIR (mirror), Robocopy deletes files in the destination that do not exist in the source. If a symlink exists in the destination pointing to the source, Robocopy might interpret the structure incorrectly during the initial scan phase, leading to unexpected behavior.
It is crucial to distinguish between a standard shortcut file (.lnk) and a system-level junction or symbolic link. A standard .lnk file is just data; Robocopy copies it as a file. It does not execute the link. Therefore, a stray .lnk file cannot cause infinite duplication. The issue only arises with NTFS reparse points, specifically junctions (mklink /J) and symbolic links (mklink /D).
Identifying Problematic Links
Before attempting any fixes, you need to verify if your destination drive contains any hidden reparse points. You can use the built-indir command with the /A:L switch to list all symbolic links and junctions in a directory tree.
Run the following command in Command Prompt:
dir "D:\Backup" /A:L /S
This will display any symlinks or junctions found under D:\Backup. Look for entries marked with <SYMLINK> or <JUNCTION>. Pay close attention to the target path. If any target points back to C:\Data or any parent directory of the backup location, you have identified the culprit.
If you suspect the issue is broader, you can check the entire drive for reparse points using PowerShell:
Get-ChildItem -Path "D:\" -Recurse -Force | Where-Object { $_.Attributes.ToString().Contains("ReparsePoint") }
This script recursively scans the drive and filters for items that are reparse points. Review the output carefully. Any item listed here that is not an intended part of your backup strategy should be removed.
Removing Junctions and Symlinks
Once you have identified the problematic link, you must remove it. Do not try to delete it like a normal file. Use thermdir or rd command with the /S flag for directories or simply rmdir for symlinks.
For a junction:
rmdir "D:\Backup\DataLink" /S
For a symbolic link:
rmdir "D:\Backup\DataLink"
Note that rmdir removes the link itself, not the target directory. This is safe and necessary to break the cycle. After removing the link, verify that the directory structure no longer contains recursive references.
Configuring Robocopy to Prevent Recursion
Even after cleaning up your destination, you should configure Robocopy to be robust against accidental recursion. There are several flags and strategies to ensure safety. First, avoid using/MIR unless you are certain the destination is clean. /MIR is aggressive and deletes files. Instead, use /COPYALL combined with /DCOPY:T to preserve timestamps without deleting extra files. This approach is safer for incremental backups.
Second, explicitly exclude reparse points from being copied. While Robocopy skips symlinks by default, adding the /XF (exclude file) or /XD (exclude directory) flags can provide an extra layer of protection. For example, you can exclude known problem directories:
robocopy C:\Data D:\Backup /E /COPYALL /XF *.tmp /XD "C:\Temp"
Third, consider using the /SL flag cautiously. /SL tells Robocopy to include symlinks. Only use this if you intentionally want to copy the link metadata itself, not follow it. In most backup scenarios, you do not want /SL.
Finally, test your command with the /L (list) flag. This performs a dry run, showing exactly what Robocopy would copy without making any changes.
robocopy C:\Data D:\Backup /E /COPYALL /L
Review the output. If you see any paths that look suspiciously recursive or point to unexpected locations, abort the operation and investigate further.
Best Practices for Safe Backups
To maintain a healthy backup environment, adopt these habits:- Regular Audits: Periodically run the
dir /A:Lcommand on your backup drives to ensure no new junctions have been created by other software. - Isolated Backup Targets: Keep your backup drive separate from your system drive. Avoid placing backup folders on the same volume as the source data whenever possible.
- Use Version Control: Consider using tools designed for versioned backups, such as Windows File History or third-party solutions, which handle symlinks more gracefully than raw Robocopy scripts.
Frequently Asked Questions
Why does Robocopy sometimes copy files even when they haven't changed?
Robocopy may copy unchanged files if the timestamps or attributes differ slightly due to system clock adjustments or permission changes. Use the/XC (eXclude Changed) and /XN (eXclude Newer) flags to strictly skip files that match the source exactly. Always verify your time synchronization settings between source and destination systems.
Can I use Robocopy to move files instead of copying them?
Yes, you can use the/MOVE or /MOV flags to move files and directories. /MOVE moves both files and folders, while /MOV only moves files. Be cautious with /MOVE as it deletes the source files immediately upon successful copy. Ensure you have verified the copy integrity before moving large datasets.
What happens if I accidentally delete the source directory during a backup?
If you use/MIR and the source directory is deleted or becomes inaccessible, Robocopy will delete the corresponding destination directory because mirroring makes the destination identical to the source. To prevent data loss, always use /L for testing first and avoid /MIR unless you are performing a complete reset of the backup destination.
How do I fix a broken symbolic link in my backup destination?
A broken symbolic link appears as a shortcut with a red arrow or shows an error when accessed. Usermdir to remove the broken link entry. If the link was pointing to a valid target that was moved, recreate the link with the correct path using mklink /D. Verify the target exists before recreating the link to prevent future errors.
Is there a way to exclude specific file types from causing issues?
You can exclude specific file types using the/XF flag followed by wildcards, such as /XF *.log or /XF *.tmp. This prevents those files from being copied and reduces the chance of conflicts with temporary or lock files. Combine this with /XD to exclude entire directories that contain volatile data.