Discover Managed Disk Snapshots

Managed disks brought  a number of benefits to Azure VM’s over thee previous method of manually managing storage accounts used for VM disks. One of these features that is often overlooked or misunderstood is snapshots. In this article we’ll explore what benefits snapshots give you, and how to use them.

Before we delve into how to use snapshots, there are some key points to be aware of when using them:

  1. Azure Managed Disk snapshots are use to create a point in time copy of an existing disk that can then be used to create a new VM. They are not like snapshots taken in something like VMWare Workstation, which can be used to easily restore a VM to a previous state. You can achieve this with managed disk snapshots, but it takes more work.
  2. You are charged for the storage of each snapshot, however snapshots only use as much storage as your VM disks are using, not the size of the managed disk. For example if you have a 128GB managed disk but are only usign 50GB, you will be charged for the full 128GB for the disk, but only 50GB for the snapshot
  3. Snapshots are not incremental, each time you take a snapshot you are creating a full new copy of the managed disk and are charged appropriately
  4. Snapshots are not consistent across disks. If you are using multiple disks in a RAID array or Storage Pool there is no guarantee that the snapshot will occur at exactly the same time on each disk and so can cause inconsistencies. In this scenario you are best turning off the machine before snapshotting it.
  5. You need to create a managed disk from a snapshot before it can be used with a VM. This does mean you can use a snapshot to create multiple VMs
  6. Snapshots are not generalised and so can’t be used as a generic image

 

So given these points, what uses are there for managed disk snapshots:

  • Restoring a VM to an earlier version by creating a new VM from the snapshot and deleting the existing VM
  • Backing up a VM before making a major change
  • Making a copy of a VM for troubleshooting or forensic investigation
  • Copying a VM to another region for Disaster Recovery
  • As a custom backup mechanism

If these operations are something you are doing regularly then you may want to look at tools like Azure Backup and Site Recovery (and in fact these may rely on snapshots), but for a one off operation snapshots can be very useful.

Creating a Snapshot

Creating a snapshot can be done through the portal, PowerShell or CLI:

Portal

  1. Browse to the managed disk you want to snapshot and select it
  2. On the top menu click the “create snapshot” button
  3. In the window that opens provide a name for the snapshot and the resource group to store it in. Also select the account type- this is either standard or premium and should be select based on the performance requirements of the VM you intend to create using this disk. You cannot change this later.
  4. Click create. Once the operation completes the snapshot will be created in the resource group selected

 

PowerShell

The following PowerShell will create a snapshot in the same resource group as the disk:

CLI

The command to create a snapshot with the CLI is “AZ Snapshot Create” , which requires parameters for:

  • Name of the snapshot to create
  • Resource group to use
  • Location
  • SKU (Premium_LRS or Standard_LRS)
  • Source – ID of the managed disk to snapshot

 

 

Using a Snapshot

Using a snapshot to create a VM needs to be done through PowerShell or CLI, there’s no experience available through the portal.

PowerShell

Before you can create a VM you first need to create a managed disk from the snapshot (this way you can create multiple disks from the same snapshot). The code below uses the same variables as above and will create a managed disk in the same location as the snapshot.

Once we have created the managed disk we can then go ahead and create a VM that is using this as it’s OS disk. This is pretty similar to the commands to create a normal VM, the important change is that we tell the commands to attached the existing managed disk and we tell it what OS is on the disk

After this point the rest of the process is the same as a new VM, configure the NIC, Public IP’s etc.

If you have any data disks you want to restore from snapshots you would create the managed disk as above, and then use the Add-AzureRMVMDataDisk command to attach it to your vm:

CLI

Using the CLI we will follow the same process, first create the managed disk from the snapshot

snapshotId=$(az snapshot show –name $snapshotName –resource-group $resourceGroupName –query [id] -o tsv)

Then we go ahead and create a new VM with this managed disk as the OS disk:

Attaching a data disk can then be done with the “disk attach” command: