Windows Server Backup – Send Email When Complete
In the days of tape backup it was easy to tell when a backup had completed because the tape could be configured to eject from the drive. A lot of our clients use a pool of USB backup drives which are swapped at the beginning of each day. This causes an occasional problem when a client swaps their USB backup drive before the backup has completed – this can happen when a full server backup takes place on a server containing large amounts of data.
One such client asked if their server could send them an automated email when the backup had completed so they would know it was safe to change the backup media. Windows Server Backup cannot do this out of the box, so we configured two scheduled tasks to run a PowerShell script when a ‘successful’ or ‘failed’ backup event was written to the Windows Event Log.
Create a PowerShell script using Notepad and save it on the server as ‘EmailBackupResults.ps1’. You will need to amend the variables to fit your configuration:
# Set up variables $SmtpServer = "192.168.1.1" $SmtpPort = 25 $FromEmail = "admin-email-address" $ToEmail = "recipient-email-address" $OutputFile = "C:BackupEmailBackupResults.txt" # Create backup results file add-pssnapin windows.serverbackup $NextBackup = Get-Date (Get-WBSummary | select "NextBackupTime" | ft -hide | out-string) $LastBackup = (Get-Date $NextBackup) - (New-TimeSpan -day 1) Get-WinEvent -LogName "Microsoft-Windows-Backup" | Where {$_.timecreated -ge $LastBackup} | Sort-Object TimeCreated | Format-Table TimeCreated, Message -AutoSize -Wrap | Out-File $OutputFile # Construct and send email $SmtpClient = new-object system.net.mail.smtpClient $Msg = new-object Net.Mail.MailMessage $SmtpClient.host = $SmtpServer $SmtpClient.Port = $SmtpPort $computer = gc env:computername $Msg.From = $FromEmail $Msg.To.Add($ToEmail) $Msg.Subject = "## Backup Complete on " +$Computer +" ##" $Msg.Body = "The backup for " +$Computer+" has completed. Please see the attached file for backup results." $Msg.Attachments.Add($OutputFile) $SmtpClient.Send($Msg)
Next you will need to create a scheduled task which launches the PowerShell script you have just created. The ‘EmailBackupResults’ task will need two triggers configured as shown below – Event ID 4 is a successful backup, and Event ID 5 is a failed backup.
The Action for this task will start a program powershell.exe with the arguments -command “& C:BackupEmailEmailBackupResults.ps1”
You will need to change the path in the arguments to match the location you saved the script to.
Now, as soon as your backup has completed (with a success of failure) the email address you configured in the script will be sent an email confirming the backup has completed. A text file will also be attached to the email which contains the backup results.
You may also need to configure your mail server (in this case it was Exchange installed as part of a Small Business Server 2011) to accept anonymous emails from the IP address of the backup server.
Guest Post by Richard Goatly
Authentic IT – Web Design and IT Consultancy