Automate Disk Snapshots in Azure !

Abhimanyu Garg
Oct 27, 2017 · 4 min read
Image for post
Image for post

In Azure there are two ways to backup your Virtual Machines. First method is using the native Backup service which creates restore points of the VM periodically based on the policy selected by user. The restore points can then be used to either restore disks or the entire VM. Second method is creating VM disk snapshots which can be used to create a new VM.

We are going to discuss the second option i.e automating the process of creating disk snapshots using powershell.

Example scenario — Create daily snapshot of the Disks which have associated tag — Snapshot=True

This means, our powershell script will fetch the list of Disks present in a Subscription / ResourceGroup and then select the Disks having Tag — “Snapshot”= “True” and create the snapshots of selected Disks. In order to automate the script to run daily at a particular time, we are going to use the Azure Automation Runbook.

Steps

  • Add Snapshot=True tag to the Disks for which you want to automate the snapshot process.
Image for post
Image for post
  • Go to Azure Automation service and create an account. Once account is created, add a new RunBook of type Powershell.
Image for post
Image for post
  • Add the following Powershell script to the runbook-
$clientID = "<client id>"
$key = "<client secret>"
$SecurePassword = $key | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $clientID, $SecurePassword
Add-AzureRmAccount -Credential $cred -Tenant "<Tenant ID>" -ServicePrincipal;
$disks=Get-AzureRmDisk | Select Name,Tags,Id,Location,ResourceGroupName ;
foreach($disk in $disks) { foreach($tag in $disk.Tags) { if($tag.Snapshot -eq 'True') {$snapshotconfig = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $disk.Location -AccountType PremiumLRS;$SnapshotName=$disk.Name+(Get-Date -Format "yyyy-MM-dd");New-AzureRmSnapshot -Snapshot $snapshotconfig -SnapshotName $SnapshotName -ResourceGroupName $disk.ResourceGroupName }}}

Replace the following in script-

<client id> with Client ID of application which has atleast Contributor access to the Subscription.

<client secret> with Secret key of the application

<Tenant ID> with the Azure AD Tenant ID

If you prefer user credentials instead of application, then –

<client id> with user id /username of the Azure login account.

<client secret> with password of the Azure login account.

Remove -ServicePrincipal from the Add-AzureRmAccount command.

  • Publish the Runbook when all the changes are done. (You can test the script using Test pane option in runbook)
  • Once the Runbook is published, go back to the Azure Automation Account and select Schedules. Click on Add a Schedule.
Image for post
Image for post
  • Enter the details and select a schedule at which you want to run the script with Recurring option and click on create.
Image for post
Image for post
  • Now go back to the published Runbook and click on Schedules from left pane. Then click on Add a Schedule and select the newly created schedule. Click ok and you are done.
Image for post
Image for post
  • The Automation Service will create daily snapshots of the Disks having Tag (Snapshot=True). The Snapshot name will have date appended at the end so that you can keep track of when the snapshot was created.

Note- These snapshots hold good for encrypted disks as well. There is no need to configure Secret or Keys along with encrypted snapshots. In next post we have covered the process of restoring the encrypted disk snapshots to a fresh VM.

Do let us know about any issues you face while implementing Runbook.

For more details and other articles on Azure -

TechManyu Azure

TechManyu is a great platform to share your technical…

Abhimanyu Garg

Written by

Cloud and DevOps Professional with keen interest in System Design & Architecture. SRE | MultiCloud | Kubernetes (CKA) | Microservices | Automation | Ops

TechManyu

TechManyu is a great platform to share your technical knowledge and expertise to help millions of other developers and technical guys out there. Building an interactive community is our ultimate goal through which everyone can share and benefit at the same time. www.techmanyu.com

Abhimanyu Garg

Written by

Cloud and DevOps Professional with keen interest in System Design & Architecture. SRE | MultiCloud | Kubernetes (CKA) | Microservices | Automation | Ops

TechManyu

TechManyu is a great platform to share your technical knowledge and expertise to help millions of other developers and technical guys out there. Building an interactive community is our ultimate goal through which everyone can share and benefit at the same time. www.techmanyu.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store