omb
asked on
ntbackup batch file/script?
We would like to use NTBACKUP as a cheap solution to backups. Scheduled to run every evening is easy. However, we also would like the .log file to be printed out or emailed and the tape to be ejected on completion. Probably best to use a batch file or script - any ideas?
Thanks.
Thanks.
You should be able to schedule all this and all these options with the NTBACKUP schedule tab.
ASKER
I certainly can't find any options on the NTbackup schedule tab - please can you let me know where they are hidden. Thx
Hi omb,
Look here for some ideas on Win2k's NTBACKUP scripting:
http://www.jsiinc.com/SUBE/tip2200/rh2265.htm
A good example of dealing with tape mounting problems. Also includes a link on how to use BLAT for emailing:
http://www.jsiinc.com/SUBL/tip5900/rh5955.htm
pb
Look here for some ideas on Win2k's NTBACKUP scripting:
http://www.jsiinc.com/SUBE/tip2200/rh2265.htm
A good example of dealing with tape mounting problems. Also includes a link on how to use BLAT for emailing:
http://www.jsiinc.com/SUBL/tip5900/rh5955.htm
pb
ASKER
Thanks for the feedback pbarrette - some great links and I'm getting a lot further with the backup script now!
Once slight problem.. any idea how I determine the name of the ntbackup log file so that this can also be attached with the email I send use BLAT? Can a variable be used to determine the new log file name? Or how about getting ntbackup to always use the name log file name? but how?
Thanks for all your help.
Once slight problem.. any idea how I determine the name of the ntbackup log file so that this can also be attached with the email I send use BLAT? Can a variable be used to determine the new log file name? Or how about getting ntbackup to always use the name log file name? but how?
Thanks for all your help.
omb,
In my blat+ntbackup batch script i determine most recent log after ntbackup ends and email it if ntbackup succeed.
Like this:
---cut---
set bkpp="%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"
set subj="Daily backup report"
set blatbody="%temp%\%random%% random%.tx t"
set recipient=my@email.com
pushd %bkpp%
for /f "tokens=1 delims=" %%I in ('dir /B /O-D') do (
if "%%~xI"==".log" (
type "%%~fI" > %blatbody%
goto :eof
)
)
popd
C:\bin\blat.exe %blatbody% -s %subj% -to %recipient%
del /q /f "%blatbody%"
---cut---
In my blat+ntbackup batch script i determine most recent log after ntbackup ends and email it if ntbackup succeed.
Like this:
---cut---
set bkpp="%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"
set subj="Daily backup report"
set blatbody="%temp%\%random%%
set recipient=my@email.com
pushd %bkpp%
for /f "tokens=1 delims=" %%I in ('dir /B /O-D') do (
if "%%~xI"==".log" (
type "%%~fI" > %blatbody%
goto :eof
)
)
popd
C:\bin\blat.exe %blatbody% -s %subj% -to %recipient%
del /q /f "%blatbody%"
---cut---
Hi omb,
You will have to use something like the following to find the log file that was modified last:
:: ------------EXAMPLE.BAT--- ---------
@ECHO OFF
SET LOGDIR=C:\Documents and Settings\%USERNAME%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\Data
FOR /F %%F IN ('DIR /B /A-D /O-D /TW "%LOGDIR%\Backup??.log"') DO (
SET LOGFILE=%%F
GOTO GOTLOG
)
:GOTLOG
ECHO Last modified logfile is "%LOGDIR%\%LOGFILE%"
:: ------------EXAMPLE.BAT--- ---------
Hope this helps,
pb
You will have to use something like the following to find the log file that was modified last:
:: ------------EXAMPLE.BAT---
@ECHO OFF
SET LOGDIR=C:\Documents and Settings\%USERNAME%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\Data
FOR /F %%F IN ('DIR /B /A-D /O-D /TW "%LOGDIR%\Backup??.log"') DO (
SET LOGFILE=%%F
GOTO GOTLOG
)
:GOTLOG
ECHO Last modified logfile is "%LOGDIR%\%LOGFILE%"
:: ------------EXAMPLE.BAT---
Hope this helps,
pb
Hi 4auHuk,
Didn't see your post before I replied..
Anyway, it isn't necessary to create another file for this. You can just use something like:
BLAT -t me@myemail.com -s "Backup Log for %DATE%-%TIME%" -attach "%LOGDIR%\%LOGFILE%"
This, of course, is based on my above example, but will work for any BLAT message, provided you have a path and filename.
pb
Didn't see your post before I replied..
Anyway, it isn't necessary to create another file for this. You can just use something like:
BLAT -t me@myemail.com -s "Backup Log for %DATE%-%TIME%" -attach "%LOGDIR%\%LOGFILE%"
This, of course, is based on my above example, but will work for any BLAT message, provided you have a path and filename.
pb
No problem,
I cut-paste these parts from my script in wich i have a reason to create another file for blat message body. In case of ntbackup failure body gets error notification, in case of success it gets header, backup log content and footer.
If you send backup log as attachment sure you do not need to create an extra file.
4auHuk.
I cut-paste these parts from my script in wich i have a reason to create another file for blat message body. In case of ntbackup failure body gets error notification, in case of success it gets header, backup log content and footer.
If you send backup log as attachment sure you do not need to create an extra file.
4auHuk.
Just as addon:
If ntbackup fails, depending on a stage at wich failure occurs (say, ntbackup fails to start completely due to error in switches which are composed dynamically or for some other reason) ntbackup may not create log. In this case you can recieve wrong log file. You should handle this possible behaviour in your script.
4auHuk
If ntbackup fails, depending on a stage at wich failure occurs (say, ntbackup fails to start completely due to error in switches which are composed dynamically or for some other reason) ntbackup may not create log. In this case you can recieve wrong log file. You should handle this possible behaviour in your script.
4auHuk
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow guys, what tremendous feeback... so here are a few more points ;o)... having 'played' with both scripts, I have several questions:
4auHuk: I like the idea of having a 'blatbody' variable in order to customise messages depending on backup outcome. However, I can't get it to work?! Every time the script runs, it says that the file cannot be found. Blat then fails as it can't find the blatbody message text. The bits I am looking at are:
set blatbody="%temp%\%random%% random%.tx t"
type "%%~fI" > %blatbody%
del /q /f "%blatbody%"
pbarrette: Your script which finds the log file that was modified last works great. For some reason, I couldn't get 4auHuk's to work. BTW, I am a scripting novice so go easy on me. However, I kind of understand your scripts and they kind of work!
I use the following to send the email (variables defined above):
%blat% %blatbody1% -to %recipient% -s %subj1% -attach "%LOGDIR%\%LOGFILE%"
I have used a combination of both scripts and also tried to include a little error checking (check for a tape) as in article http://www.jsiinc.com/SUBL/tip5900/rh5955.htm - but I have a problem with accented characters. Unfortunately, some of the OS installations I use are in German, so sentences such as "The command completed successfully." are translated as "Der Befehl wurde ausgeführt.". Hence the error checking does not really work - the "ü" is not correctly displayed when run. The bit of the script I am referring to is:
if not "%%a"=="The command completed successfully." set media="%%a"
rem (or in German...)
if not "%%a"=="Der Befehl wurde ausgeführt." set media="%%a"
Thanks so much for your invaluable feedback and support.
4auHuk: I like the idea of having a 'blatbody' variable in order to customise messages depending on backup outcome. However, I can't get it to work?! Every time the script runs, it says that the file cannot be found. Blat then fails as it can't find the blatbody message text. The bits I am looking at are:
set blatbody="%temp%\%random%%
type "%%~fI" > %blatbody%
del /q /f "%blatbody%"
pbarrette: Your script which finds the log file that was modified last works great. For some reason, I couldn't get 4auHuk's to work. BTW, I am a scripting novice so go easy on me. However, I kind of understand your scripts and they kind of work!
I use the following to send the email (variables defined above):
%blat% %blatbody1% -to %recipient% -s %subj1% -attach "%LOGDIR%\%LOGFILE%"
I have used a combination of both scripts and also tried to include a little error checking (check for a tape) as in article http://www.jsiinc.com/SUBL/tip5900/rh5955.htm - but I have a problem with accented characters. Unfortunately, some of the OS installations I use are in German, so sentences such as "The command completed successfully." are translated as "Der Befehl wurde ausgeführt.". Hence the error checking does not really work - the "ü" is not correctly displayed when run. The bit of the script I am referring to is:
if not "%%a"=="The command completed successfully." set media="%%a"
rem (or in German...)
if not "%%a"=="Der Befehl wurde ausgeführt." set media="%%a"
Thanks so much for your invaluable feedback and support.
ASKER
I've just had another look at the error checking script and am totally confused :o(... With a tape in the drive, I run:
rsm view /cg%guiddisplay% /TPhysical_media
and receive a display:
PHYSICAL_MEDIA
Tape_label_name - 1
The command completed successfully.
When I run the same command without a tape in the drive, I receive:
PHYSICAL_MEDIA
The command completed successfully.
I can't make sense of the the script (below). Which part of the script is looking for my blank line, which is where the tape label would be if there was a tape in the drive?
:checkmedia
set media=
for /f "Tokens=*" %%a in ('rsm view /cgC888E08900454C4C956DF84 868C17C78 /TPhysical_media') do (
if not "%%a"=="The command completed successfully." set media="%%a"
)
if /i %media% EQU "Unable to open session with the RSM server." goto checkmedia
if /i %media% EQU "PHYSICAL_MEDIA" goto notape
if not defined media goto notape
All I want to be able to test is whether there is a tape in the drive or not - if yes, run the backup and send email with backup log, if no, abort backup and send email saying backup failed. So close, but yet so far! Thanks again for your comments...
rsm view /cg%guiddisplay% /TPhysical_media
and receive a display:
PHYSICAL_MEDIA
Tape_label_name - 1
The command completed successfully.
When I run the same command without a tape in the drive, I receive:
PHYSICAL_MEDIA
The command completed successfully.
I can't make sense of the the script (below). Which part of the script is looking for my blank line, which is where the tape label would be if there was a tape in the drive?
:checkmedia
set media=
for /f "Tokens=*" %%a in ('rsm view /cgC888E08900454C4C956DF84
if not "%%a"=="The command completed successfully." set media="%%a"
)
if /i %media% EQU "Unable to open session with the RSM server." goto checkmedia
if /i %media% EQU "PHYSICAL_MEDIA" goto notape
if not defined media goto notape
All I want to be able to test is whether there is a tape in the drive or not - if yes, run the backup and send email with backup log, if no, abort backup and send email saying backup failed. So close, but yet so far! Thanks again for your comments...
Not sure what is the prolbem.
You can remove @echo off in your script for troublrshooting.
At which line(s) you get this error?
You can also insert "echo %blatbody% & pause" after "set blatbody" to see what you get in variable. Same for other variables.
If for some reason "%temp%\%random%%random%.t xt" is what causes problems you can set blatbody to whatever you wish. E.g. c:\winnt\temp\blatbody.txt
4auHuk
You can remove @echo off in your script for troublrshooting.
At which line(s) you get this error?
You can also insert "echo %blatbody% & pause" after "set blatbody" to see what you get in variable. Same for other variables.
If for some reason "%temp%\%random%%random%.t
4auHuk
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great stuff guys...
4auHuk, I changed:
type "%%~fI" > %blatbody%
to
echo "%%~fI" > %blatbody%
...works great with the temp files & msg variables.
pbarrette, I changed:
ECHO %LINE% | FINDSTR "The command" > NUL
to
ECHO %LINE% | FINDSTR "Der Befehl" > NUL
...the script works perfect and sends different messages depending on if there is a tape in the drive or not - yeee haaa! Will this also work if I insert a brand new tape (which has been never used before)?
FYI, my script now looks like this:
:: ----------BACKUP.BAT------ ----
set LOGDIR=%USERPROFILE%\Lokal e Einstellungen\Anwendungsda ten\Micros oft\Window s NT\NTBackup\data
set subj1="%computername% Backup Failure"
set subj2="%computername% Backup Successful"
set recipient=alias@domain.de
set blatErrorText=Backup error text for %computername% at %USERDOMAIN% on
set blatOKtext=Backup ran OK text for %computername% at %USERDOMAIN% on
set guiddisplay=E759FC802D6140 6A9C1B1DDC 6764ABEE
set ntbackup=d:\WINNT\system32 \NTBACKUP. EXE
set bks=d:\backup\daily.bks
set blat=d:\downloads\blat\bla t.exe
set DDSdevice=HP C1537A SCSI Sequential Device
set blatErrorFile="%temp%\%ran dom%%rando m%.txt"
set blatOKfile="%temp%\%random %%random%. txt"
SET MEDIA=
SET LINE=
rsm.exe inventory /LF"%DDSdevice%" /AFULL
sleep 30
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j%%k%%l
for /f "Tokens=1" %%i in ('time /t') do set tm=-%%i
set dtt=%dt%%tm%
FOR /F "SKIP=2 DELIMS=" %%F IN ('rsm view /cg%guiddisplay% /TPhysical_media') DO (
SET LINE=%%F
CALL :PROCESS
)
GOTO :GOTTAPE
:PROCESS
ECHO %LINE% | FINDSTR "Der Befehl" > NUL
IF ERRORLEVEL 1 (
ECHO %LINE% | FINDSTR "Der Befehl" > NUL
IF ERRORLEVEL 1 (
SET MEDIA=%LINE%
)
)
GOTO :EOF
:NOTAPE
echo %blatErrorText% %dtt% > %blatErrorFile%
%blat% %blatErrorFile% -to %recipient% -s %subj1%
del /q /f "%blatErrorFile%"
GOTO :EOF
:GOTTAPE
IF NOT DEFINED MEDIA ( GOTO NOTAPE )
ECHO Media Name = "%MEDIA%"
:RUNBACKUP
%ntbackup% backup "@%bks%" /n "%computername%-%dtt%" /d "%computername% voll Sicherung" /v:no /r:no /rs:no /hc:on /m normal /j "%computername% voll Sicherung" /l:s /p "4mm DDS" /UM
rsm.exe eject /PF"%computername%-%dtt% - 1" /astart
:BACKUPRAN
FOR /F %%F IN ('DIR /B /A-D /O-D /TW "%LOGDIR%\Backup??.log"') DO (
SET LOGFILE=%%F
)
ECHO Last modified logfile is "%LOGDIR%\%LOGFILE%"
echo %blatOKtext% %dtt% > %blatOKfile%
%blat% %blatOKfile% -to %recipient% -s %subj2% -attach "%LOGDIR%\%LOGFILE%"
del /q /f "%blatOKfile%"
goto :EOF
:EOF
exit
:: ----------BACKUP.BAT------ ----
Any final comments or suggestions on my script? Just a thought, what would happen if the scheduled task is manually stopped or automatically ended after x hours (option in scheduler)? Do I also need error checking here? Thanks so much for all your tremendous help... and more points for my appreciation :0)
4auHuk, I changed:
type "%%~fI" > %blatbody%
to
echo "%%~fI" > %blatbody%
...works great with the temp files & msg variables.
pbarrette, I changed:
ECHO %LINE% | FINDSTR "The command" > NUL
to
ECHO %LINE% | FINDSTR "Der Befehl" > NUL
...the script works perfect and sends different messages depending on if there is a tape in the drive or not - yeee haaa! Will this also work if I insert a brand new tape (which has been never used before)?
FYI, my script now looks like this:
:: ----------BACKUP.BAT------
set LOGDIR=%USERPROFILE%\Lokal
set subj1="%computername% Backup Failure"
set subj2="%computername% Backup Successful"
set recipient=alias@domain.de
set blatErrorText=Backup error text for %computername% at %USERDOMAIN% on
set blatOKtext=Backup ran OK text for %computername% at %USERDOMAIN% on
set guiddisplay=E759FC802D6140
set ntbackup=d:\WINNT\system32
set bks=d:\backup\daily.bks
set blat=d:\downloads\blat\bla
set DDSdevice=HP C1537A SCSI Sequential Device
set blatErrorFile="%temp%\%ran
set blatOKfile="%temp%\%random
SET MEDIA=
SET LINE=
rsm.exe inventory /LF"%DDSdevice%" /AFULL
sleep 30
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j%%k%%l
for /f "Tokens=1" %%i in ('time /t') do set tm=-%%i
set dtt=%dt%%tm%
FOR /F "SKIP=2 DELIMS=" %%F IN ('rsm view /cg%guiddisplay% /TPhysical_media') DO (
SET LINE=%%F
CALL :PROCESS
)
GOTO :GOTTAPE
:PROCESS
ECHO %LINE% | FINDSTR "Der Befehl" > NUL
IF ERRORLEVEL 1 (
ECHO %LINE% | FINDSTR "Der Befehl" > NUL
IF ERRORLEVEL 1 (
SET MEDIA=%LINE%
)
)
GOTO :EOF
:NOTAPE
echo %blatErrorText% %dtt% > %blatErrorFile%
%blat% %blatErrorFile% -to %recipient% -s %subj1%
del /q /f "%blatErrorFile%"
GOTO :EOF
:GOTTAPE
IF NOT DEFINED MEDIA ( GOTO NOTAPE )
ECHO Media Name = "%MEDIA%"
:RUNBACKUP
%ntbackup% backup "@%bks%" /n "%computername%-%dtt%" /d "%computername% voll Sicherung" /v:no /r:no /rs:no /hc:on /m normal /j "%computername% voll Sicherung" /l:s /p "4mm DDS" /UM
rsm.exe eject /PF"%computername%-%dtt% - 1" /astart
:BACKUPRAN
FOR /F %%F IN ('DIR /B /A-D /O-D /TW "%LOGDIR%\Backup??.log"') DO (
SET LOGFILE=%%F
)
ECHO Last modified logfile is "%LOGDIR%\%LOGFILE%"
echo %blatOKtext% %dtt% > %blatOKfile%
%blat% %blatOKfile% -to %recipient% -s %subj2% -attach "%LOGDIR%\%LOGFILE%"
del /q /f "%blatOKfile%"
goto :EOF
:EOF
exit
:: ----------BACKUP.BAT------
Any final comments or suggestions on my script? Just a thought, what would happen if the scheduled task is manually stopped or automatically ended after x hours (option in scheduler)? Do I also need error checking here? Thanks so much for all your tremendous help... and more points for my appreciation :0)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the EOF label info - nice to know! The correct log is now emailed - great. Batch file now tried and tested in a live environment and it works perfect.
After lots and lots of testing, just found 1 problem... if one of the tapes in the drive had not been "prepared" in Computer Manager, Removable devices etc... although it was in the drive, and NTbackup tried to write to it, the whole process failed. The log file mentioned something about not being able to find correct media. However, the email that was sent said that the backup was successful (of course it did, 'coz it only checks to see if a tape is in the drive) - only looking into the attached log file could I see that the back was unsuccessful. Is there a way I can also error-trap this type of message?
Many thanks and more points!
After lots and lots of testing, just found 1 problem... if one of the tapes in the drive had not been "prepared" in Computer Manager, Removable devices etc... although it was in the drive, and NTbackup tried to write to it, the whole process failed. The log file mentioned something about not being able to find correct media. However, the email that was sent said that the backup was successful (of course it did, 'coz it only checks to see if a tape is in the drive) - only looking into the attached log file could I see that the back was unsuccessful. Is there a way I can also error-trap this type of message?
Many thanks and more points!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks for your feedback pb. I will try out your suggestions and let you know the results...
ASKER
Hi pb. I liked the idea of checking bits within the actual log files so that different messages could be parsed to the emails. I used your first example to check for 2 different conditions within the backup log file, as follows:
:CHECKLOGFORERRORS
ECHO "%LOGDIR%\%LOGFILE%" | FINDSTR "Die gesicherten Daten sind beschädigt oder unvollständig" > NUL
IF NOT ERRORLEVEL 1 (
ECHO "%BackupError1%"
set BackupErrorText=%BackupErr or1%
GOTO BACKUPnotOK
)
ECHO "%LOGDIR%\%LOGFILE%" | FINDSTR "Der Vorgang wurde nicht durchgeführt, da die angegebenen Medien nicht gefunden wurden" > NUL
IF NOT ERRORLEVEL 1 (
ECHO "%BackupError2%"
set BackupErrorText=%BackupErr or2%
GOTO BACKUPnotOK
)
GOTO :BACKUPOK
Does FINDSTR search for the complete text between the 2 quotes "..." or any word? It is important that it finds the whole sentence. I also wasn't sure how to combine both search in 1 command line, hence the FINDSTR twice. This script is getting real cool - thanks so much for all of your help and patience with my non-existent programming skills!
:CHECKLOGFORERRORS
ECHO "%LOGDIR%\%LOGFILE%" | FINDSTR "Die gesicherten Daten sind beschädigt oder unvollständig" > NUL
IF NOT ERRORLEVEL 1 (
ECHO "%BackupError1%"
set BackupErrorText=%BackupErr
GOTO BACKUPnotOK
)
ECHO "%LOGDIR%\%LOGFILE%" | FINDSTR "Der Vorgang wurde nicht durchgeführt, da die angegebenen Medien nicht gefunden wurden" > NUL
IF NOT ERRORLEVEL 1 (
ECHO "%BackupError2%"
set BackupErrorText=%BackupErr
GOTO BACKUPnotOK
)
GOTO :BACKUPOK
Does FINDSTR search for the complete text between the 2 quotes "..." or any word? It is important that it finds the whole sentence. I also wasn't sure how to combine both search in 1 command line, hence the FINDSTR twice. This script is getting real cool - thanks so much for all of your help and patience with my non-existent programming skills!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Turn123,
I'd suggest a split here.
pb
I'd suggest a split here.
pb
ASKER
Sorry that I have not come back on this issue - unfortunately I overlooked the fact that I hadn't graded this question and given due credit to those that helped. The feedback, esp. from pbarrette was outstanding A1. Thanks again for all your help - batch file works great.
Hi Omb,
Glad to be of help.
Don't worry too much about the lateness. I actually got a bit bored here a while back and haven't been visiting unless I get an email notification from an old question. So it's like visiting an old friend.
pb
Glad to be of help.
Don't worry too much about the lateness. I actually got a bit bored here a while back and haven't been visiting unless I get an email notification from an old question. So it's like visiting an old friend.
pb
I would be very grateful if you could post the final version of your batch file, omb. It's hard to piece it all together from this thread. Thanks. :)
ASKER
calling pbarrette... help me please! I've discovered the script that you wrote for me does NOT 100% work - my fault. I would really appreciate your help again, please see a new call I've opened "https://www.experts-exchange.com/questions/21457271/ntbackup-batch-script-problem.html" - thanks.