Skip to content

Instantly share code, notes, and snippets.

@jstorrs
Last active December 29, 2025 23:16
Show Gist options
  • Select an option

  • Save jstorrs/3c7e43aac503b63eeaf0953a5008fd23 to your computer and use it in GitHub Desktop.

Select an option

Save jstorrs/3c7e43aac503b63eeaf0953a5008fd23 to your computer and use it in GitHub Desktop.
@echo off
setlocal EnableExtensions
REM ====== CONFIG ======
set "SRC=C:\Path\To\SourceFolder"
set "DST=\\server\share\SourceFolderBackup"
set "LOG=%~dp0backup_%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.log"
REM ====================
echo ================================================ >> "%LOG%"
echo Backup started: %DATE% %TIME% >> "%LOG%"
echo Source: %SRC% >> "%LOG%"
echo Dest: %DST% >> "%LOG%"
REM ---- FAILSAFE: destination must already exist ----
if not exist "%DST%\" (
echo ERROR: Destination path does not exist. Aborting. >> "%LOG%"
echo ERROR: Destination path does not exist. Aborting.
exit /b 100
)
REM ---- FAILSAFE: source should exist too (optional but wise) ----
if not exist "%SRC%\" (
echo ERROR: Source path does not exist. Aborting. >> "%LOG%"
echo ERROR: Source path does not exist. Aborting.
exit /b 101
)
REM ---- RUN BACKUP ----
robocopy "%SRC%" "%DST%" /E /XO /FFT /Z /R:2 /W:2 /NP /LOG+:"%LOG%"
REM ---- ROB0COPY EXIT CODE HANDLING ----
set "RC=%ERRORLEVEL%"
if %RC% LEQ 7 (
echo Backup completed successfully. Robocopy exit code: %RC% >> "%LOG%"
echo Backup completed successfully.
exit /b 0
) else (
echo Backup completed with ERRORS. Robocopy exit code: %RC% >> "%LOG%"
echo Backup completed with ERRORS.
exit /b %RC%
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment