May 18 2017 04:40 PM
Hi All,
Looking for some assistance in reverting a Managed disk snapshot to an OS disk of the VM. Idealy i would like to do the following:
1. Detach existing OS managed disk from VM
2. Convert the snapshot to a managed disk
3. Attach new managed disk to VM
I'm struggling at point 1. The Remove-AzureRMDisk cmdlet doesnt like the fact its currently attached.
The current way im achieving this is by deleting / recreating the VM with the new disk. Seems to work well but is getting complicated when more services are setup against this VM.
Any suggestions?
May 19 2017 02:28 AM
May 22 2017 12:03 AM
Hi Sunit,
Actually no. I'm attempting to restore the snapshot in a similar way you would apply a Hyper-V checkpoint. I would prefer not to delete and recreate the Azure VM from the snapshot. Instead i was wondering if there is a way i can convert the snapshot to a disk and attach it back to the existing VM....
May 22 2017 07:29 AM - edited May 22 2017 07:33 AM
Hi Tom,
in this scenario use backup feature to take Snapshot backup you may have to pay some more money for backup Feature.
Check this URL for more details
https://docs.microsoft.com/en-us/azure/backup/backup-azure-vms
Best
Sunit Patil
Skype: Sunitonline
May 22 2017 08:34 PM
Thats what i love about azure, so many ways to achieve the same thing.
Thanks Sunit, i think Azure Backup is a bit excessive for my needs. The VM;s in question are all DEV boxes, we need a quick procedure to take a point in time backup pre-deployment. This would also serve as a fast restore point if required.
What we are currently doing by using managed disks with snapshot capabilities, then recreating a disk from the snapshot and then re-creating the VM from the new disk is serving its purpose. I think we will stick with this approach for now as to not introduce additional costs. Its actually a fast process when the powershell scripts are all in order too.
Mar 28 2018 07:30 AM - edited Mar 28 2018 07:34 AM
I came here looking for the exact same solution to the original poster's question:
"Actually no. I'm attempting to restore the snapshot in a similar way you would apply a Hyper-V checkpoint. I would prefer not to delete and recreate the Azure VM from the snapshot. Instead i was wondering if there is a way i can convert the snapshot to a disk and attach it back to the existing VM...."
Left disappointed.
Why does Microsoft make it easy to create a snapshot of disks in the Azure portal, but not provide any obvious way to revert to these snapshots?
May 11 2018 01:29 AM
May 23 2018 08:41 AM
Hi Tom. I have a similar problem and I came across your message. I think that the proper thing to do would be to first stop/deallocate the VM. Then you should be allowed to detach the old OS disk from its configuration and attach the new OS disk to it (the one, that you have created from the snapshot) and startup the VM. I have not had time to test this yet since my problem is more complicated than yours (my VM is a rather "fat" SQL Server instance with two -2- data disks attached to it in addition to its OS disk) - I hope that I will be able to test it some time later - please, let me know of your findings if you use this approach.
May 27 2018 05:53 AM
Jul 10 2018 08:42 AM
Jul 22 2018 07:37 PM
Hi Can you please send me the script too.
Sep 13 2018 01:04 PM
Yes, I've seen it. However, in order to revert back to the VM's original state, you must delete the original VM, then create a new VM from the snapshot by running a PS script and manually entering many required parameters.
This process seems awfully involved, even risky and prone for mistakes.
What we're looking for is something similar to VMWare where you can take a quick snapshot just before making changes to the VM (e.g. upgrade the OS/apps, make config changes, etc). If something fails, you can easily revert back to the VM's original Snapshot state by simple clicking a button.
Thanks for considering.
Sep 18 2018 05:37 PM - edited Sep 18 2018 05:38 PM
First off - sorry for the late response. I've had a lot of requests for this script so hopefully it helps.
Here's the scripts we use. We do it slightly differently to the Microsoft docs by retaining the original components, nic / pip etc. Its also tailored to allow for one data disk as an automatic restore. You can amend as suits.
I'm sure this could be refined more but it does what we need it to.
#Update Variables
$location = "Location" #Update if required
$resourceGroupName = "ResourceGroupName" #Update if required
$vmName = "VMName" #Update VMName
$diagStorageAcc = "DiagStorageAcct" #Update if required
$snapshotName = "OSDiskSnapshotNAme" #Update OS Disk SnapshotName
$DataSnapshotName = "DataDiskSnapshotName" #Update Data Disk SnapshotName
$vmSize = 'VMSize' #Update if required
#Get Target VM
$vm = Get-AzureRMVM -ResourceGroupName $resourceGroupName -Name $vmName
#Shutdown the target VM
Stop-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName -Force
#Get existing NIC to use during re-create
$NicID = $vm.NetworkProfile.NetworkInterfaces.Id
$VmId = $vm.VmId #Gets and sets VMId - Used to remove the diagnostics storage account.
#Get Boot Diagnostic storage container to be deleted
Set-AzureRmCurrentStorageAccount –ResourceGroupName $resourceGroupName –StorageAccountName $diagStorageAcc
$diagContainer = Get-AzureStorageContainer | Where-Object { $_.Name -like "*$VmId" }
#Get Managed OS Disk if Exists
$ManagedDiskName = $vm.StorageProfile.OsDisk.Name
$ManagedDisk = Get-AzureRMDisk -ResourceGroupName $resourceGroupName -DiskName $ManagedDiskName
#Get Managed Data Disk if Exists
$ManagedDataDiskName = $vm.StorageProfile.DataDisks.Name
If ($ManagedDataDiskName) {
$ManagedDataDisk = Get-AzureRMDisk -ResourceGroupName $resourceGroupName -DiskName $ManagedDataDiskName
}
#Remove VM
Remove-AzureRMVM -ResourceGroupName $resourceGroupName -Name $vmName -Force
#Remove boot Diagnostics storage
If ($diagContainer.Name) {
Remove-AzureStorageContainer -Name $diagContainer.Name -Force
}
#Remove and Re-Create Managed OS Disk
If ($ManagedDisk) {
Remove-AzureRMDisk -ResourceGroupName $resourceGroupName -DiskName $ManagedDiskName -Force
#Create New Disk Procedure
$snapshot = Get-AzureRmSnapshot -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName
$snapshotID = $snapshot.Id
$diskConfig = New-AzureRmDiskConfig -AccountType Standard_LRS -Location $location -CreateOption Copy -SourceResourceId $snapshotID
$diskName = "$vmName-osdisk"
$NewDisk = New-AzureRmDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $resourceGroupName
}
#Remove and Re-Create Managed Data Disk
If ($ManagedDataDiskName) {
Remove-AzureRMDisk -ResourceGroupName $resourceGroupName -DiskName $ManagedDataDiskName -Force
#Create New Disk Procedure
$DataSnapshot = Get-AzureRmSnapshot -SnapshotName $DataSnapshotName -ResourceGroupName $resourceGroupName
$DataSnapshotID = $DataSnapshot.Id
$DataDiskConfig = New-AzureRmDiskConfig -AccountType Standard_LRS -Location $location -CreateOption Copy -SourceResourceId $DataSnapshotID
$DataDiskName = "$vmName-datadisk"
$NewDataDisk = New-AzureRmDisk -DiskName $DataDiskName -Disk $DataDiskConfig -ResourceGroupName $resourceGroupName
}
#Create New VM Procedure
$Newvm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$Newvm = Add-AzureRmVMNetworkInterface -VM $Newvm -Id $NicID
$osDisk = Get-AzureRMDisk -ResourceGroupName $resourceGroupName -DiskName $diskName
$Newvm = Set-AzureRmVMOSDisk -VM $Newvm -Name $diskName -ManagedDiskId $osDisk.Id -CreateOption Attach -Windows -Caching ReadWrite
If ($ManagedDataDiskName) {
$dataDisk = Get-AzureRMDisk -ResourceGroupName $resourceGroupName -DiskName $DataDiskName
$Newvm = Add-AzureRmVMDataDisk -VM $Newvm -Name $DataDiskName -CreateOption Attach -ManagedDiskId $dataDisk.Id -Lun 0 -Caching ReadOnly
}
New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $Newvm
Echo "****************************Complete****************************************"
Oct 24 2018 03:10 AM
Sep 15 2020 02:19 PM
@Tom Harvey These instructions worked well for me: https://www.makak.ch/azure-restore-vm-disk-from-a-snapshot/
Sep 15 2020 03:23 PM
@Shane Curtis Thanks Shane, The swap OS function was not in place when i wrote this procedure. We have now reverted back to un-managed disks as managed was becoming too expensive for the amount we had.
Oct 28 2022 10:22 PM