Restoring a snapshot of a managed OS to an existing virtual machine

Similar to this one , but with permissions exclusively and with managed disks and an existing virtual machine.

I took a snapshot of the managed OS disk and want to restore it, but I can't figure out how to do it. I tried several things, but now I think you cannot detach the OS disk even if the VM is freed. No matter how much I cut into the link , I cannot find anything to help me restore the snapshot to an existing disk. Is it possible?

+3


source to share


2 answers


I cannot find anything to help me restore a snapshot of an existing disk. Is it possible?

As far as I know, Azure does not support snapshot recovery to an existing disk.

But we can use a snapshot to create a managed disk and attach it to an existing virtual machine.

For example, a PowerShell script uses a snapshot to create a managed disk:



PS C:\Users> $resourceGroupName = 'vm'
PS C:\Users> $snapshotResourceGroupName = 'vm'
PS C:\Users> $snapshotName = 'manageddisk1'
PS C:\Users> $managedDiskType = 'StandardLRS'
PS C:\Users> $location = 'eastus'
PS C:\Users> $managedDiskCreateOption = 'Copy'
PS C:\Users> $diskName = 'manageddisk2'
PS C:\Users> $snapshot = Get-AzureRmSnapshot -SnapshotName $snapshotName -ResourceGroupName $snapshotResourceGr
oupName
PS C:\Users> $diskConfig = New-AzureRmDiskConfig -AccountType $managedDiskType -Location $location -CreateOptio
n $managedDiskCreateOption -SourceResourceId $snapshot.Id
PS C:\Users> New-AzureRmDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $resourceGroupName


AccountType        : StandardLRS
TimeCreated        : 4/21/2017 1:26:27 PM
OsType             : Windows
CreationData       : Microsoft.Azure.Management.Compute.Models.CreationData
DiskSizeGB         : 128
EncryptionSettings :
OwnerId            :
ProvisioningState  : Succeeded
Id                 : /subscriptions/5384xxxx-xxxx-xxxx-xxxx-xxxxe29axxxx/resourceGroups/vm/providers/Microsoft.Compute/
                     disks/manageddisk2
Name               : manageddisk2
Type               : Microsoft.Compute/disks
Location           : eastus
Tags               :

      

If you want to attach it to an existing VM, we can use this script:

PS C:\Users> $datadisk2 = Get-AzureRmDisk -ResourceGroupName vm -DiskName manageddisk2
PS C:\Users> $vmName = 'jasonvm'
PS C:\Users> $rgname = 'vm'
PS C:\Users> $dataDiskName = 'manageddisk2'
PS C:\Users> $vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName
PS C:\Users> $vm = Add-AzureRmVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataD
isk2.Id -Lun 2
PS C:\Users> Update-AzureRmVM -VM $vm -ResourceGroupName $rgName

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK

      

Thus, this managed disk can be found in the Azure VM: enter image description here

+2


source


The New-AzureRMDiskConfig command has a switch -SourceResourceId

that you can use to point to the created snapshot.

For example:

$diskConfig = New-AzureRMDiskConfig -CreateOption Copy -SourceResourceId <<id>> -Location westus -DiskSizeGB 64 -AccountType StandardLRS

      

The Resource ID is the snapshot ID of the managed disk and can be found in the properties of that snapshot in the portal.



You will then create a new disk from this disk configuration. for example

$disk = New-AzureRmDisk -DiskName "name" -Disk $diskConfig -ResourceGroupName rgname

      

Once launched, you will see the new disk in the target resource group. Then you can use this to create a virtual machine or attach as needed.

UPDATE: The official documentation can be found here . please note that in this example they are using -CreateOption Import

and not Copy as I did.

0


source







All Articles