Example PowerShell to demo blob snapshots with Azure IaaS VMs

See and use a demo of blob snapshots with Azure IaaS VMs.

John Savill

June 15, 2016

2 Min Read
Example PowerShell to demo blob snapshots with Azure IaaS VMs

Q. Is there example PowerShell showing taking a snapshot of a data disk for an Azure VM then applying the snapshot?

A. Below is a fairly large PowerShell script that demonstrates using snapshots with a data disk that has been added to a VM. You can change the various variables at the start of the script for your own VM name, resource group and data disk name. Note also the comments where I talk about going inside the VM to make some changes. Note through this whole demo the VM can stay running as data disks can be hot-added and removed from a VM. I walk through this demo in a video at to help better understand this at https://youtu.be/WP7-96KQJl0.

#Test with a data disk that has been added to the VM$AzStorAccName = 'savtechsalrsscus' #Storage account name$AzResGroup = 'rg-scusa' #resource group name$AzStrAct = Get-AzureRmStorageAccount -Name $AzStorAccName -ResourceGroupName $AzResGroup$AzStrKey = Get-AzureRmStorageAccountKey -Name $AzStorAccName -ResourceGroupName $AzResGroup$AzStrCtx = New-AzureStorageContext $AzStorAccName -StorageAccountKey $AzStrKey[0].Value $Container = 'vhds' $VHDName = 'DummyVM-Data1.vhd'$VHDNameShort = 'DummyVM-Data1'$VMName = 'DummyVM'$VMblob = Get-AzureRMStorageAccount -Name $AzStorAccName -ResourceGroupName $AzResGroup | Get-AzureStorageContainer | where {$_.Name -eq $Container} | Get-AzureStorageBlob | where {$_.Name -eq $VHDName -and $_.ICloudBlob.IsSnapshot -ne $true}#Inside the VM partition and format the VHD to create drive letter so its empty$VMsnap = $VMblob.ICloudBlob.CreateSnapshot() #Empty disk snapshot which will be snapshot [0]#View all snapshotsGet-AzureStorageBlob –Context $AzStrCtx -Container $Container | Where-Object {$_.ICloudBlob.IsSnapshot -and $_.SnapshotTime -ne $null } #Now copy some data to the data disk then...$VMsnap = $VMblob.ICloudBlob.CreateSnapshot() #Snapshot with data on the disk which will be snapshot [1]#Save array of all snapshots$VMsnaps = Get-AzureStorageBlob –Context $AzStrCtx -Container $Container | Where-Object {$_.ICloudBlob.IsSnapshot -and $_.Name -eq $VHDName -and $_.SnapshotTime -ne $null } #Detach the disk from the VM can be done while VM is running and will break the lease enabling the blob to be overwritten$VM = Get-AzureRmVM -ResourceGroupName $AzResGroup -VMName $VMNameRemove-AzureRmVMDataDisk -VM $VM -Name $VHDNameShortUpdate-AzureRmVM -ResourceGroupName $AzResGroup -VM $VM #Restore the first via a copy which should be empty#Copy snapshot to blob$status = Start-AzureStorageBlobCopy -CloudBlob $VMsnaps[0].ICloudBlob -Context $AzStrCtx -DestContext $AzStrCtx -DestContainer $Container -DestBlob $VHDName -ConcurrentTaskCount 10 -Force$status | Get-AzureStorageBlobCopyState#Attach disk back to VMAdd-AzureRmVMDataDisk -VM $VM -Name $VHDNameShort -Caching None -CreateOption Attach -DiskSizeInGB 1023 -VhdUri $VMblob.ICloudBlob.Uri.AbsoluteUriUpdate-AzureRmVM -ResourceGroupName $AzResGroup -VM $VM #Look inside and note all the content has gone. now we'll restore back to the latest snapshot with the data on it againRemove-AzureRmVMDataDisk -VM $VM -Name $VHDNameShortUpdate-AzureRmVM -ResourceGroupName $AzResGroup -VM $VM #Restore the first via a copy which should be empty#Copy snapshot to blob$status = Start-AzureStorageBlobCopy -CloudBlob $VMsnaps[$VMsnaps.Count - 1].ICloudBlob -Context $AzStrCtx -DestContext $AzStrCtx -DestContainer $Container -DestBlob $VHDName -ConcurrentTaskCount 10 -Force$status | Get-AzureStorageBlobCopyState#Attach disk back to VMAdd-AzureRmVMDataDisk -VM $VM -Name $VHDNameShort -Caching None -CreateOption Attach -DiskSizeInGB 1023 -VhdUri $VMblob.ICloudBlob.Uri.AbsoluteUriUpdate-AzureRmVM -ResourceGroupName $AzResGroup -VM $VM #Data is all back 

 

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