Emailing The NTBackup Log With VBScript


Home > Misc > Emailing The NTBackup Log With VBScript Buy Me A Coffee

Introduction

While it has its quirks and limitations, NTBackup is a fairly decent backup application for Windows NT4 all the way up to Windows Server 2008 in the home and small business environment. It supports tape and disk based backups, bare metal disaster recovery and if you know the command line switches, can be fairly powerful when combined with the Windows task scheduler.

Of course if you are using the task scheduler to schedule multiple backup jobs, say in the middle of the night, perhaps across many different systems, you need to be vigilant in checking out the log files to make sure those jobs are running. Since NTBackup buries the logs deep in the Application Data directory of the user account under which the job runs, this typically involves either sharing that directory or RDP'ing into the machine and manually checking the log. NTBackup also keeps the log in 10 rolling text files named Backup01.log to Backup10.log. This makes it a little annoying as every time your job runs, the result will be in a different file.

To mitigate this annoyance, I wrote a simple VBScript which will automatically find the most recent NTBackup log file, and email it to a specified address. That way, every morning I have in my inbox all the logs I need to check and it is easy to spot any issues which may require attention.

The simple script is presented below. It relies on the free AspEmail ActiveX component to send mail, and you will need to have an accessible SMTP server.

MailBackupLog.VBS

Option Explicit  
Dim fso, File, RecentFile, LogFilePath, LogFile, LogFileContents, Mail

LogFilePath = "C:\Documents and Settings\(BACKUP USER PROFILE DIRECTORY)\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\"

Set fso = CreateObject("Scripting.FileSystemObject")

Set RecentFile = Nothing

For Each File in fso.GetFolder(LogFilePath).Files
  If (RecentFile is Nothing) And (fso.GetExtensionName(LogFilePath & File.Name)) = "log" Then
    Set RecentFile = File
  ElseIf (File.DateLastModified > RecentFile.DateLastModified) And (fso.GetExtensionName(LogFilePath & File.Name)) = "log" Then
    Set RecentFile = File
  End If
Next

'note the parameters. First one is forreading, 2nd is to not create the file if it doesn't exist, 3rd is to open as unicode
Set LogFile = fso.OpenTextFile (LogFilePath & RecentFile.Name, 1,False,-1)

Do Until LogFile.AtEndOfStream
    LogFileContents = LogFileContents & LogFile.ReadLine & vbCRLF
Loop

LogFile.Close


Set Mail = CreateObject("Persits.MailSender")

Mail.Host="(SMTP SERVER)"
Mail.Port="25"
Mail.From="(SENDER'S ADDRESS)"
Mail.FromName="(SENDING SYSTEM) Backup Log"
Mail.Subject="(SENDING SYSTEM) Backup" & " " & Now
Mail.AddAddress("(RECIPIENT ADDRESS)")

Mail.Body=LogFileContents

Mail.Send
Set Mail=Nothing

Using The Script

  1. Download AspEmail and install if you have not already done so.
  2. Download this script and unzip or copy and paste it into a text file then save as .VBS.
  3. Place this script in a directory with permissions accessible to the user account you will run it under. I typically place it in the NTBackup data directory with the log files.
  4. Replace (BACKUP USER PROFILE DIRECTORY) in LogFilePath with the profile directory of the user name you run your NTBackup job under. Windows versions newer than 2003 use c:\Users instead of c:\Documents and Settings, so adjust your path accordingly.
  5. Replace (SMTP SERVER) with your SMTP server (IP address or FQDN), change the port if necessary, replace (SENDER'S ADDRESS) with the address the system will send under (typically a generic notification address).
  6. Figure out some description for the backup log subject line (I typically use the system name, customer name, etc.) and replace (SENDING SYSTEM) with it.
  7. Replace (RECIPIENT ADDRESS) with the address you want the message sent to.
  8. Create a scheduled task to run this script some amount of time after the backup job. I typically pick two hours to give a decent buffer but it depends on the size of the job. If you have a 20 minute job, run it 20 minutes after the job, for example. Run the script under the same user you run the NTBackup job under.
  9. Test by running the script from Task Scheduler by right clicking and selecting "Run now". You should receive no errors and depending on the delay of your mail system, you should receive an email moments later.

Note that most well behaved mail servers these days require SMTP authentication to send messages to outside domains. If you receive errors from AspMail (particularly a "550 Relaying Denied") you may need to modify the script to authenticate to the server with a known user name/password. See the AspEmail security documentation on how to use the Username and Password properties of the control to send authentication info to your server.

Using NTBackup In Windows XP Home, Windows Vista, Windows Server 2008

NTBackup is not installed by default in Windows XP Home. To install it, follow these instructions: How to install NTBACKUP on Windows XP Home.

While NTBackup is not included with Windows Vista or Windows 2008 Server, it can be installed by following these instructions: How to install NTBackup on Server 2008. Note that you will need to turn on (Vista) or install (Server 2008) the Removable Storage Management service as this is not done by default in these versions of Windows.

Back To Misc Page | Mail Me | Search | Buy Me A Coffee