How to backup files with ROBOCOPY

Windows Backup and File History work well enough for workstations, but they are not tools most sysadmins reach for when they need to back up a file server, a shared folder, or a specific directory structure on a schedule they control. Robocopy gives you that control: exact source and destination, retry behavior, filtering, logging, and exit codes you can act on in a script.

This guide covers the practical backup patterns — incremental copies, full mirrors, exclusions, and scheduled automation — with the flag combinations that actually work in production environments.


Quick answer

Incremental backup — copy only new and changed files, log the result:

robocopy D:\Data E:\Backup\Data /E /XO /R:3 /W:5 /LOG+:C:\bat\backup-log.txt /NP

Incremental backup — copy only changed files

An incremental backup copies only files that are new or have changed since the last run. Robocopy does this by default — it compares file size and timestamp between source and destination and skips files that are already up to date. The /XO flag reinforces this by explicitly excluding files where the destination copy is newer than the source.

rem Incremental backup of D:\Data to E:\Backup\Data
rem /E        — include all subdirectories, including empty ones
rem /XO       — exclude older: skip files where destination is newer than source
rem /R:3      — retry 3 times on locked files before skipping
rem /W:5      — wait 5 seconds between retries
rem /LOG+:    — append output to log file (+ keeps history across runs)
rem /NP       — suppress per-file progress percentage (cleaner log output)

robocopy D:\Data E:\Backup\Data /E /XO /R:3 /W:5 /LOG+:C:\bat\backup-log.txt /NP
Note: Robocopy compares files by size and last-write timestamp by default. It does not compute checksums. If a file has been modified but its timestamp was reset (some applications do this), Robocopy may skip it. For checksum-based comparison, add /IS to force copy of files with identical timestamps but different sizes, or consider a dedicated backup solution for those edge cases.

Full mirror backup — exact copy with deletion

A mirror backup keeps the destination as an exact replica of the source. Files deleted from the source are also deleted from the destination. This is the right pattern when you want the backup to reflect the current state of the source — not accumulate deleted files over time.

rem Full mirror backup — destination becomes exact copy of source
rem /MIR      — mirror: copy new/changed files AND delete files no longer in source
rem /COPYALL  — preserve Data, Attributes, Timestamps, ACLs, Owner, Auditing
rem /R:3 /W:5 — retry behavior for locked files
rem /LOG:     — write output to log (overwrites on each run — use for single daily job)
rem /NP       — suppress progress output

robocopy D:\Data E:\Backup\Data /MIR /COPYALL /R:3 /W:5 /LOG:C:\bat\mirror-log.txt /NP
Common mistake: Using /MIR without first verifying source and destination arguments. If the arguments are swapped, Robocopy mirrors the (empty or smaller) destination over the source — deleting your actual data. Always run with /L first to preview what will be deleted before executing a mirror for the first time.
rem Dry run — preview mirror operation without making any changes
robocopy D:\Data E:\Backup\Data /MIR /L
Warning: /COPYALL copies NTFS ACLs and requires administrator privileges. On a standalone backup server or across domain boundaries, copied ACLs may reference SIDs that do not resolve on the target — which can silently block access to backed-up files. Use /COPY:DT (data and timestamps only) when copying to a different domain or workgroup environment.

Backup with exclusions

Most production folders contain files you do not want in a backup: temp files, application caches, log files that rotate on their own, or version control metadata. Backing them up wastes space and slows the job. Use /XD to exclude directories and /XF to exclude files by name or pattern.

rem Incremental backup with exclusions
rem /E              — include all subdirectories
rem /XO             — skip files already up to date in destination
rem /XD             — exclude these directory names anywhere in the tree
rem /XF             — exclude files matching these patterns
rem /R:3 /W:5       — retry behavior
rem /LOG+:          — append to log

robocopy D:\AppRoot E:\Backup\AppRoot /E /XO /XD Temp Cache .git node_modules /XF *.log *.tmp *.bak /R:3 /W:5 /LOG+:C:\bat\backup-log.txt /NP
Note: /XD matches directory names anywhere in the tree — not just at the root. A rule like /XD Temp excludes every folder named Temp at any depth. This is usually what you want, but be aware it also excludes legitimately named folders deeper in the structure.

Automate with Task Scheduler

The right way to run Robocopy backups on a schedule is through a batch file registered as a Task Scheduler job. This gives you full control over timing, elevation, logging, and exit code handling.

Step 1 — create the batch file at C:\bat\backup.bat:

