Use Blob snapshots with Azure IaaS VMs?

Learn how to use snapshots with Azure IaaS VMs?

John Savill

June 15, 2016

2 Min Read
Use Blob snapshots with Azure IaaS VMs?

Q. How do I use blob snapshots with Azure IaaS VMs?

A. Azure VMs do not have a native checkpoint capability which enables a point-in-time view of a VM to be saved. Some solutions would include performing a VM Capture or a backup via Azure Backup. If you want to only save the disk state you can use blob snapshots which saves the state of the blob which will work for VHD files since they are stored in page blobs. If the VM is running this snapshot would be crash consistent or shutdown the VM first for a clean snapshot. The PowerShell below creates a snapshot of a specific blob 

$Container = 'vhds'$VHDName = 'DummyVM201656141318.vhd'$AzStorAccName = 'savtechsalrsscus'$AzResGroup = 'rg-scusa'$AzStrKey = Get-AzureRmStorageAccountKey -Name $AzStorAccName -ResourceGroupName $AzResGroup$AzStrCtx = New-AzureStorageContext $AzStorAccName -StorageAccountKey $AzStrKey[0].Value$VMblob = Get-AzureRMStorageAccount -Name savtechsalrsscus -ResourceGroupName rg-scusa | Get-AzureStorageContainer | where {$_.Name -eq $Container} | Get-AzureStorageBlob | where {$_.Name -eq $VHDName -and $_.ICloudBlob.IsSnapshot -ne $true}$VMsnap = $VMblob.ICloudBlob.CreateSnapshot()#View all snapshotsGet-AzureStorageBlob –Context $AzStrCtx -Container $Container | Where-Object {$_.ICloudBlob.IsSnapshot -and $_.SnapshotTime -ne $null } 

Multiple snapshots can be created and to view the most recent snapshot use:

$VMsnaps = Get-AzureStorageBlob –Context $AzStrCtx -Container $Container | Where-Object {$_.ICloudBlob.IsSnapshot -and $_.Name -eq $VHDName -and $_.SnapshotTime -ne $null } $VMsnaps[$VMsnaps.Count -1].ICloudBlob

The snapshots can then be copied to other blobs to be used for other VMs. The copy can be performed with Start-AzureStorageBlobCopy. You cannot restore a snapshot back to the original blob that contains a disk attached to a VM as the VM has an lease on the blob stopping it being overwritten. You would have to detach the disk from the VM first which is fine for a data disk but not possible for the OS disk. If you want to revert the OS disk to a snapshot you would have to delete the VM, restore the blob from the snapshot and then recreate the VM. For example to copy the latest snapshot for a blob to a new blob (note in this case I'm copying to same storage account however you could use a different storage context to copy to a different account):

$status = Start-AzureStorageBlobCopy -CloudBlob $VMsnaps[$VMsnaps.Count -1].ICloudBlob -Context $AzStrCtx -DestContext $AzStrCtx -DestContainer $Container -DestBlob 'newvm.vhd' -ConcurrentTaskCount 10$status | Get-AzureStorageBlobCopyState

 

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like