Friday 26 October 2012

How to Set Up Windows Server 2008 Backup Notification Emails

This is for the benefit of anyone who might find themselves needing some pointers when setting up the backup on Windows Server 2008 or, more likely, email notifications from the server.
We have a new customer who recently invested in a new database/CRM and needed to ensure their backups were working correctly.
They are running Windows Server 2008 with minimal roles installed, no exchange and simple file sharing.
The Windows backup was set up to backup to either of two portable USB drives which are usually rotated every night with one being stored off site.
Unfortunately, only one of the drives had ever been configured for use by the backup and the second one had been failing for around a month other reasons, however nobody had been shown how to access the server to check and their critical data remained unprotected.
And so to work.
Firstly, we repaired the backup and set it up to use the second drive as well as the first by going into ‘Windows Server Backup’ and making a change to ‘Backup Schedule’ in the right pane.
Leave the selection on ‘Modify Backup’ on the first window and work your way through to ‘Add or remove backup disks’ where you can select ‘Add more disks’.
Don’t forget that disks for Windows Server Backup on 2008 need to be dedicated – you can’t use them for any other storage and they won’t show up with a drive letter in ‘Computer’.
Another useful point is the quick removal setting.
If you go to ‘Device Manager’ and select your USB device -> Properties, and click the ‘Policies’ tab.
If ‘Optimize for quick removal’ is selected, as is explained next to the radio button, the device can be removed without having to log in and go through the ‘Safely remove hardware’ process before changing USB drives – something of a timesaver.
OK, now the notifications.
In the Windows Server Backup, there is no built in method of notifying any user of the success or failure of the backup.
There is a slightly sneakier way to work around this by attaching a task to certain events in the Event Log.
There is an option to set the task to send an email when these events appear in the log, however there is no way to apply any SMTP login details to this task which makes it pretty difficult to get to work.
The way I found to get around this is to use a third party stand-alone emailing program utilised by a batch file which is activated on certain events appearing in the Event Log.
My solution……
Firstly, download the free sendemail client from the creator’s website:http://caspian.dotconf.net/menu/Software/SendEmail/
There are a couple of zip files you can download depending on your requirements, I needed the one with TLS support.
Extract the contents of this zip file to an easy to find folder on your server – for this example, to C:\Sendemail
Create two batch files to use with the Event Log tasks – one for failure and one for success.
In order to make it look good, I added two users to an SMTP server,
Backup.Success@domain-name.co.uk and Backup.Failed@domain-name.co.uk , although you can just use backup-notification@your-domain.co.uk for simplicity.
To create your batch files, open notepad and save the empty file with ‘File->Save as’ into C:\Sendemail by changing the ‘Save as type’ dropdown option to ‘All Files’ and naming the files with the .bat extension.
Name one file ‘backupsuccess.bat’ (don’t miss the .bat part) and then do the same again and save the second file as ‘backupfailure.bat’
Now you have the two batch files in the Sendemail folder, right click on ‘backupsuccess.bat’ and select ‘Edit’.
Now copy and paste the following details into it and replace anything Bold with your own data. There should only be 3 lines in each of these files, everything after Sendemail should be on one line:

REM Uses sendemail.exe to send an email from (-f) to the recipient (-t) when an event 4 (Backup Successful) appears in the event log.
cd c:\Sendemail
sendemail -f Backup.Success@domain-name.co.uk -t recipient@ofnotification.co.uk -u "Backup Successful" -m "The backup finished successfully, please change the portable USB drive in readiness for tonight's backup." -s SMTPserver.Hostname.co.uk -xu backup.success@domain-name.co.uk -xp password

Close down this file and save it, then right click on the ‘backupfailure.bat’ and do the same again with the following data:

REM Uses sendemail.exe to send an email from (-f) to the recipient (-t) when an event 19 or 49 (Backup Failures) appears in the event log.
cd c:\Sendemail
 sendemail -f Backup.Failure@domain-name.co.uk -t recipient@ofnotification.co.uk -u "Backup Failure Alert!" -m "Warning! Last night's backup failed. Please contact SupportCompany on Telephone Number to try to resolve this issue in readiness for tonight's backup. Depending on the cause of failure, you may receive this notification twice." -s SMTPserver.Hostname.co.uk -xu Backup.Failure@domain-name.co.uk -xp password

Again, close and save changes to this file.
Now your batch files are ready you can test them to make sure you’ve got it right by running them, but before you do, you may want to substitute recipient@ofnotification.co.uk with your own email address for the purpose of this.
You can also add a new line at the bottom of the batch file which simply says:
pause
This will keep the command window open after it has run until a key is pressed so you can see if there are any problems with the batch file but remember to remove this line and change the email address back after you’ve finished testing and troubleshooting.
The last step is to attach the tasks to the relevant events.
There are two ways to do this, through the Event Log (which requires events 4, 19 and 49 to already be logged), or the following:
Open Start->Administrative Tools->Task Scheduler.
In the right pane, select ‘Create Basic Task’ and give it a name – I suggest ‘Backup_Success_Event_4’ and click Next.
Under the ‘Trigger’ options, select the bottom option ‘When a specific event is logged’ and click Next again.
On the next screen, under Log, select ‘Microsoft-Windows-Backup/Operational’, under Source, select ‘Backup’ and under EventID,  enter the number 4, and click Next.
On the Action screen, ensure that ‘Start a program’ is selected and click Next.
On this screen, use the Browse button to select C:\Sendemail\backupsuccess.bat, click Open, and then click Next.
Note, on the Finish screen, you will need to ensure you tick the box next to ‘Open the Properties dialog for this task when I click Finish’, and click Finish.
You now need to select ‘Run whether user is logged on or not’ and ‘Run with highest privileges’ and click OK.
That’s it for the Successful Backup Notification.
In order to do the same for failures, you need to go through the process above another two times.
When naming the task, use the names ‘Backup_Failure_Event_19’ and ‘Backup_Failure_Event_49’, and instead of applying to EventID 4, apply them to EventID numbers 19 and 49 respectively.
This time, you will browse to C:\Sendemail\backupfailure.bat to apply to these two tasks.
Now hopefully, you will get a notification of failure or success as soon as your Windows Server Backup completes.

1 comment:

  1. This works very well, thank you so much for your post.

    ReplyDelete