3

Similar to this one but powershell exclusively and with managed disks and an existing VM.

I took a snapshot of a managed OS disk and want to restore it but can't figure out how. I have tried a number of things but now think that you can't detach an OS disk even if the VM is deallocated. No matter how much I pore through the reference, I can't find anything to help me restore a snapshot to an existing disk. Is this even possible?

Community
  • 1
  • 1
sirdank
  • 3,351
  • 3
  • 25
  • 58

2 Answers2

3

I can't find anything to help me restore a snapshot to an existing disk. Is this even possible?

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

But we can use the snapshot to create a Managed Disk and attach it to an existing VM.

Here is the PowerShell script use 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

In this way, we can find this managed disk in Azure VM: enter image description here

Jason Ye
  • 13,710
  • 2
  • 16
  • 25
  • Why is that not supported? Once it is a "disk" can't you use it to create a new VM? – CtrlDot Apr 21 '17 at 16:36
  • But I can only use this for a data disk, correct? Not an OS disk? – sirdank Apr 21 '17 at 16:54
  • Also you can create an os disk with this snapshot, then use this is disk to create a new vm. – Jason Ye Apr 21 '17 at 22:32
  • @ctrldot I mean azure does not support restore data from snapshot to an existing disk direct, as you say, we can use snapshot to create OS disk then use this OS disk to create new vm – Jason Ye Apr 21 '17 at 22:37
  • @sirdank you are right, when we want to attach it to an existing vm, it work as a data disk. if you want us this snapshot as an OS disk, we can use this snapshot to create an OS disk then use this is disk to create new vm – Jason Ye Apr 21 '17 at 22:46
  • @sirdank Please let me know if you would like further assistance. – Jason Ye Apr 24 '17 at 06:25
  • @JasonYe-MSFT Thank you for your help but one of my constraints is not to create a new VM so this sounds like it's not feasible at the moment. – sirdank Apr 24 '17 at 12:43
1

The New-AzureRMDiskConfig command has a switch -SourceResourceId that you can use to point to the snapshot you have created.

For example:

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

The resource ID is that of your managed disk snapshot which can be found under the properties for that snapshot in the portal.

After that, you would create a new disk from this disk config. For example

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

After you run that, you will see a new disk in the target resource group. You can then use that to create a VM or attach as required.

UPDATE: Official documentation can be found here. Please note that in that example they use the -CreateOption Import rather than Copy like I did.

CtrlDot
  • 2,463
  • 14
  • 11