Revert to Azure Managed Disk snapshot on Azure VM

Copper Contributor

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?

17 Replies
Hi, Do you want to use this VM as master Image? are you going to create multiple VM from this image? Best Sunit Patil

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

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

image.png

Best

Sunit Patil

Skype: Sunitonline

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.

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?


 

 

Have you guys seen this? but this only available in Managed Disk

https://docs.microsoft.com/en-us/azure/virtual-machines/linux/snapshot-copy-managed-disk

Use Azure portal

  1. Sign in to the Azure portal.
  2. Starting in the upper-left, click Create a resource and search for snapshot.
  3. In the Snapshot blade, click Create.
  4. Enter a Name for the snapshot.
  5. Select an existing Resource group or type the name for a new one.
  6. Select an Azure datacenter Location.
  7. For Source disk, select the Managed Disk to snapshot.
  8. Select the Account type to use to store the snapshot. We recommend Standard_LRS unless you need it stored on a high performing disk.
  9. Click Create.

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.

Hi Panos,
I haven't spent any more time on this since my post. I wrote a script that will recreate the Disk from the snapshot and then recreate the VM using the new Disk. It deletes the VM but retains the VM components, nic, pip etc. The whole process takes about 5 minutes which isn't too bad. Sometimes when the VM comes back up its lost its domain account although it's an easy fix and happens 1 out of every 10 restores old say. Needless to say, the process is working for us I'm just surprised there isn't an easier option. On your note, I'm not sure you can detach / attach an os Disk from a VM, not sure I've tried that although could be a better option.

I'm happy to share my script for anyone interested, been a few comments on here since my last post.
Tom Harvey, Please send me your script.
I appreciate it
Robin Hood

Hi Can you please send me the script too.

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. 

 

https://docs.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-windows-powershell-...

 

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.

 

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****************************************"

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

You my refer to this powershell script to take the azure vm snapshot and to restore a vm from the snapshot

https://www.magicpowershell.com/2022/10/azure-vm-snapshot-and-restore-with-powershell.html

Hi All, 

Here is how, you can revert back to previous Snapshopt on the existing VM and not by deleting and re-creating the VM. 

1. Login to Azure Portal. 
2. Navigate to "snapshots". 

3. Select the Snapshot that you want your VM revert back to. 
4. Crete the Disk using the Snapshot that you want to revert to. Now the Disk is ready. 
5. Navigate to Virtual Machines in Azure Portal. 
6. Select the Virtual Machine you want to revert. 
7. Navigate to "Disks". 
8. Select the OS Disk of the VM. There will an Option to "swap OS disks". 

9. Select "swap OS disks", it will open a form to select the New Disk. 

10. Select the New Disk That Created in Step#4. 

11. Provide the VM Name and Say Submit. 
12. Now the VM will stop --> Swap the OS Disk. Wait for the process to complete. 

13. Start the VM and try to login and verify your packages.