● Windows - NTBackup Emailed Backup Report

posted 1 Jan 2017, 16:05 by Dominique Cressatti   [ updated 22 Mar 2017, 14:26 ]

The following Perl script was created to send a daily email of the backup log generated on
a Windows server, saving you from of having to log on each server to check how the backup ran.

What you need to have and do:
  • Active Perl on your server
  • Win32::DriveInfo and Net::SMTP Perl modules
  • Set the $UserName variable to the account under which you will perform the backup
    and under 
    which directory the NTbackup  backup log will end up
  • Set the $recipient variable as the recipient who will receive the email
  • Set the$FromAddress variable as the email address of sender of this email
  • Set the $host variable as the name of the server to be monitored
  • Set the $MailServer variable as the FQN of your local mail server through which this email alter will come from
  • Schedule to run this script after the backup has ended using either with Windows AT or better the Windows scheduler

#!/usr/perl
#
use Net::SMTP;

$UserName = <"UserAccount">
$host = `hostname`;
$LogPath = 'c:/Documents\ and\ Settings/'.$UserName.'/Local\ Settings/Application\ Data/Microsoft/Windows\ NT/NTBackup/data/*.log';
$FoundError = 0;
$recipient = '<admin@snakeoil.com>';
$FromAddress =  '<admin@snakeoil.com>';
$MailServer =  '<mailserver.snakeoil.com>';

 print "###$LogPath\n";

foreach (glob($LogPath)) {
       $age = -M ;
       $age = int($age);

       if ($age == 0) 
       {
$LastLogFile = $_ ;
       }
}
# "backupXX.log" string
$LastLogFile =~ /backup\d*.log/;
# convert from unicode to ASCCI
system "type \"c:\\Documents and Settings\\'.$UserName.'\\Local Settings\\Application Data\\Microsoft\\Windows NT\\NTBackup\\data\\$&\" > \"c:\\Documents and Settings\\zzServiceUser\\Local Settings\\Application Data\\Microsoft\\Windows NT\\NTBackup\\data\\LastestBackupLog.log\"";
# open newly created ASCII file
$LastLogFile ="c:\\Documents and Settings\\'.$UserName.'\\Local Settings\\Application Data\\Microsoft\\Windows NT\\NTBackup\\data\\LastestBackupLog.log";

# print "lastlogfile= $LastLogFile \n";
open (FILE, $LastLogFile) or die "can't open file '$LastLogFile': $! "; 
@lines = <FILE>;

foreach $_ (@lines)
{
$_ =~ s/[\x00-\x1f]//g ;
# print "$_\n";

if ( $_ =~ /error/i || /warning/i|| /failed/i || /aborted/i || /skipped/i)
{
   $FoundError = 1;
   $String = $_;
   last
}
}

# print "FoundError:$FoundError\n";
close (FILE);

#print "@lines\n";
if ($FoundError == 1)
{
print "sending email error with backup\n";
$smtp = Net::SMTP->new('mailgate.lansa.co.uk');
$smtp->mail('administrator@lansa.co.uk');
$smtp->to('postmaster@lansa.co.uk');

$smtp->data();
    $smtp->datasend("to:$recipient\n");
$smtp->datasend("Subject: Backup problem on $host\n");
$smtp->datasend("\n");
$smtp->datasend("There is (are) error(s) or warning(s) in\n");
$smtp->datasend("the backup log of $host\n");
$smtp->datasend("$String\n");
$smtp->dataend();

$smtp->quit;
}

open (FILE, $LastLogFile) or die "can't open file '$LastLogFile': $! "; 
@lines = <FILE>;
#print "@lines\n";
print "sending email backup OK\n";
$smtp = Net::SMTP->new($MailServer);
$smtp->mail($FromAddress);
$smtp->to($recipient);

$smtp->data();
$smtp->datasend("to:$recipient\n");
$smtp->datasend("Subject: Backup on $host\n");
$smtp->datasend("\n");
$smtp->datasend("@lines\n");
$smtp->dataend();

$smtp->quit;

close (FILE);
system "del \"c:\\Documents and Settings\\'.$UserName.'\\Local Settings\\Application Data\\Microsoft\\Windows NT\\NTBackup\\data\\LastestBackupLog.log\""