# 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:

$ResourceGroupName = <resource group to use>$location = <location>
$dataDiskName = <name of the disk to create snapshot from>$snapshotName = <name of the snapshot>
$accountType = <either Standard_LRS or Premium_LRS>$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName$dataDiskName
$snapshot = New-AzureRmSnapshotConfig -SourceUri$disk.Id -CreateOption Copy -Location $location New-AzureRmSnapshot -Snapshot$snapshot -SnapshotName $snapshotName -ResourceGroupName$resourceGroupName -AccountType $accountType  ### 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 az snapshot create --name snapshotCLI --resource-group$resourceGroup –location "$location --sku "Standard_LRS" --source$managedDiskID


## 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.

$snapshot = Get-AzureRmSnapshot -ResourceGroupName$resourceGroupName -SnapshotName $snapshotName$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location$snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName$resourceGroupName -DiskName $osDiskName  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 $virtualMachineName = "<Virtual Machine Name>"
$virtualMachineSize = "Standard_DS3_v2"$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize$virtualMachineSize
$VirtualMachine = Set-AzureRmVMOSDisk -VM$VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach –Windows  After this point the rest of the process is the same as a new VM, configure the NIC, Public IP’s etc. $publicIp = New-AzureRmPublicIpAddress -Name $publicIPName -ResourceGroupName$resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName$resourceGroupName
$nic = New-AzureRmNetworkInterface -Name$nicName -ResourceGroupName $resourceGroupName -Location$snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId$publicIp.Id
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM$VirtualMachine -Id $nic.Id New-AzureRmVM -VM$VirtualMachine -ResourceGroupName $resourceGroupName -Location$snapshot.Location


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:

Add-AzureRmVMDataDisk -VM $vm -Name$dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1  ### 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)

az disk create --resource-group $resourceGroupName --name$diskName --sku $storageType --size-gb$diskSize --source $snapshotID  Then we go ahead and create a new VM with this managed disk as the OS disk: managedDiskId=$(az disk show --name $managedDiskName --resource-group$resourceGroupName --query [id] -o tsv) az vm create --name $virtualMachineName --resource-group$resourceGroupName --attach-os-disk $managedDiskId --os-type Windows  Attaching a data disk can then be done with the “disk attach” command: dataDiskID=$(az disk show --name $managedDiskName --resource-group$resourceGroupName --query [id] -o tsv) az vm disk attach –g myResourceGroupDisk –-vm-name myVM –-disk \$dataDiskID