@echo off
rem backup.bat — daily incremental backup of D:\Data to E:\Backup\Data
rem Save to C:\bat\backup.bat
rem Run as: SYSTEM or Administrator account via Task Scheduler

rem Set timestamp for log entry
echo. >> C:\bat\backup-log.txt
echo ========================================= >> C:\bat\backup-log.txt
echo Backup started: %DATE% %TIME% >> C:\bat\backup-log.txt
echo ========================================= >> C:\bat\backup-log.txt

rem Run incremental backup
rem /E        — all subdirectories including empty
rem /XO       — skip files already current in destination
rem /XD       — exclude common noise directories
rem /R:3 /W:5 — limited retries for unattended operation
rem /LOG+:    — append to persistent log file
rem /NP /NDL  — suppress progress and directory listing (cleaner log)

robocopy D:\Data E:\Backup\Data /E /XO /XD Temp Cache .git /R:3 /W:5 /LOG+:C:\bat\backup-log.txt /NP /NDL

rem Capture Robocopy exit code
rem Codes 0-7: success or informational (not errors)
rem Codes 8+:  actual errors occurred

if %ERRORLEVEL% GEQ 8 (
    echo BACKUP FAILED with exit code %ERRORLEVEL% >> C:\bat\backup-log.txt
    exit /b 1
)

echo Backup completed successfully. Exit code: %ERRORLEVEL% >> C:\bat\backup-log.txt
exit /b 0

Step 2 — register the task from an elevated command prompt:

rem Create a scheduled task that runs backup.bat daily at 02:00
rem /SC DAILY    — run once per day
rem /ST 02:00    — start time
rem /RU SYSTEM   — run as SYSTEM account (has local admin rights, no password needed)
rem /RL HIGHEST  — run with highest privileges (required for /COPYALL)
rem /F           — overwrite if task already exists

schtasks /Create /TN "Daily Robocopy Backup" /TR "C:\bat\backup.bat" /SC DAILY /ST 02:00 /RU SYSTEM /RL HIGHEST /F
Result: The task appears in Task Scheduler under the root folder. You can verify it with schtasks /Query /TN "Daily Robocopy Backup" or trigger a test run with schtasks /Run /TN "Daily Robocopy Backup".
Note: If the backup destination is a network share, the SYSTEM account may not have access to it. In that case, create a dedicated service account with write access to the share and use /RU domain\svc-backup /RP password instead of /RU SYSTEM.

Reading the result — exit codes and log output

Robocopy does not use a simple pass/fail exit code. It uses a bitmask where each bit represents a different outcome. This is the most common source of confusion when integrating Robocopy into scripts or monitoring tools.

Exit codeMeaningIs it an error?
0No files were copied — source and destination are identicalNo
1Files were copied successfullyNo
2Extra files exist in destination (no action taken without /MIR)No
3Files copied + extra files in destinationNo
4Mismatched files found (different attributes)No
5–7Combinations of the above informational statesNo
8Some files or directories could not be copiedYes
16Fatal error — no files were copied, check source/destinationYes

The end of every Robocopy run prints a summary. The key lines to check:

rem Example Robocopy summary output (plaintext — appears at end of every run)

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       145         3       142         0         0         0
   Files :      1823        47      1776         0         0         2
   Bytes :   4.12 g    234.5 m    3.90 g         0         0   18.22 k

The FAILED column is the one that matters. A non-zero value here means files were not copied due to access errors, locked files that exceeded the retry limit, or permission issues. Everything in Skipped is expected behavior — those files were already current.


What Robocopy cannot do

  • Open files. Robocopy cannot back up files that are locked by another process — databases, Outlook PST files, VM disk images. It skips them after the retry limit and notes them in the log. For open files, you need VSS (Volume Shadow Copy Service). Use robocopy with a VSS-aware wrapper, or a dedicated backup product that creates a shadow copy before reading files.
  • True incremental with versioning. Robocopy keeps one copy of each file. It does not maintain versions — if you overwrite a file and then run the backup, the previous version is gone. For versioned backups, use a dedicated solution (Veeam, Windows Server Backup with VSS, or cloud backup with versioning).
  • Encryption-aware copying. Robocopy copies EFS-encrypted files as encrypted data. If the destination machine does not have access to the encryption certificate, the backed-up files will be inaccessible. Test restoring encrypted files before relying on this as your only backup.
  • Bandwidth throttling. /IPG (inter-packet gap) can slow Robocopy down by adding delays between packets, but it is a blunt instrument and incompatible with /MT. For proper bandwidth control on a WAN link, use a backup solution with built-in throttling or schedule jobs during off-peak hours.

Official documentation

Related tools

Related guides