Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backup Script for NTBackup on 2003

Status
Not open for further replies.

alfcateat

MIS
Dec 31, 2003
4
US
Hello All-
Just sharing here. I have put quite a few hours into a script which can be used to automate backups on Windows 2003 Server. I strongly suspect it could be used for Windows 2000 as well. I hope that this script may be of use to you. If you have any recommendations for improvements, or whatever, please feel free to send them my way or post them here.

Thanks-
John Tracy

Here's the script:

REM @echo off

REM
REM This script automates backups and is meant to be run by task scheduler.
REM It will back up the items specified in backup.bks, which contains your backup
REM selections. It is generated from NT Backup (File, Save after you've made your
REM backup selections with ntbackup.exe).
REM
REM For this script to function for you, in addition to creating the backup.bks file,
REM you must define the smtp, domain, adminemail, drvguid, backuppath, and pool variables
REM as appropriate for your environment. I've tried to make it clear below how to
REM enumerate those values.
REM
REM This script created by John Tracy (tracy@covenant.edu) based on instructions written
REM by REM Brien M. Posey available at
REM REM and an article written by Crobin1 at REM
REM It is known to work on Windows 2003 Server with SP1, and suspected to work on Win2K,
REM and other variants
REM
REM It has a dependency to make it run that you'll need to download:
REM mailsend is a free program available at
REM REM
REM Note: I've had it occur that the removeable storage database becomes corrupt. If this
REM occurs, stop the "Removable Storage" service, go to the
REM %SystemRoot%\system32\NtmsData folder and delete the files. Restart the service
REM and it will recreate the files. My symptoms were that I coudn't do anything with
REM the RSM GUI without getting an error. Beware: This removes existing tape catalogs,
REM so to restore these tapes you'll have to reindex them.

REM _______________You must set all of the variables below_________________
REM Failure to change these values will guarantee that the scipt will not work for you.

REM Set the value of remove of the tape storage device. This value can be
REM determined by running the command "REM rsm view /tlibrary /guiddisplay"
REM This has to be changed everytime the hardware changes, or the drive is reinstalled

set drvguid=005C4095974D4AF3A1F032B2A95B6D00

REM Set the value of the script's home directory. This path should include
REM this batch file, as well as you backup.bks file, and the mailsend.exe program.

set backuppath=c:\backup

REM Set the value of the tape drives media pool name. Probably the easiest way to
REM do this is to use the GUI for the Removable Storage Manager, and click down under
REM "Removeable Storage" > "Media Pools" > Free and see what the pool name is.
REM Hint: It's probably DLT or "4mm DDS"

set pool=DLT

REM Set adminemail, the e-mail address that you would like the output of these jobs to be
REM sent.

set adminemail=noc@yourdomain.com

REM Set domain so that mailsend.exe won't need it specified each time it runs. Can be
REM whatever you want it to be.

set domain=yourdomain.com

REM Set your smtp server's IP address here (to process outbound e-mail logs)

set smtp=10.0.0.1


REM ___________________Here's where the scipt begins____________________


REM Delete old backup logs that might be laying around
del %backuppath%\backup.log
del /f /q "%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\*"

REM let's restart the removeable storage manager so that it clears any work requests
REM or idle/hung processes
start /wait net stop NtmsSvc
start /wait net start NtmsSvc

REM Let's refresh the tape drive and wait a minute to make sure everything is kosher
start /wait rsm refresh /lg%drvguid%
ping -n 30 -w 1000 15.15.15.15 >NUL

REM create logfile and fill out some details
echo Beginning backup of %COMPUTERNAME% on %date% at %time% > %backuppath%\backup.log
echo. >> %backuppath%\backup.log
echo ___We're going to backup the following items___ >> %backuppath%\backup.log
type %backuppath%\backup.bks >> %backuppath%\backup.log
echo. >> %backuppath%\backup.log
echo _______________________________________________ >> %backuppath%\backup.log
echo. >> %backuppath%\backup.log

REM There is probably an easier way to do this... but this is what I have
REM This part figures out if there is a tape physically present in the drive
REM If it's there, the backup is allowed to continue. If absent, the backup
REM immediately aborts and sends an e-mail stating that no usuable media was present.

REM List all media present in the tape drive
rsm view /tphysical_media /cg%drvguid% > %backuppath%\mediacheck.log

REM Create a variable called linenum. Pump out the line number on which the text, "The command completed successfully."
REM appears at the beginning of the line. It will also give us the actual matching text, which we'll strip out in a moment.
FOR /F "usebackq delims==" %%x IN (`findstr /N /B /C:"The command completed successfully." %backuppath%\mediacheck.log`) DO

set linenum=%%x

REM take our new linenum variable, and strip out everything except the first character,
REM and stick it in a variable named mediacount
set mediacount=%linenum:~0,1%

REM if mediacount is less than six, there is no media in the drive
if %mediacount% LEQ 5 goto INVALIDMEDIA

rem Get the GUID of the tape in the drive
FOR /F "usebackq delims==" %%i IN (`rsm view /tphysical_media /cg%drvguid% /guiddisplay /b`) DO set tapeguid=%%i

rem Get the partition GUID of the tape
FOR /F "usebackq delims==" %%i IN (`rsm view /tpartition /cg%tapeguid% /guiddisplay /b`) DO set partguid=%%i

rem Get the logical media GUID of the partition; needed by NTBackup
FOR /F "usebackq delims==" %%i IN (`rsm view /tlogical_media /cg%partguid% /guiddisplay /b`) DO set logguid=%%i

rem If the tape is new, logguid will contain
rem this string: Unable to view information for all objects.
rem Due to some command-line oddities we take just the first 6 characters of
rem logguid to make the comparison
set t1=%logguid:~0,6%
if /i %t1% neq Unable goto CONTINUE

