Email us

Email us
Business I.T support Brighton from LJE Ltd.

Friday 18 July 2008

Perl Script to email on NT backup failure

Dear All,

Its handy to know when your auto backup product ( in this case Microsoft NT Backup) has failed on your Server or PC.

It maybe because disk space is low or data corruption, either way you want to know about it, so you can resolve the problem before you miss too many backups.

The perl script below checks a log directory for last updated file and looks for Error keywords and then emails out to admin@email.com

Download the Windows Sendmail ' bmail ' client app, this works great


#!C:\Perl\bin\perl.exe
# SEARCH-NEW-ERROR-EMAIL.PL
# list files in a directory and sort by Modified time
# check for 'Error' in last last modofied file and email if exisits
# ----help@infrasup.com March 2008


$dir = "C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Microsoft\\Windows NT\\NTBackup\\data\\";
opendir (DIR, $dir);
@dir=readdir(DIR);
closedir(DIR);

@dir = sort { -M "$dir/$a" <=> -M "$dir/$b" } (@dir);


print "@dir\n"; # print all array entries

$file=$dir[0]; # only print files not '.' or '..'
print "$file\n";


# convert unicode log files to ansi file for searching for string

print "$dir$file\n";
open (CONVERT, "|type \"C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Microsoft\\Windows NT\\NTBackup\\data\\$file\" > $file ") ;
sleep 5;
print "copied $dir$file to local $file \n";




sleep 2; # little pause to ensure files copied


# search for Error in file
#
#
#
#
open (STRINGS, "$file");
foreach $line ()
{

if ($line =~ m/Error(.*) ./)
{

#$found=1 ;
$description=$1;
print " found Error $description\n" ;
$found="errors found" ;
#print " hello" ; # add new line this is the last field
}

#print "$description" ; # print ; field delimiter for excel

}
print "$found\n";

## email results
if ($found eq "errors found") {

print "MAILED\n";
open (MAIL, "|\"c:/Program Files/bmail/bmail\" -s MAILSVR -t admin\@email.com -a \"Found Error in file backup file '$file' \" -f backups\@email.com -m $file ") || die "bmail failed:$!\n";
}



# note: del .log files in current script directory using a batch file



You can set this script to run every day using Windows Scheduler

Enjoy, Akbel

No comments: