RSS

Set up backups in Azure part 4 – Notifications

23 Sep

Ok, so now we have our backups, Windows Server Backup, Azure Backup and SQL backup. If you have used a normal SQL edition and not express you have probably used maintenance plans to notify when the jobs fail but if you use SQL express you need to do it in another way and that is what I’m going to show you.

The principle is that whenever a service fail it will generate a fail event in the event log. By listening to a certain type of event we can then invoke an action when the event occur such as sending an email. To listen to an event we will use Windows Task Scheduler and to send an email we will use Powershell.

First we need to find the fail event ID:s to Windows Server Backup, Azure Backup and SQL backup.

Windows Sever Backup Event ID:s

Here is the event id:s to backup operations in Windows Server Backup, the interesting ones are:

  • 4  –  Backup finished successfully
  • 19 – Backup attempted at ‘%4’ failed to start, error code ‘%5’
  • 564 – Backup started at ‘%1’ failed as network target ‘%2’ is not writeable. Please ensure user ‘%3’ has write permissions on the target.

Log: Microsoft-Windows-Backup/Operational

Source: Backup

Microsoft SQL Event ID:s

Here is the complete list of event id:s to SQL, the interesting ones are:

  • 3041  – BACKUP failed to complete the command %.*ls. Check the backup application log for detailed messages.
  • 18264 – Database backed up. Database: %s, creation date(time): %s(%s), pages dumped: %I64d, first LSN: %s, last LSN: %s, number of dump devices: %d, device information: (%s). This is an informational message only. No user action is required.

Log: Application

Source: MSSQLSERVER

Azure Backup Event ID:s

The list of event id:s to Azure Backup. The interesting ones are:

  • 120002 The backup operation failed
  • 170017 The backup failed as the total size of the data that has been backed up has exceeded the quota limit for your organization
  • 120001 The backup operation was performed successfully

Log: CloudBackup/Operational

Source: CloudBackup

Windows Task Scheduler

Now we have a bunch of event id:s that we can listen to. To listen after these events we will use the Windows Task Scheduler . Open up Windows Task Scheduler and choose Create Basic Task. Fill in the event you want to listen to.

Task scheduler

Press next and choose to start when a specific event is logged.

Task scheduler_event

Specify which source and event you want to listen to. When use

Task scheduler_eventsource

In the next step you will choose action. In Windows Server 2012 the e-mail functionality is deprecated but don’t sweat, we are not going to use it anyway. Instead we are going to use windows powershell to send mail.

Task scheduler action

Here we fill in that the task should start powershell.exe and add the following parameters: -executionpolicy remotesigned -File  C:\BackupNotifier\SystemBackup.ps1

Task scheduler powershell action

Press next and then finish. As a final touch we right click on the new task and choose properties. Change so the task will be run even if we’re not logged in. Also change Hidden to Yes.

Task Scheduler properties

The next step will be create our powershell script.

Send Mail With Powershell

if you haven’t gotten your own SMTP server I recommend to get that before moving on to the next steps.

Create the folder C:\BackupNotifier\

The idea is to launch a powershell script that sends an email with our SMTP server with a given text and subject. But we don’t want to store the password to the SMTP server in the script. The solution is to convert the password to a non-readable password and add it to the script.

Open up powershell and type: read-host -assecurestring | convertfrom-securestring | out-file C:\BackupNotifier\SecureStr.txt
Press enter and write you password to your smtp account and press enter again.

Powershell_Secure_string

Now we have solved our password problem by creating a non-readable version of our password that we can use as input to our next script.

To send the mail we will use the powershell command send-mailmessage.

Create the script file by creating a text document and rename it to SystemBackup.ps1 . Then right click on file and choose edit. Paste the following code and change SERVERNAME to the actual servername were you configure this setup. The idea is know which server that has generated the mail when you read it 🙂
Also change SMTPUser to your real SMTP user account and the to/from email address.

$pass = cat C:\BackupNotifier\SecureStr.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "SMTPUser",$pass
send-mailmessage -from "SERVERNAME Backup <support@company.se>" -to "Support <support@company.se>" -subject "The Windows Server Backup has succeded" -body "The server SERVERNAME has had a successful Windows Server Backup" -priority High -smtpServer smtp.sendgrid.net -Credential $mycred

Save and exit.

To test our new mail function you can create a batch file that launches the script.
Paste this into a textfile and save it as Testmail.bat.

Powershell.exe -executionpolicy remotesigned -File C:\BackupNotifier\Systembackup.ps1
Pause

After that double-click the file. If everything goes well you will soon receive a mail in your inbox.

This will be the end in my blog series about setting up backups in azure. But my intention is to make a better version of this series, a Set Up Backups In Azure 2.0 were almost everything is automated with powershell. Wouldn’t it be real nice to set up some parameters and then launch a script that does most of the tasks with setting up SQL backups, Windows Server Backups and Azure backup and it is almost completely automated? Not to mention a huge time saver! Hopefully someone is stupid enough to publish the code so you won’t have to create it yourself 😉   Lets hope about a bright future soon 🙂

Advertisements
 
Leave a comment

Posted by on 2014-09-23 in Okategoriserade

 

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Mark Brummel Blog | Microsoft Dynamics NAV

Master Classes Design Patterns and more...

Vjeko.com

ideas in the cloud

Goprowe AB

Reflections from the ERP field

ArcherPoint Developer Blog

Reflections from the ERP field

The NAV Viking´s Blog

- Sharing my experience in the Dynamics NAV world

Comments for waldo's blog

Reflections from the ERP field

Nav Can Be Fun

Its Really Fun to Work with Microsoft Dynamics Nav

Navision-Girl.Com

Reflections from the ERP field

LET'S TALK NAV

Tips, Tricks, How-To's & News about Microsoft Dynamics NAV & Dynamics 365 for Financials

Kine's Info

Reflections from the ERP field

blog.wibeck.org/

Just another WordPress.com site

NAV/SQL Performance - My Two Cents

Reflections from the ERP field

Hougaard.com – Applied Hacking

Reflections from the ERP field

Kauffmann @ Dynamics NAV

Reflections from the ERP field

Kriki’s Dynamics NAV blog

Reflections from the ERP field

Rashed Amini

Reflections from the ERP field

Olof Simren - Microsoft Dynamics NAV Blog

My blog about Microsoft Dynamic NAV tips and tricks. Remember to Subscribe!

Van Vugt's dynamiXs

Reflections from the ERP field

Navisionary – Dynamics NAV

Reflections from the ERP field

%d bloggers like this: