@ECHO OFF REM cmd script to back-up under MS Windows using simple directory rotation REM and Drive Snapshot from Tom Ehlert Software REM REM Author and copyright 2018-2021 Dr. Thomas Redelberger, redetho@gmx.de REM REM This script is provided as-is. I do not take any responsibility REM and do not grant any warranty nor do I imply any fitness for any REM purpose or kind, nor shall I be liable for any issues when using it. REM REM * Prerequisites REM REM - Date format /must/ be set to ISO (YYYY-MM-DD) in account preferences of REM the admin account this script is run under! REM REM - Directory DS must exist in the root directory of the target drive and REM a directory with name of PC to be backed-up thereunder REM REM - On the drive where the back-up is to be stored, there must be a volume REM with the label that starts with "MYBCKP" (without quotes). This will be REM the target volume REM REM Version for all my PCs REM REM $Id: myDS-Backup.cmd 1.2 2021/09/05 16:35:13 redetho Exp redetho $ REM * Key definitions REM Drive label prefix of the backup drive where backup(s) are to be written REM to. REM Need to have substring length below (see line ***) aligned to the REM following literal string's length (6 in this case)! set myBckLbl=MYBCKP REM * Find the Drive Snapshot executable REM first look for a 32bit executable on a 64bit Windows SET myExe="%ProgramFiles(x86)%\DriveSnapshot\snapshot.exe" REM then look for a 64bit executable on a 64bit Windows IF NOT EXIST %myExe% SET myExe="%ProgramFiles%\DriveSnapshot\snapshot64.exe" REM finally look for a 32bit executable on a 32bit Windows IF NOT EXIST %myExe% SET myExe="%ProgramFiles%\DriveSnapshot\snapshot.exe" REM Give up if neither of the three exist IF NOT EXIST %myExe% GOTO error1 REM * Find out drive letter of the backup drive REM preset undefined result SET myBckDrv= REM use Drive Snapshot to enumerate all drives FOR /F "skip=1 tokens=1,8" %%G IN ('%myExe% -W --showlist') DO CALL :catchlbl %%G %%H IF NOT DEFINED myBckDrv GOTO error2 ECHO Good: identified backup drive letter is %myBckDrv% REM * Prepare date variables REM Assumes ISO date (YYYY-MM-DD) is set in personal (account) preferences! SET myYear=%DATE:~0,4% SET myMonth=%DATE:~5,2% SET myDay=%DATE:~8,2% SET myYYYYMMDD=%myYear%%myMonth%%myDay% SET myYYYYMM=%myYear%%myMonth% REM * Set options for Drive Snapshot SET myBckPath=%myBckDrv%\DS\%COMPUTERNAME% REM SET myLog=--LogFile:Logfilename.log REM The following adds an option to limit the IO rate to 10 MB/s. REM I had used that to avoid too high CPU usage REM SET myOpt=-L0 -W --AutoBackupSize:512 --ForceVSS --PrintTotals --LimitIORate:10 SET myOpt=-L0 -W --AutoBackupSize:512 --ForceVSS --PrintTotals REM -L0 store image in /one/ file rather than splitting REM -W No /Hit any key to continue/ message REM -hPath\Filename.hsh generate a /differential/ image. The .hsh file must already exist! REM --AutoBackupSize:512 automatically save partitions up to 512MB (cmd line default is 0 !) REM --LogFile:Logfilename.log write to a logfile istead of stdout REM --ForceVSS Use VSS and exit with error if VSS is not available REM --PrintTotals REM --exclude:path1,path2,filename1,filename REM Just to get drive info: REM Human readable REM %myExe% --show REM To be used by scripts REM %myExe% --showlist REM * Establish whether a full or differential backup is to be done REM Do a /full/ backup when month is different from last time SET /P myLastSet=<%myBckPath%\LastSet.txt SET /P myLastFullDt=<%myBckPath%\LastFullDt.txt SET myLastYYYYMM=%myLastFullDt:~0,6% IF "%myLastYYYYMM%"=="%myYYYYMM%" GOTO DiffBck REM :FullBck Rotate the backup directory IF "%myLastSet%"=="A" SET myNxtSet=B IF NOT "%myLastSet%"=="A" SET myNxtSet=A SET myLastSet=%myNxtSet% ECHO Full %myLastSet% SET myDiff= DEL /Q %myBckPath%\%myLastSet%\*.sna %myBckPath%\%myLastSet%\*.hsh GOTO Cont1 :DiffBck ECHO Diff %myLastSet% SET myDiff=-h%myBckPath%\%myLastSet%\%myLastFullDt%_$disk_ful.hsh :Cont1 ECHO On REM Separate commands as there is no direct dependency %myExe% C: %myBckPath%\%myLastSet%\%myYYYYMMDD%_$disk_$type.sna %myOpt% %myLog% %myDiff% IF %ERRORLEVEL% NEQ 0 GOTO error3 %myExe% D: %myBckPath%\%myLastSet%\%myYYYYMMDD%_$disk_$type.sna %myOpt% %myLog% %myDiff% IF %ERRORLEVEL% NEQ 0 GOTO error3 @ECHO OFF REM Success. REM If it was a full backup, write out the new directory IF "%myLastYYYYMM%"=="%myYYYYMM%" GOTO exit ECHO %myLastSet%>%myBckPath%\LastSet.txt ECHO %myYYYYMMDD%>%myBckPath%\LastFullDt.txt GOTO exit REM * Auxiliary subroutines :catchlbl SET myVolLbl=%2 REM *** keep left substring length aligned to definition above IF %myVolLbl:~0,6%==%myBckLbl% SET myBckDrv=%1 EXIT /B REM * Error handlers :error1 ECHO Error: Drive Snapshot exe file not found GOTO exit :error2 ECHO Error: No backup drive with label starting with %myBckLbl% found GOTO exit :error3 ECHO Error code from DriveSnapshot: %ERRORLEVEL% REM GOTO exit - fall thru :exit REM Have a look... PAUSE