David
David

Reputation: 197

How to revert an azure disk back to its former snapshot?

I have a VM in azure, and via the portal have selected its Disk, and created a snapshot of it. How do I now revert back to that snapshot for the Disk (via portal or CLI)?

I'm not looking to create new disks or VMs from the snapshot, just revert back.

Upvotes: 2

Views: 7339

Answers (4)

thezar
thezar

Reputation: 1298

I did it with the PS script below. It stops VM, creates a new disk using the snapshot, and then swaps VM to this new disk. Snapshot has been created on stopped VM.

$resourceGroupName = '...' 
$location = 'eastus2' 
$vmName = '...'
$snapshotName = '...' 

$snapshotinfo = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName 

$vm = Get-AzVM `
-ResourceGroupName $resourceGroupName `
-Name $vmName 

Stop-AzVM -ResourceGroupName $resourceGroupName -Name $vm.Name -Force

# Create the new disk that you want to swap in
$newDiskName = $($vmName + "_disk_" + (Get-Date).ToString("yyyyMMddhhmm"))
$newDisk = New-AzDisk -DiskName $newDiskName (New-AzDiskConfig  -zone 1 -Location $location -CreateOption Copy -SourceResourceId $snapshotinfo.Id) -ResourceGroupName $resourceGroupName

Set-AzVMOSDisk -VM $vm -ManagedDiskId $newDisk.Id -Name $newDisk.Name 
Update-AzVM -ResourceGroupName $resourceGroupName -VM $vm 

Start-AzVM -Name $vm.Name -ResourceGroupName $resourceGroupName 

Upvotes: 0

juanMSFT
juanMSFT

Reputation: 11

This worked for eastus zone 1 using the cloud shell in the azure portal

$SnapshotName = "my_snapshot"
$SnapshotResourceGroup = "my_resource_group" 
$DiskNameOS = "my_new_snapshotdisk" 
$snapshotinfo = Get-AzSnapshot -ResourceGroupName $SnapshotResourceGroup -SnapshotName $snapshotName
New-AzDisk -DiskName $DiskNameOS (New-AzDiskConfig  -zone 1 -Location eastus -CreateOption Copy -SourceResourceId $snapshotinfo.Id) -ResourceGroupName $SnapshotResourceGroup

After that I went to the VM in the portal, selected "disks" and selected "swap os disk".

Upvotes: 1

Marcel Boldt
Marcel Boldt

Reputation: 252

I did it successfully with Azure PowerShell, using Set-AzVMOSDisk. There is a module which unfortunately can only restore the snapshots that it created, but its code demonstrates how it works.

Summarised:

Get-AzSnapshot -ResourceGroupName "..." # to find the Disk ID
$vm = Get-AzVM -Name "..."
$old_disk = Get-AzDisk -Name $vm.StorageProfile.OsDisk.name
$diskconf = New-AzDiskConfig -AccountType $old_disk.sku.name -Location $old_disk.Location -SourceResourceId "Id of the disk" -CreateOption Copy
$newdisk = NewAzDisk -Disk $diskconf -ResourceGroupName "..." -DiskName "OS_disk_$((New-Guid).ToString())"
Set-AzVMOSDisk -VM $vm -ManagedDiskId $newdisk.Id -Name $newdisk.Name
Update-AzVM -ResourceGroupName "..." -VM $vm

Upvotes: 0

Jason Ye
Jason Ye

Reputation: 13954

How do I now revert back to that snapshot for the Disk (via portal or CLI)?

Do you mean you want to use this snapshot to rollback your system?

Unfortunately, for now Azure does not support this, we can't use snapshot to revert back.

In Azure, we can't revert back Azure VM directly, we should create disk or VM from that snapshot.

By default, snapshot used for Azure backup. In Azure recovery services, we can restore VMs from the snapshot. Restore this VM was create a new VM with this OS disk, not rollback.

Upvotes: 2

Related Questions