How to build an Automated, Logged Backup with Robocopy and Task Scheduler

Configurare noua (How To)

Situatie

A scheduled job that copies a folder to another drive or a network share, writes a dated log, deletes logs older than 30 days, and writes an Event Log entry if the copy fails.

Prerequisites

  • Windows 10/11 (Robocopy is built in)
  • Admin rights
  • A destination with enough free space (a second drive, USB disk or NAS share).

Solutie

Step 1: Create the script

Create C:\Scripts\backup.bat:

bat
@echo off
set "SRC=C:\Documents"
set "DST=D:\Backup\Documents"
set "LOGDIR=D:\Backup\logs"

if not exist "%SRC%" exit /b 16
if not exist "%LOGDIR%" mkdir "%LOGDIR%"

for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set "STAMP=%%i"

robocopy "%SRC%" "%DST%" /E /XO /R:2 /W:5 /NP /LOG:"%LOGDIR%\backup_%STAMP%.log"
set "RC=%ERRORLEVEL%"

if %RC% GEQ 8 eventcreate /T ERROR /ID 100 /L APPLICATION /SO BackupScript /D "Robocopy failed with exit code %RC%"

forfiles /p "%LOGDIR%" /m backup_*.log /d -30 /c "cmd /c del @path" 2>nul

exit /b %RC%

The date comes from PowerShell because %date% formatting depends on the regional settings.

Step 2: Understand the Robocopy flags

  • /E copies all subfolders, including empty ones.
  • /XO skips files that are older than the copy already in the destination.
  • /R:2 /W:5 retries twice, waiting 5 seconds, instead of the default of one million retries.
  • /NP removes the progress percentage to keep logs readable.
  • /LOG writes output to a file.

Step 3: Dry run

Add /L (list only) to the robocopy line temporarily and run the script. Check the log to confirm that the right files are selected. Remove /L afterwards.

Step 4: Schedule it with PowerShell (Admin)

powershell
$action    = New-ScheduledTaskAction -Execute "C:\Scripts\backup.bat"
$trigger   = New-ScheduledTaskTrigger -Daily -At 9pm
$settings  = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
Register-ScheduledTask -TaskName "Backup Documents" -Action $action -Trigger $trigger -Settings $settings -Principal $principal

-StartWhenAvailable runs the job as soon as possible if the PC was off at 9 PM. Running as SYSTEM means it works while nobody is logged in, but SYSTEM can’t see mapped drive letters. Use UNC paths (\\NAS\Backup\Documents) for network destinations.

Step 5: Test

  1. Run: Start-ScheduledTask -TaskName "Backup Documents"
  2. Check D:\Backup\logs\backup_<date>.log.
  3. Check Task Scheduler → Task Scheduler Library → Last Run Result. 0x0 or 0x1 is success.

Robocopy exit codes

  • 0: nothing to copy
  • 1: files copied
  • 2: extra files in the destination
  • 4: mismatches
  • Combinations of these (3, 5, 6, 7) are still success
  • 8 or higher: at least one failure, and the script logs it to the Event Log

Verification

  • Open a few files in the destination and confirm they open.
  • Simulate a failure by making the destination unavailable, then check Event Viewer → Windows Logs → Application for source BackupScript.

Caveats

  • /XO never propagates deletions, so files you delete from the source remain in the backup. /MIR mirrors deletions too, which is risky if you delete something by accident.
  • This is a copy, not versioned backup. For history, add a dated destination folder or use File History or a dedicated backup tool.
  • A backup on the same physical disk isn’t a backup. Follow the 3-2-1 rule: three copies, two media, one offsite.

Tip solutie

Permanent

Voteaza

(1 din 2 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?