@echo off
rem a batch script to perform an incremental backup
rem syneticon GbR
rem Last changed 2003-04-11

rem result from an "rsm view" call:
rem Free Media 14902267A48A4F64A0321999F5B4B7DF
rem +- 4mm DDS A563A5483A8342E3BE5515C73DE4656D
rem Imported Media 194D1B4BDC2D48A9A514DE2BB284BCCF
rem Unrecognized Media 9A0573B536604A3BA98CD92ECB7C6ADF
rem +- 4mm DDS 353A1F180D5F4C549FAC0AA7102B4417
rem Backup 1E2BF9E8F1224027A7E0A664F493FEEF
rem +- 4mm DDS 00545AD84D8D43A08A11E44F33781772
rem +- Inkremental 9F7EBB9D3B16462C97E9E97C9EE972DF
rem +- Full 231A05D4268A4BF08336001B59E907EF
rem result from rsm view /Tlibrary
rem Offline Media 0122057F04EC4D178F1445E405AB115D
rem SAMSUNG CD-ROM SN-124 2322C88AE24B4907AEF4CD803070FA2E
rem ARCHIVE Python 06408-XXX SCSI Sequential Device ADF7B38BB64E4D17BB1DD58255C6268B
rem +- Drive 0 9DF22E179A9248679D88DC72CF441B09

rem set some values first
rem the GUID of our Tape Library / Tape Drive
SET TapeLibGUID=ADF7B38BB64E4D17BB1DD58255C6268B
rem the GUID of the "Free Media\4mm DDS" pool
SET FreeMediaGUID=A563A5483A8342E3BE5515C73DE4656D
rem the GUID of the "Unrecognized Media\4mm DDS" pool
SET UnrecMediaGUID=353A1F180D5F4C549FAC0AA7102B4417
rem the GUID of the "Backup\Incremental" pool
SET IncrementalGUID=9F7EBB9D3B16462C97E9E97C9EE972DF
rem the GUID of the "Backup\Full" pool
SET FullGUID=231A05D4268A4BF08336001B59E907EF
rem The directory which we write our log files to.
SET OurLogDir=C:\Backup
rem the mail address to send status mail to
SET BackupOperator=BackupOperator@syneticon.de
rem full path to the backup selection file
SET BackupSelection=C:\Backup\Backup.bks

rem fetch the actual date and reformat it to yyyy-mm-dd
rem (Be sure to get that changed according to your date format.
rem If you do not live in Europe, you probably will have to change
rem both - the delimeters and the order of the parameters)
FOR /f "tokens=2-4 delims=. " %%i in ('date /t') do SET date=%%k-%%j-%%i

rem Determine the expected / desired name for the media
FOR /F "tokens=1" %%i IN ('date /T') DO set ExpectedMedia=Incremental %%i

rem set the name of our log file
SET logfile=%OurLogDir%\messages-%date%.log

rem Guess the directory, where NTBACKUP will save its log files
rem It is a subdirectory of LocalAppData
rem Change this to whatever is appropiate to your system
SET BackupLogDir=%userprofile%\Lokale Einstellungen\Anwendungsdaten\Microsoft\Windows NT\NTBACKUP\data

:start
rem force a library refresh
rsm.exe refresh /LG%TapeLibGUID%
rem rem wait for completion
sleep 30

rem determine the name and guid of the media inserted
FOR /F " usebackq delims==" %%i IN (`rsm view /Tphysical_media /CG%TapeLibGUID% /B /GUIDDISPLAY`) DO set medGUID=%%i
FOR /F " usebackq delims==" %%i IN (`rsm view /Tphysical_media /CG%TapeLibGUID% /B`) DO set medName=%%i

echo INFO: Incremental Backup procedure %date% started >> %logfile%

rem do we have a tape at all?
IF "%medGUID%"=="" GOTO NoMedia

rem is the inserted media part of the "\free media\4mm DDS" pool?
FOR /F " usebackq delims==" %%i IN (`rsm view /Tphysical_media /GUIDDISPLAY /CG%FreeMediaGUID% /B`) DO IF %%i==%medGUID% GOTO Free

