/flags/cmd/robocopy-mir-mirror
robocopy /MIR: mirror a folder (and why it deletes files in the destination)
- tool
- Command Prompt (cmd)
- command
- robocopy
- flag
- /MIR
- default
- off (robocopy copies only the files in the source folder itself, never deletes)
robocopy SOURCE DEST /MIR makes DEST an exact copy of SOURCE: it copies all subdirectories (including empty ones), updates changed files and deletes files and folders in the destination that no longer exist in the source. Microsoft's reference: "Mirrors a directory tree (equivalent to /e plus /purge)." That last part is why /MIR is the right switch for a backup copy and the wrong one if the destination holds anything else: pointing it at the wrong folder deletes that folder's contents.
robocopy D:\Projects \\nas\backup\Projects /MIR /R:2 /W:5 /LOG:C:\Logs\mirror.log
robocopy D:\Projects \\nas\backup\Projects /MIR /L &rem preview onlyWhat /MIR does, step by step
In a scratch folder: a source with a file and a subfolder, a destination with an old file that is not in the source. The first run copies both files and removes the extra one (*EXTRA File); the second run has nothing to do; a run with /L only lists what would happen.
mkdir src\sub
echo one> src\a.txt
echo two> src\sub\b.txt
mkdir dst
echo old> dst\stale.txt
robocopy src dst /MIR /NJH /NJS /NP /NDL
echo exit code: %errorlevel%
dir /b dst
robocopy src dst /MIR /NJH /NJS /NP /NDL
echo exit code: %errorlevel%
echo new> src\c.txt
robocopy src dst /MIR /L /NJH /NJS /NP /NDL
echo exit code: %errorlevel%
dir /b dstOutput
*EXTRA File 5 /tmp/work/dst/stale.txt New File 5 /tmp/work/src/a.txt New File 5 /tmp/work/src/sub/b.txt exit code: 3 a.txt sub exit code: 0 New File 5 /tmp/work/src/c.txt exit code: 1 a.txt sub
The exit codes are bit flags: 1 = files copied, 2 = extra files or folders found in the destination, 3 = both, 0 = nothing to do. Values of 8 and above mean failures; see robocopy exit codes.
Related switches
| Switch | Effect |
|---|---|
| /E | copy subdirectories, including empty ones; never deletes |
| /S | copy subdirectories, excluding empty ones |
| /PURGE | delete destination files and folders that no longer exist in the source |
| /MIR | /E plus /PURGE; also overwrites the destination folder security settings |
| /L | list only: nothing is copied, deleted or time-stamped |
| /XD dir, /XF file | exclude folders or files (excluded items in the destination are not purged) |
| /XO | skip source files that are older than the destination copy |
| /R:n /W:n | retries and wait seconds; defaults are 1,000,000 retries and 30 seconds |
| /MT:n | multi-threaded copy with n threads (default 8) |
| /COPY:DATS, /COPYALL | also copy security (S) or everything |
Pitfalls
- Swapped arguments.
robocopy DEST SOURCE /MIRmirrors the empty or old folder over your data. Run with/Lfirst and read the*EXTRAlines. - Root of a drive. Mirroring into
E:\makes the whole drive match the source; everything else on it is deleted. Use a subfolder. - Default retries. With the default
/R:1000000 /W:30a locked file can stall the job for days; always set/Rand/W. - Access denied. Files that need backup privileges fail with Access is denied; run elevated and add
/Bor/ZB. - xcopy is not a mirror.
xcopy /Enever deletes; the differences are on xcopy vs robocopy.
FAQ
What is the difference between /MIR and /E /PURGE?
The copy and delete behaviour is the same: /MIR is documented as equivalent to /E plus /PURGE. The difference is the destination folder's security settings, which /MIR overwrites and /E /PURGE leaves in place.
Why does robocopy /MIR return exit code 3 in my script?
Robocopy exit codes are bit flags, not errors: 1 means files were copied, 2 means extra files were found (and, with /MIR, deleted). 3 is both. Only values of 8 or higher mean that some copies failed.
Does /MIR copy NTFS permissions?
Not by default. The default /COPY:DAT copies data, attributes and timestamps. Add /COPY:DATS or /SEC to copy security (ACLs), /COPYALL for everything.