@ECHO OFF REM Backup files using Robocopy command REM Author: Katevaoglou Stylianos, Center of Informatics and New Technologies of Samos (KEPLHNET Samou) REM License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International REM Attribution link: http://dide.sam.sch.gr/keplinet REM Enter the correct values at the 3 following lines that start with SET command REM Exit Codes: REM 0 = Completed Successfully REM 1 = Backup operation aborted by user before doing anything REM 2 = Source folder was not found REM 3 = Destination folder was not found REM 4 = Robocopy encountered some minor faults. See %logFile% for further info REM 5 = Robocopy encountered serious errors. Partial backup was made. See %logFile% for further info REM 6 = Robocopy encountered a fatal error. No backup was made. See %logFile% for further info REM Initializing local environment SETLOCAL EnableDelayedExpansion SETLOCAL EnableExtensions REM Variables (edit as needed) SET sourceFolder=C:\My Data SET destFolder=H:\My Backup SET logFile=RobocopyLogFile.txt REM Print info ECHO ============================================================================== ECHO Backup files using Robocopy command ECHO ============================================================================== ECHO Your data will be copied: ECHO -From %sourceFolder% ECHO -To %destFolder% ECHO A complete mirror will be made ECHO Files in %destFolder% that do not exist in %sourceFolder% will be deleted ECHO Files in %destFolder% that also exist in %sourceFolder% will be replaced ECHO ============================================================================== REM Assume everything will go OK SET backupResultCode=0 SET backupResultText=Backup completed successfully with no errors at all REM If you do NOT want a confirmation message to appear comment the six following lines (just add REM command in front of each line) SET /P continue=If you are sure enter Y or y... IF "%continue:~0,1%" NEQ "y" IF "%continue:~0,1%" NEQ "Y" ( SET backupResultCode=1 SET backupResultText=ABBORTED: Backup operation aborted by user before doing anything GOTO exit ) IF NOT EXIST "%sourceFolder%" ( SET backupResultCode=2 SET backupResultText=ERROR: Source folder was not found GOTO exit ) IF NOT EXIST "%destFolder%" ( SET backupResultCode=3 SET backupResultText=ERROR: Destination folder was not found GOTO exit ) ECHO ============================================================================== ECHO Processing... ECHO ============================================================================== REM Starting the real backup process REM Explaining parameters of Robocopy command REM /COPY:DATS = COPY D=Data, A=Attributes, T=Timestamps, S=Security REM /MIR = MIRror a directory tree REM /R:0 = number of Retries (locked files will fail either way) REM /W:0 = Wait time between retries REM /LOG = Output to log file REM /NFL = No file logging REM /NDL = No dir logging ROBOCOPY "%sourceFolder%" "%destFolder%" /COPY:DATS /MIR /R:0 /W:0 /LOG:%logFile% /NFL /NDL REM Trying to work out what has happened IF %ERRORLEVEL% EQU 16 ( SET backupResultCode=6 SET backupResultText=FATAL ERROR: Robocopy encountered a fatal error. No backup was made. See %logFile% for further info GOTO exit ) IF %ERRORLEVEL% GEQ 8 ( SET backupResultCode=5 SET backupResultText=SERIOUS ERROR: Robocopy encountered serious errors. Partial backup was made. See %logFile% for further info GOTO exit ) IF %ERRORLEVEL% GEQ 4 ( SET backupResultCode=4 SET backupResultText=MINOR FAULTS: Robocopy encountered some minor faults. A full backup was made. See %logFile% for further info GOTO exit ) :exit ECHO ============================================================================== ECHO %backupResultText% ECHO ============================================================================== ECHO End of execution. See %logFile% for further info. Goodbye REM Finish using local environment ENDLOCAL ECHO on @EXIT /B %backupResultCode%