rem is it an unrecognized media?
FOR /F " usebackq delims==" %%i IN (`rsm view /Tphysical_media /GUIDDISPLAY /CG%UnrecMediaGUID% /B`) DO IF %%i==%medGUID% GOTO Unrecognized

rem is the inserted media part of the "\backup\incremental" pool?
FOR /F " usebackq delims==" %%i IN (`rsm view /Tphysical_media /GUIDDISPLAY /CG%IncrementalGUID% /B`) DO IF %%i==%medGUID% GOTO Incremental

rem is the inserted media part of the "\backup\full" pool?
FOR /F " usebackq delims==" %%i IN (`rsm view /Tphysical_media /GUIDDISPLAY /CG%FullGUID% /B`) DO IF %%i==%medGUID% GOTO Full

rem if none of the above, handle it appropiately
goto SomeOther

:NoMedia
echo ERROR: No media present in tape drive >> %logfile%
echo ERROR: Backup process aborted, no data was backed up >> %logfile%
goto end

:Free
rem Free media detected, initialize and move to the "Incremental" pool
rem prior to further use
echo INFO: Free media found, initializing it first >> %logfile%
goto doInitialize

:Unrecognized
rem Everything's fine, although media is unrecognized. Log & proceed
echo INFO: Unrecognized media found, initializing it first >> %logfile%
goto doBackup

:Full
rem Media is out of the "Full" media pool - log & abort here
echo ERROR: The media "%medName%" has already been used for a full backup. >> %logfile%
echo ERROR: Backup process aborted, no data was backed up. >> %logfile%
goto end

:SomeOther
rem Log that the inserted tape was in the wrong media pool and exit
echo ERROR: The media "%medName%" is already allocated to another pool. >> %logfile%
echo ERROR: Backup process aborted, no data was backed up. >> %logfile%
goto end

:Incremental
rem Incremental backup, incremental pool - everything fine, go on
echo INFO: Media out of the Backup\Incremental pool is going to be used. >> %logfile%
goto doBackup

:doBackup
rem check if we have the expected medium name and log a warning if we do not
IF not "%ExpectedMedia% - 1"=="%medName%" echo WARNING: Media "%ExpectedMedia%" expected, media "%medName%" loaded. >> %logfile%

rem Log & start backup procedure
echo INFO: backup process starting >> %logfile%

start /wait C:\WINNT\system32\NTBACKUP.EXE backup "@%BackupSelection%" /a /d "Set %date%" /v:yes /r:no /rs:no /hc:on /m incremental /j "Inkremental %date%" /l:s /UM /p "Inkremental"

if errorlevel 1 goto backuperror

rem log success
echo INFO: backup process terminated gracefully >> %logfile%
goto endbackup

:backuperror
rem log errors
echo ERROR: backup process has thrown errors, inspect the backup log >> %logfile%
goto endbackup

:endbackup
rem log eject & eject
echo INFO: ejecting media "%medName%" >> %logfile%
rsm eject /PG%medGUID% /ASTART >> %logfile%
goto end

:doInitialize
rem initialize the tape with some help from ntbackup
rem log what we are doing
echo INFO: Inserted media either new or empty >> %logfile%
echo INFO: Media will be initialized... >> %logfile%
echo INFO: Media will be assigned to the \Backup\Inkrementiell media pool >> %logfile%
rem format tape and move it to "\Backup\Inkrementiell" pool
start /wait ntbackup backup c:\boot.ini /m normal /p "Inkrementiell" /UM
if not errorlevel 1 goto start

rem did we encounter errors?
echo ERROR: Media init for "%medName%" failed. >> %logfile%
echo ERROR: Backup process aborted, no data was backed up. >> %logfile%
goto endbackup

:end
rem determine the log file with the latest change
FOR /F "tokens=*" %%i IN ('dir /B /OD /TW "%BackupLogDir%\*.log"') DO set backuplog=%%i
rem send logfiles to responsible operators
blat %logfile% -to %BackupOperator% -s "%date% backup report (incremental)" -attacht "%BackupLogDir%\%backuplog%"