ntbackup-logcheck

Description

Many administrators use a scheduled task with Windows ntbackup as a cheap and easy backup solution. One of the main drawbacks of this technique is that you are not notified if something goes wrong, i.e. that you have to manually check the log files from time to time.

ntbackup-logcheck is a small PowerShell script which counts the "Backup completed..." and "Verify completed..." lines in ntbackup's log files and sends an e-mail if the number is not what it should be. It automatically finds "today's" log file.

Prerequisites

Windows Powershell must be installed and configured to allow executing unsigned scripts locally (Set-ExecutionPolicy RemoteSigned).

Your ntbackup task must use verify (/V:yes) and summary logging (/f:s).

Usage

  1. Download the ntbackup-logcheck.ps1 and configure the constants at the start of the file.
  2. Tell Windows Task Scheduler to run "powershell <full-path>\ntbackup-logcheck.ps1" at some time after your ntbackup task has finished.

Localization

Since the language of the ntbackup log files depends on your system language, you need to tell the script what "Backup completed", "Verify completed" and "Error" is called in your system language (just have a look at the ntbackup log file). In addition, the log file location might be different on your system (it is also language-dependant). The version of ntbackup-logcheck you can download here is customized to a German version of Windows. If you are using a non-German version, you need to change the constants in the beginning of the script.

Download

ntbackup-logcheck.ps1.

Source Code

# Author: Heinrich Moser, www.moware.at, heinrich.moser.jun@moware.at
# Version: 1.0, 2007-11-26

# Checks if there is a file backup??.log with todays date in the
# specified directory. Checks if there are the specified number of
# "Sicherung abgeschlossen ..." and "Überprüfung
# abgeschlossen ..."-lines.

# Configure here
$LOGFILEDIR = "C:\Dokumente und Einstellungen\Administrator\Lokale Einstellungen\Anwendungsdaten\Microsoft\Windows NT\NTBackup\data"
$NUMBER_TASKS = 2 # number of backup tasks (example: Exchange and SystemState = 2)

$SMTPSERVER = "localhost"
$MAILFROM = "heinrich.moser.jun@moware.at.invalid"
$MAILTO = "heinrich.moser.jun@moware.at.invalid";
$MAILSUBJECT = "Error during ntbackup";

$BACKUPCOMPLETED = "Sicherung abgeschlossen"   # "Backup completed" line in the log file in your system language
$VERIFYCOMPLETED = "Überprüfung abgeschlossen" # "Verify completed" line in the log file in your system language
$ERROR = "Fehler"							   # "Error" in your system language

# Send mail and show message
function Alert {
	Write-Warning $args[0]
	$sc = new-object Net.Mail.SmtpClient -arg "$SMTPSERVER"
	$sc.Send($MAILFROM, $MAILTO, $MAILSUBJECT, $args[0])
	exit 1
}

Trap [Exception] {
	Alert $($_.Exception.GetType().FullName + " in ntbackup-logcheck: " + $_.Exception.Message)
}

# find file with todays date
$today = $null;
foreach ($file in get-childitem $LOGFILEDIR\backup??.log) {
	if ($file.LastWriteTime.Date -eq  [DateTime]::Now.Date) {
		$today = $file
		break
	}
}

if ($today -eq $null) {	Alert "No backup file with today's date found!" }

# open file and count lines
$iBackup = (Get-Content $today | Select-String -SimpleMatch "$BACKUPCOMPLETED" | Measure-Object -Line).Lines
$iVerify = (Get-Content $today | Select-String -SimpleMatch "$VERIFYCOMPLETED" | Measure-Object -Line).Lines
$iErrors = (Get-Content $today | Select-String -SimpleMatch "$ERROR" | Measure-Object -Line).Lines

if ($iErrors -gt 0) { Alert "Error found in logfile." }

if (($iBackup -ne $NUMBER_TASKS) -or ($iVerify -ne $NUMBER_TASKS)) {
	Alert "Incomplete backup. $NUMBER_TASKS entries expected, $iBackup/$iVerify found."
}

Write-Host "Everything is OK." # don't send a mail
  

Notes

If you change anything, you can send me the changes and I will put them on this page if I like them. ;-)

As Sebastian Haensch correctly pointed out to me, the script does not report an error if there are problems with the tape drive (instead, it only complains about missing "Verify completed..." lines). This could be improved by making additional checks for phrases such as "nicht ordungsgemäß" or "Vorgang wurde abgebrochen" when filling $iErrors.

License

You can use, modify and distribute (but not sell) this script freely. If you use it, feel free to drop me a line and tell me that it's been useful to you.


Last update: 2009-08-25
Contact/Impressum