Windows Servers
Robocopy for File Backups on Windows Server: A Practical Guide
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيRobocopy (Robust File Copy) ships with every modern version of Windows and Windows Server, and it is one of the most dependable tools for copying large folder trees. It retries on failure, preserves timestamps and permissions, skips files that have not changed, and writes detailed logs. That makes it a natural fit for scheduled file backups on a Windows VPS, whether you are protecting a web root, a shared documents folder or application data. This guide walks through the switches that matter, a ready-to-use backup script, and the mistakes that cause silent data loss.
How Robocopy works
The basic syntax is:
robocopy <source> <destination> [files] [options]By default Robocopy copies only files that are new or different (by size and timestamp) from source to destination. Unchanged files are skipped, so the second and later runs are much faster than the first. The [files] filter defaults to *.*, which means everything.
The switches you will actually use
| Switch | What it does |
|---|---|
/E | Copy subfolders, including empty ones |
/MIR | Mirror: same as /E plus delete files in the destination that no longer exist in the source |
/COPY:DAT | Copy data, attributes and timestamps (the default) |
/COPYALL | Copy everything, including NTFS permissions, owner and auditing info |
/DCOPY:DAT | Preserve folder timestamps and attributes |
/Z | Restartable mode, useful for large files over unstable links |
/R:2 /W:5 | Retry 2 times, waiting 5 seconds (the defaults are 1 million retries and 30 seconds) |
/MT:16 | Multithreaded copy with 16 threads (default 8, max 128) |
/XD and /XF | Exclude folders or files by name or wildcard |
/LOG+:file | Append output to a log file |
/NP | Hide percentage progress, which keeps logs readable |
/L | List only: show what would happen without copying anything |
Always set /R and /W. With the defaults, a single locked file can make a job appear to hang for days.
Mirror or copy: choose carefully
/MIR keeps the destination an exact replica of the source. That is great for a synchronized copy, but it is not a real backup on its own: if a file is deleted or encrypted by ransomware in the source, the next mirror run deletes or overwrites it in the destination too. Before running a mirror against a new path, test it with /L first.
A safer pattern is to combine Robocopy with versioning:
- Mirror to a "current" folder for fast restores.
- Keep dated copies (for example one folder per day) and delete old ones after a retention period.
- Store at least one copy off the server, such as another VPS or object storage, following the 3-2-1 rule explained in our website backups guide.
A ready-to-use backup script
Save this as C:\Scripts\backup.cmd:
@echo off
set SRC=D:\Data
set DST=\\backup-host\backups\data
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set TODAY=%%i
set LOG=C:\Scripts\logs\robocopy-%TODAY%.log
robocopy "%SRC%" "%DST%\%TODAY%" /E /COPY:DAT /DCOPY:DAT /R:2 /W:5 /MT:16 /NP /XD "$RECYCLE.BIN" "System Volume Information" /XF *.tmp /LOG+:"%LOG%"
if %ERRORLEVEL% GEQ 8 (
echo Backup FAILED with code %ERRORLEVEL% >> "%LOG%"
exit /b 1
)
exit /b 0Create the log folder first with mkdir C:\Scripts\logs. If the destination is a network share, the account that runs the task needs write permission on it.
Understanding exit codes
Robocopy exit codes are a bitmask, and a non-zero code does not mean failure:
| Code | Meaning |
|---|---|
| 0 | Nothing copied, source and destination already in sync |
| 1 | Files copied successfully |
| 2 | Extra files found in destination |
| 4 | Mismatched files or folders detected |
| 8 | Some files could not be copied (failures) |
| 16 | Serious error, nothing was copied |
Values can combine, so 3 means 1 + 2. The practical rule is simple: anything 8 or higher needs attention. Monitoring tools that treat every non-zero code as an error will raise false alarms.
Scheduling with Task Scheduler
You can create the scheduled task from an elevated PowerShell prompt:
$action = New-ScheduledTaskAction -Execute "C:\Scripts\backup.cmd"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
Register-ScheduledTask -TaskName "Nightly Robocopy Backup" -Action $action -Trigger $trigger -User "SYSTEM" -RunLevel HighestRunning as SYSTEM works for local destinations. For a network share, use a dedicated service account with only the permissions it needs, and check "Run whether user is logged on or not".
Common mistakes checklist
- Swapping source and destination with
/MIR, which wipes the real data. Always run/Lfirst. - Leaving default retries, so a locked file stalls the job.
- Backing up open database files. Use the database's own dump tool, then copy the dump.
- Never testing a restore. Copy a few files back regularly and open them.
- Keeping the only backup on the same disk or the same server.
For full image-level protection, pair Robocopy with server snapshots. On NileVPS you can take snapshots from the control panel before risky changes, and you can compare plans on the pricing page.
FAQ
Does Robocopy copy open or locked files?
No. Robocopy cannot read files locked exclusively by another process, and it will retry and then skip them. For locked files you need a Volume Shadow Copy based tool such as Windows Server Backup, or you should stop the application or export its data first.
Is /MIR safe for backups?
It is safe as long as you understand that deletions propagate. Use it for a synchronized working copy, but keep dated or versioned copies elsewhere so an accidental deletion in the source cannot erase your only backup.
How many threads should I use with /MT?
The default of 8 is fine for most cases. Values between 16 and 32 often help with many small files on fast disks or networks. Very high values can saturate a slow link or disk, so measure your own job and adjust.