Email Results of a Database Export

From NazimWIKI
Jump to navigation Jump to search

Had to write a batch script to check on the status of a nightly database export and email out the 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 Wiki for future reference.

This uses a 3rd party email application called "postie". The Script break-down is as follows:


@ECHO OFF

Enable local environments to be changed without affecting anything else:

SETLOCAL

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
 )
 )

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

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

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)

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 and use a third party application, in this case, Postie, to Email all these Results:

if %_status%==SUCCESS ( 
 cd \TSM\baclient\
 dsmc archive -des="TSM Archive Name %_fdt%" -archmc=ARCH14D export.dmp >> bkup.log
 
 c:\postie\postie -host:mailhost -to:sender@email.com -s:"SUCCESS:ExportDaily (%USERDOMAIN%) for %_fdt%" -nomsg -from:sender@email.com -file:bkup.log
) 

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\bkup.log