Simple manual Backup with robocopy

I wanted to have a “simple” solution for an (external) backup of my USB stick. The simplest way would be copy and paste, but I wanted something more robust. So what gets you robust copying of files in Windows? Robust File Copy – robocopy –, of course.

I use this manually, but for internal or permanently attached drives etc. this could be automated with task planning and removing the user promts.

Yes, I wanted something simple, but I also like batch files. So my typing ended up in a file that is a little more complicated than necessary, but I had quite a bit of fun. The code ended up being just 133 lines including descriptive text and formatting.

The mirroring mode of robocopy is what I especially like about this, since it allows me to reuse multiple drives for off-site backups in a relatively simple manner and switch them around.

Additionally, using robocopy and mirroring means that future backups are fast, since unchanged files are skipped.

It also generates a handy log file while the terminal window still shows the progress, thanks to the TEE operator.
The only thing that is not shown is the percentage done during each copy operation, since this would blow up the log file unnecessarily.

This is it (Link to the txt):

@echo off
REM Changing Codepage to 65001 to make the log file more readable
REM (especially for umlauts)
chcp 65001

REM Enter default backup source and destination here
SET source=Q:
SET destination="G:\USB"

REM ISO 8601 Date and Time in basic format (YYYYMMDDTHHMMSS)
SET isodt=%date:~6,4%%date:~3,2%%date:~0,2%T%time:~0,2%%time:~3,2%%time:~6,2%
SET isodt=%isodt: =0%

REM Set Logfile dir to current path
SET Path=%~dp0
REM Using anything but "Path" didn't work in my tests when running ad admin,
REM as otherwise the log would end up in system32.

REM Create logfile name with timestamp
SET LogNo=RobocopyBackupLog_%isodt%.txt

:BEGIN
cls
echo BACKUP WITH ROBOCOPY
echo ====================
echo.
echo IMPORTANT: RUN AS ADMINISTRATOR
echo.
echo Source:      %source%
echo Destination: %destination%
echo Files:       *.*
echo Logfile Dir: "%Path%%LogNo%"
echo.
echo Copy Parameters
echo ---------------
echo /E        Copies subdirectories. This option automatically includes empty
echo           directories.
echo /ZB       Copies files in restartable mode. If file access is denied,
echo           switches to backup mode.
echo /J        Copies using unbuffered I/O (recommended for large files).
echo /COPY:DAT Specifies which file properties to copy. The valid values for
echo           this option are: D - Data, A - Attributes, T - Time stamps
echo /MIR      Mirrors the directory tree
echo /MT:8     Creates multi-threaded copies with n threads.
echo /R:5      3 retries on failed copies
echo /W:2      2 second wait between retries
echo /EFSRAW   Copies all encrypted files in EFS RAW mode.
echo.
echo Logging Parameters
echo ------------------
echo /X      Reports all extra files, not just those that are selected.
echo /V      Produces verbose output, and shows all skipped files.
echo /TS     Includes source file time stamps in the output.
echo /FP     Includes the full path names of the files in the output.
echo /DEBUG  Show volume debug information
echo /TEE    Writes the status output to console window and log file.
echo /NP     Progress of copying op not displayed. (No percent because logfile).
echo.
echo WARNING: The Backup will be a mirror of the source. Everything in the
echo destination will be overwritten with newer versions. Robocopy will copy
echo subdirectories. Note that this option includes empty directories.
echo Robocopy DELETES destination files and directories that no longer
echo exist in the source.
echo ---------------------------------------------------------------------------
echo.
CHOICE /C YNC /N /M "Are the settings correct and shall the backup process begin? Press Y for Yes, N for No or C for Cancel"
IF ERRORLEVEL ==3 GOTO CANCEL
IF ERRORLEVEL ==2 GOTO NEEDSWORK
IF ERRORLEVEL ==1 GOTO BACKUP
:BACKUP
CLS
robocopy "%source%" "%destination%" *.* /E /ZB /J /COPY:DAT /MIR /MT:8 /R:5 /W:2 /X /V /TS /FP /DEBUG /tee /np /UNILOG:"%Path%%LogNo%"
echo.
echo The backup process has finished!
echo.
echo The logfile is: "%Path%%LogNo%"
echo.
echo If this process happened suspiciously fast please check the log.
echo Maybe you didn't run the .bat as administrator.
echo.
pause
GOTO END
:NEEDSWORK
CLS
echo.
echo If the settings are incorrect, please adjust them now, assuming it's just
echo about the backup source and destination.
echo Otherwise you need to adjust the batch file itself.
echo.
echo You will need to confirm the setting again before the backup
echo actually starts.
echo.
echo.
echo Enter the backup source and destination like this:
echo.
echo      C:
echo.
echo No backslash needed.
echo.
echo For strings containing spaces enclose everything in quotation marks.
echo.
echo      "C:\Foo Bar"
echo.
echo Enter the source:
SET /P source=
echo.
echo  Thanks.
echo.
echo Now enter the destination:
SET /P destination=
echo.
echo OK.
echo.
echo The defined backup source is: %source%
echo.
echo The defined backup destination is: %destination%
echo.
echo.
echo In the next step, you will see the backup summary again.
echo.
pause
GOTO BEGIN
:CANCEL
CLS
echo.
echo The batch process will end now. No files have been processed.
echo No log has been written.
echo.
pause
GOTO END
:END
EXIT

The log looks something like this:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robustes Dateikopieren für Windows                              
-------------------------------------------------------------------------------

  Gestartet: Mittwoch, 22. Dezember 2021 10:09:31
OSVersion : 10.0 (22000) 55F0000A

   Quelle = \\?\Q:\
     Root : "Q:\"
  VolName : ""
   Serial : 9023F956
  MaxName : 255
   FSFlag : 00020206
   FSType : "exFAT"
   IsNTFS : 0

     Ziel : \\?\G:\USB\
     Root : "G:\"
  VolName : "My Passport"
   Serial : AE01704D
  MaxName : 255
   FSFlag : 03E70EFF
   FSType : "NTFS"
   IsNTFS : 1

    Fudge : 20000000

    Dateien : *.*
	    
  Optionen: *.* /V /X /TS /FP /TEE /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /ZB /J /NP /DEBUG /MT:8 /R:5 /W:2 

------------------------------------------------------------------------------

	        Gleich		     136 -A----------- 2021/10/25 15:04:48.000	\\?\Q:\10key.txt

.
.
.



------------------------------------------------------------------------------

           Insgesamt   KopiertÜbersprungenKeine Übereinstimmung    FEHLER    Extras
Verzeich.:      1494      1493      1494         0         0         0
  Dateien:     12220        83     12137         0         0         0
    Bytes:  36.179 g  142.73 m  36.039 g         0         0         0
   Zeiten:   0:00:41   0:00:10                       0:00:00   0:00:09


Geschwindigkeit:           14.437.412 Bytes/Sek.
Geschwindigkeit:              826,115 Megabytes/Min.
   Beendet: Mittwoch, 22. Dezember 2021 10:09:51