Tuesday, 29 March 2011

DOS Script to Email Results of a Database Export

Had to write a batch script to check on the status of a nightly database export and email status. Some of what I've done may not be perfect, but it does the job nicely and portions can be reused, hence it's appearance on this blog for future reference.

@ECHO OFF
REM Enable local environments to be changed without affecting anything else.
SETLOCAL

REM check the export.log file for the filedate and pass it into a variable. Done by doing a directory listing on the file, returning just the filename and then looping through and capturing the file's timestamp into a variable

for /f "tokens=* delims=" %%a in ('dir/b export.log') do (
for /f "tokens=1 delims=" %%i in ("%%a") do (
set _fdt=%%~Ti
)
)

REM Write the logfile contents to a secondary file. This is only done to allow us to append the results of a TSM tape backup of the export.dmp to a logfile and email it in the email body.

type export.log > bkup.log

REM set a variable _status with a default of ERRORS. If the process has a status of SUCCESS, the following commands will overwrite this variable. If not, it will report it as ERRORS in the email

SET _status=ERRORS

REM Loop through the earlier generated secondary logfile and find the string "Export terminated successfully without warnings" which is a standard Oracle output found in export logs

for /f "tokens=*" %%s in ('type bkup.log ^| find /i "Export terminated successfully without warnings"') do (SET _status=SUCCESS)

REM Processes to follow a SUCCESS which includes sending the export.dmp to TSM tape storage. Append the TSM results to the secondary logfile generated earlier.
if %_status%==SUCCESS (
cd \TSM\baclient\
dsmc archive -des="TSM Archive Name %_fdt%" -archmc=ARCH14D export.dmp >> bkup.log

Use a third party application, postie to email the results

c:\postie\postie -host:mailhost -to:sender@email.com -s:"SUCCESS:ExportDaily (%USERDOMAIN%) for %_fdt%" -nomsg -from:sender@email.com -file:bkup.log
)

REM Email as ERRORS if the above step fails

if %_status%==ERRORS c:\postie\postie -host:mailhost -to:recipient@email.com -s:"ERRORS:ExportDaily (%USERDOMAIN%) for %_fdt%" -nomsg -from:sender@email.com -file:D:\backup\obj_live\bkup.log

1 comments:

Email Database said...

Thanks for sharing this nice info. Its really a magnificent information.