Friday, April 3, 2009

Adding email notifications to ntbackup

ntbackup is the free, Windows-based, backup utility provided by Microsoft. It is a very helpful tool for backing up and restoring files on a Windows computer. You can download it for free from if you don't have it, but unless your computer is older, it should be included in your original Windows installation. This article focuses mainly on Windows Server 2003 and Windows XP.

One of the features missing in ntbackup is email notifications. Maybe you don't need them, but I would guess that like myself, you want to at least know whether your backup job finished. There's a way to get around this though, and that is by scripting. You can use ntbackup from the command line, and then add an additional command to use a simple smtp mail program to send an email. This actually isn't all that difficult, as long as you already have an SMTP host you're allowed to send mail through. Here's how:

First you need to create a batch file. You can use VB Script if you'd like, but I think batch files are easier to work with for more people. The reason for using a script file is because you can use it to perform multiple commands, and also can schedule it to run using Windows scheduler. You can create this file simply by creating a new text document in Notepad, then using File->Save As to save it as a .bat file. You'll have to change the Save As Type to All Files in order for this to work, otherwise Notepad will assign the default .txt extension to your file. For now that's all you need to do, as you'll add the commands to the file later in the next steps.

Now you need to know how to run ntbackup from the command line. A good reference for this is MS KB 814583. The main thing to know here is that you can select what to backup two different ways: in the command itself, or with a saved backup selection file (.bks). I suggest using the backup selection file, since you can use the ntbackup GUI to create it. To do this, check out my other post about saving a selection list for ntbackup.

For an example, lets say I want to back up the entire Documents and Settings folder to a location on a drive mapped as Z, and created a .bks file with that information in it called BackupDocs.bks. I saved my .bks file right on the C drive, so it's at "C:\BackupDocs.bks".  I can use the command line to invoke the backup by using the command:

ntbackup backup "@C:\BackupDocs.bks" /J "BackupJobName" /F "Z:\BackupFolder\BackupFile.bkf"

In the example, /J assigns a name to the backup job, and /F lets you specify a file location for your backup file. Consult KB 814583 if you want to find out more about ntbackup from the command line and the various switches you can use.

Ok, now that you know how to use ntbackup from the command line you're ready to create your .bat file. You can either go into Notepad and open it from there, or right-click on it and choose Edit. Either way it'll open in Notepad. On the first line, add you ntbackup command and save. Now if you were to double-click on your .bat file, it would execute that command. You can add multiple ntbackup commands if you'd like, or any other command line commands you'd like to perform before or after the backup. One command you're going to want to add to the end is for your email, and you'll have to use a command line SMTP program.

If you just want a notification saying the job completed, a free SMTP program you could use is called bmail. It's free to download, and does the job. Clicking that link will bring you to a download page, and also contains more information on the command line switches you'll need in order to get it to work. If you want to include the backup log files in the email, you'll have to do some searching to find a command line utility for that. I ended up writing my own C#-based Windows application just for my purposes. If you're interested in that rather than finding one somewhere else, leave me a comment and I can get it to you. Once you've added your SMTP notification command to the end of your batch file, you can save and close. The only thing left is to use Windows scheduler to schedule your batch file to run.

You can find the Windows scheduler by going to Start->All Programs->Accessories->System Tools->Scheduled Tasks. When asked what you want to schedule, use the Browse button to go out and select your .bat batch file that you created above. This way your backup and notification will run according to whatever schedule you set.

That's it. This turned into a longer post than I originally intended, but hopefully it covers the topic enough to help you get those notification going. If you run into problems, or have more questions, leave me a comment.

No comments: