Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

Robocopy /MIR copied the deletion: why a mirror is not a backup, and what to do about it

Technical article
Backup & Recovery

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

Robocopy /MIR makes the destination the same as the source. Microsoft's documentation defines it as /E plus /PURGE, and /PURGE "deletes destination files and directories that no longer exist in the source". So when a file is deleted at the source, the next mirror run deletes it at the destination as well. A mirror holds the state of the source as at the last run, and nothing older.

If a scheduled /MIR job has just removed files you needed, stop the schedule first so the next run does not purge more, then look for an older copy somewhere else. Robocopy has no undo.

What the symptom means

The symptom is a copy that looks healthy (the job exits cleanly, the destination holds the right number of files) but is missing something from before. Nothing is broken. The command did what it is documented to do.

Microsoft's robocopy page documents these options, all relevant here:

  • /mir: "Mirrors a directory tree (equivalent to /e plus /purge)."
  • /purge: "Deletes destination files and directories that no longer exist in the source."
  • /l: "Specifies that files are to be listed only (and not copied, deleted, or time stamped)."
  • /xx: "Excludes extra files and directories present in the destination but not the source. Excluding extra files doesn't delete files from the destination."
  • /e: copies subdirectories, including empty ones. It does not remove anything from the destination.

The page documents no option that keeps older versions of a file, and no recycle area for purged files. It is a copy tool. Whether a destination file is replaced when the source file changes follows from its defaults: the documentation's own example for skipping files that already exist at the destination, "regardless of whether they're newer, older, or modified", needs /XC /XN /XO. By implication, without those options, a changed source file is copied over the destination file. That is reasoning from the example, not a vendor statement.

Three things therefore flow from source to destination on every run:

  1. Deletions. A file removed or renamed at the source is purged from the destination.
  2. Replacements. A file overwritten at the source, including by ransomware that encrypts files in place, is copied over the good one.
  3. Absence. If the source path is empty or unreachable, /mir treats everything at the destination as "no longer exist[ing] in the source". This follows from the documented definition; the page does not describe the empty-source case in particular.

That is why a mirror protects against a disk failing, but not against a mistake or an attack that happens in the source.

Likely causes

Cause What you see How to confirm
A user or script deleted or moved files at the source The files are gone from both copies after the next scheduled run The job's schedule time falls after the deletion; the log from the run (if /LOG was used) shows the run time
Files at the source were encrypted or overwritten in place Both copies hold unreadable or changed files; newer than the originals Source file contents and modified times; start with the clean-room restore order for recovering after ransomware
Source path empty or not reachable at run time (unmounted drive, share down, wrong path) The destination was emptied or shrank sharply Source folder lists nothing now; the job's start time and log
Source and destination swapped in the command The "backup" folder now matches an older or smaller tree Order in the task: Microsoft's syntax is robocopy <source> <destination>; read the command as written
A folder was renamed at the source Old folder gone from the destination; new folder present with a full copy Folder names at the source against destination

Prove the cause before changing anything

These steps read only. Stop first if you can.

  1. Stop further runs. In the scheduler, find the task that runs the robocopy command. With PowerShell, Get-ScheduledTask -TaskName "<name>" shows the task and its state; Microsoft's documentation shows this form for a task in the root folder, and -TaskPath for other folders.

    Disruptive step: Disable-ScheduledTask -TaskName '<name>' stops the job from running. Nothing is copied or protected while it is off. Undo it with Enable-ScheduledTask -TaskName '<name>' once the command is fixed.

  2. Read the command. Find the exact robocopy line in the task or script. Check for /MIR and /PURGE, and check that the first path is the source.

  3. List what the next run would do, without doing it. Add /L to the same command, with a log file:

robocopy <source> <destination> /MIR /L /LOG:C:\Logs\mir-check.log

/L lists only, and does not copy, delete or time-stamp. /X adds a report of all extra files, not only the ones selected. Open the log and read what is marked for removal from the destination. If the list is large and you did not expect it, the cause is in the source.

  1. Read exit codes correctly. Microsoft states: "Any value equal to or greater than 8 indicates that there was at least one failure." Codes 0 to 7 describe what was copied, mismatched or extra, not a failure, so a task that records "success" for a purge is normal.

  2. Check the source. Open the source path as the account that runs the job. Confirm it lists what you expect and that every folder the job covers is present.

Example (illustrative): on fs01, D:\Data holds 500 GB, and about 2 GB of it changes each day. A mirror to \\backup01\copy runs at 02:00 each night with /MIR. On Tuesday afternoon someone deletes a 40 GB project folder. At 02:00 on Wednesday the mirror removes the same 40 GB from \\backup01\copy, leaving 460 GB in both places. On Thursday the missing folder is noticed. The mirror reports a code below 8 to the scheduler, which Microsoft's table does not treat as a failure. No older copy exists unless something else, such as shadow copies or a separate backup, held one. The 40 GB is 8% of 500 GB (40 / 500 = 0.08).