:newtape
rem Since we have a new tape, we'll need to move it to the Free Media pool
start /wait rsm freemedia /lg%drvguid%

REM now let's start the backup. Since this is new tape, we tell the backup to
REM use the pool instead of a piece of physical media.
start /wait NTBACKUP backup @%backuppath%\backup.bks /N "%COMPUTERNAME% %DATE%" /v:no /hc:eek:n /m normal /L:f /P "DLT" /SNAP:eek:n
goto TESTFORFAILURE

:CONTINUE
REM The following set the variables specifying the globally unique id of the
REM current tape's partitions and logical volumes. Yes--tapes have filesystems
REM just like any other media.
FOR /F "usebackq delims==" %%x IN (`rsm view /tphysical_media /cg%drvguid% /guiddisplay /b`) DO set tapeguid=%%x
FOR /F "usebackq delims==" %%x IN (`rsm view /tpartition /cg%tapeguid% /guiddisplay /b`) DO set partguid=%%x
FOR /F "usebackq delims==" %%x IN (`rsm view /tlogical_media /cg%partguid% /guiddisplay /b`) DO set logguid=%%x

REM format the variable bkupguid in a format that ntbackup appreciates (requires)
Set part1=%logguid:~0,8%
Set part2=%logguid:~8,4%
Set part3=%logguid:~12,4%
Set part4=%logguid:~16,4%
Set part5=%logguid:~20,12%
Set bkupguid=%part1%-%part2%-%part3%-%part4%-%part5%
echo %bkupguid%

REM Startup the backup for known media.
REM Command line switches are documented by running ntbackup /? on any windows box
NTBACKUP backup @%backuppath%\backup.bks /g "%bkupguid%" /N "%COMPUTERNAME% %DATE%" /v:no /hc:eek:n /m normal /L:f /SNAP:eek:n
goto TESTFORFAILURE

:TESTFORFAILURE
REM Display the raw Ntbackup log and convert it from ugly unicode to
REM plain text and append it to our backup log
type "%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\*" >> %backuppath%\backup.log

echo. >> %backuppath%\backup.log
echo All done at %date% %time% >> %backuppath%\backup.log

REM Let's try to determine if there are any failures and their precise cause

REM If somehow somebody managed to swap tapes after the backup was started, this error condition should result
findstr /C:"The operation was not performed because an invalid Removable Storage GUID was specified." %backuppath%\backup.log
if %errorlevel% == 0 goto INVALIDMEDIA

REM if a user clicks cancel during the backup operation, the log should say this:
findstr /B /C:"The operation was ended." %backuppath%\backup.log
if %errorlevel% == 0 goto CANCELLED

REM Check to see if the output log file is too small, indicating no backup occured
REM the trigger values for loglength might have to be changed if you have quite a few
REM item specified in the backup.bks file
FOR /F "usebackq delims==" %%x IN (`findstr /N /B /C:"All done" %backuppath%\backup.log`) DO set logcheck=%%x
set loglength=%logcheck:~0,2%
if %loglength% LEQ 25 goto INVALIDMEDIA
if %loglength% GEQ 26 goto SENDSUCCESS

REM And finally, a catch all for any other failed backups
findstr /B /C:"The operation did not successfully complete." %backuppath%\backup.log
if %errorlevel% == 0 goto FAILURE1

REM If none of these matched, we assume success
goto SENDSUCCESS

:INVALIDMEDIA
REM This section appends to the log stating that no valid media was found, and then sends off an e-mail
echo The backup did not have valid media on which to dump data. >> %backuppath%\backup.log
echo Either there was no tape in the drive, it is damaged, or write protected. >> %backuppath%\backup.log
%backuppath%\mailsend -f %COMPUTERNAME% -d %domain% -smtp %smtp% -t %adminemail% -sub "%COMPUTERNAME% Backup FAILED on

%date%--INVALID MEDIA" -a backup.log
goto CLEANUP

:FAILURE1
REM This secion just sends out the log file with a failure e-mail
%backuppath%\mailsend -f %COMPUTERNAME% -d %domain% -smtp %smtp% -t %adminemail% -sub "%COMPUTERNAME% Backup FAILED on

%date%" -a backup.log
goto CLEANUP

:CANCELLED
REM This secion just sends out the log file with a cancellation e-mail
%backuppath%\mailsend -f %COMPUTERNAME% -d %domain% -smtp %smtp% -t %adminemail% -sub "%COMPUTERNAME% Backup CANCELLED on

%date%" -a backup.log
goto CLEANUP


:SENDSUCCESS
REM This section sends an e-mail in the event of a successful backup. The log file will be attached
%backuppath%\mailsend -f %COMPUTERNAME% -d %domain% -smtp %smtp% -t %adminemail% -sub "%COMPUTERNAME% Backup SUCCESS on

%date%" -a backup.log
GOTO CLEANUP

:CLEANUP
REM This secion cleans up all of our temporary files
REM del %backuppath%\backup.log
REM del %backuppath%\mediacheck.log

REM ejecting tape
REM rsm eject /pg%tapeguid% /astart
 
One quick change... On the ntbackup.exe command line that has "DLT" in quotes, that "DLT" should be replaced by %pool% (without quotes). Sorry for any confusion. I couldn't see how to edit my post... if I can figure out how I'll go back and change it above.

Thanks!
 
Make that, with quotes on the %pool". So the line should read start /wait NTBACKUP backup @%backuppath%\backup.bks /N "%COMPUTERNAME% %DATE%" /v:no /hc:eek:n /m normal /L:f /P "%pool%" /SNAP:eek:n

Cheers-
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top