Fix

If the copy is already purged: look for an older copy

Robocopy cannot bring files back. Look elsewhere, in this order:

  • A separate backup with restore points, if one exists.
  • Shadow copies of the source volume or the destination volume. Microsoft's Volume Shadow Copy Service documentation describes a shadow copy as a point-in-time view of a volume, but they are not a backup, so check their limits (Previous Versions and shadow copies are covered in a separate topic) before relying on them.
  • Another machine or share that received the same files.

If the cause was ransomware, do not restore from any copy until you know which is clean; follow the clean-room restore order for recovering after ransomware.

Change the command so it cannot purge

If the job is a backup copy, drop /MIR and /PURGE. /E copies subdirectories and never removes anything from the destination. Then give each run its own destination folder, named for the date, so a later run cannot overwrite an earlier one. The date in the folder name comes from your own script; the robocopy page documents no option that creates versions, so this is a method of your own, not a robocopy feature.

robocopy <source> "<destination>\<date-folder>" /E /R:2 /W:5 /LOG:C:\Logs\copy.log

Risk: changing the job from /MIR to /E changes what the destination does: it no longer drops files deleted at the source, so it only grows, and the dated folders multiply space use (see the table below). Keep a copy of the old command text so you can go back; going back to /MIR means the next run purges again.

/R:2 /W:5 limits retries to 2 and waits 5 seconds between them, as in Microsoft's examples. The documented defaults are 1,000,000 retries and a 30-second wait, which can leave a job retrying for a very long time on one locked file.

To copy only recently changed files into a dated folder, Microsoft documents /MAXAGE:<n>, which "Specifies the maximum file age to exclude files older than n days or a date based on when the files were last modified". Use a window longer than the schedule gap (for example, /MAXAGE:2 for a nightly job) so a missed night does not lose a day. Take a full copy at intervals.

Method Space for the example's 500 GB source (2 GB changes a day) Restores to a past day Deleted files
/MIR to one folder 500 GB No: only the last run Removed from the copy on the next run
/E full copy into a new dated folder each night, kept 7 days 7 × 500 GB = 3,500 GB Yes: the whole tree as at that night Kept in earlier folders
Weekly full copy plus six /E /MAXAGE:2 copies (about 4 GB each), kept 4 weeks 4 × (500 + 6 × 4) GB = 4 × 524 GB = 2,096 GB Partly: a changed-files folder holds only files changed in that window Not recorded: a changed-files copy cannot show that a file was deleted

The figures use decimal gigabytes and the 2 GB a day assumed in the example above; real change rates differ.

If you need many versions without multiplying storage, a backup product that stores only data that is new to its store can keep restore points at a lower storage cost than dated full copies. A robocopy mirror is the right tool where "make this folder the same as that one" is the job, such as the final sync before a server cut-over.

If you keep a mirror for another reason

Keep it, but keep versions elsewhere: the mirror covers a disk failure, and a separate backup with restore points covers mistakes and attacks. Run /MIR only with /L first when the paths or the command change.

Risk: a live /MIR run is the step that deletes. Microsoft notes that on the root of a volume robocopy skips a System Volume Information directory in the top level of the source and destination; everything else that is not in the source goes. Microsoft also notes that using /MIR with /E and a destination directory "overwrites the destination directory security settings". There is no way back from a purge except a copy that you already hold.

Confirm the fix

  1. The new command contains neither /MIR nor /PURGE. Read it again from the scheduled task, not from memory.
  2. Run it once to a scratch destination, with /L first. The log shows copies and no removals.
  3. After the first real run, check that the destination folder is new and that the previous night's folder is unchanged.
  4. Delete a test file at the source on a test share, run the job again, and confirm earlier folders still hold it. Do this only on a test path.
  5. Restore one folder from a dated copy to a scratch location and compare it with the source.

What to watch afterwards

  • Free space on the destination. Dated copies grow by the amount you keep.
  • Exit codes of 8 or more, and a sudden fall in the file count of the log summary.
  • Any task that later reintroduces /MIR or /PURGE.
  • A source that is empty or missing at run time.

Risks and limits

  • These commands have not been run or lab-tested. They are taken from Microsoft Learn.
  • The robocopy page does not describe how it decides that a file differs, how an empty source behaves, or where purged files go. Those points are given only as consequences of the documented options, and are labelled as such where they appear.
  • A changed-files copy keeps no record of deletions, so restoring a past day from it can bring back files deleted earlier.
  • Dated copies on the same machine or the same account as the source are exposed to the same mistakes and attacks as the source. Immutable or offline copies are covered under logical and physical air-gapped repositories.
  • Robocopy is a Windows tool; other tools, such as rsync on Linux, have their own options and defaults.

Sources