Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to create snapshots for Azure managed disks

·1068 words·6 mins· 100 views · 5 likes ·
Add-AzVMNetworkInterface Azure PowerShell Connect-AzAccount Get-AzDisk

Azure managed disks have a long list of benefits that make them the ideal choice to use with your virtual machines. One of those benefits is the ability to take snapshots of your virtual disks. This post will show you how to take snapshots of your managed disks and how to create a VM using these snapshots. Requirements:

  • This tutorial assumes that you already have a Microsoft Azure account set up.
  • You have an existing virtual machine that uses managed disks. If you want to know how to convert Azure VM to use Managed Disks, see this link.

Important: Snapshots are billed based on the used size.

Azure PowerShell Workaround>

Azure PowerShell Workaround #

If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"
Create a snapshot>

Create a snapshot #

Set the variables Here, we define the characteristics of our environment

$resourceGroupName = 'RG-DEMO-NE'
$location = 'North Europe'
$vmName = "VM-DEMO-NE"
Get the virtual machine>

Get the virtual machine #

To get and store the virtual machine in a variable, use Get-AzVM with the following syntax.

$vm = Get-AzVM `
    -Name $vmName `
    -ResourceGroupName $resourceGroupName
Set the snapshot configuration>

Set the snapshot configuration #

To set snapshot settings for an OS Disk, you should use New-AzSnapshotConfig cmdlet with the following syntax.

$snapshotConfig =  New-AzSnapshotConfig `
    -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id `
    -Location $location `
    -CreateOption copy `
    -SkuName Standard_LRS

Depending on the value established in the -SKU parameters, you can store your snapshot in

  • Standard_LRS
  • Standard_ZRS
  • Premium_LRS

If instead, you want to set snapshot settings for a Data disk, you should use the following syntax.

$snapshotConfig = New-AzSnapshotConfig `
    -SourceUri $vm.StorageProfile.DataDisks[0].ManagedDisk.Id `
    -Location $location `
    -CreateOption copy `
    -SkuName Standard_LRS

You must keep in mind the number of disks attached to your virtual machine to set the index. In my case, I only have a Data disk attached to the VM.

Take the snapshot>

Take the snapshot #

To take a snapshot, you should use the New-AzSnapshot cmdlet with the following syntax. In the following example, I set the name of the snapshots by adding the date to the name of the VM, but you can choose the name you want for your snapshots.,

$timestamp = Get-Date -Format yyMMddThhmmss
$snapshotName = ($vmName+$timestamp)

New-AzSnapshot `
    -Snapshot $snapshotConfig `
    -SnapshotName $snapshotName `
    -ResourceGroupName $resourceGroupName
New-AzSnapshot
>

New-AzSnapshot
#

Create an incremental snapshot>

Create an incremental snapshot #

Microsoft has recently released the ability to take incremental snapshots of your managed disks. These types of snapshots include only all changes since the last snapshot. To use this type of snapshot follow the next steps. You must first get the disk you have snapshots of and want to take an incremental snapshot. To do this, you should use the cmdlet with the following syntax.

$disk = Get-AzDisk `
    -DiskName $vm.StorageProfile.OsDisk.Name `
    -ResourceGroupName $resourceGroupName
Set the snapshot configuration>

Set the snapshot configuration #

To create an incremental snapshot, you should use the New-AzSnapShotConfig cmdlet with the new parameter -Incremental.

$snapshotConfig=New-AzSnapshotConfig `
    -SourceUri $disk.Id `
    -Location $disk.Location `
    -CreateOption Copy `
    -Incremental

Unlike regular snapshots, these snapshots are always stored on standard HDDs, regardless of the type of storage on the primary disks. Also, for increased reliability, they are stored in Zone Redundant Storage (ZRS) by default in regions that support ZRS.

Take the incremental snapshot>

Take the incremental snapshot #

To take a snapshot, you should use the New-AzSnapshot cmdlet with the following syntax. In the following example, I set the name of the snapshots by adding the date to the name of the VM, but you can choose the name you want for your snapshots.

$timestamp = Get-Date -Format yyMMddThhmmss
$snapshotName = ($vmName+$timestamp)

New-AzSnapshot `
    -Snapshot $snapshotConfig `
    -SnapshotName $snapshotName `
    -ResourceGroupName $resourceGroupName
New-AzSnapshot
>

New-AzSnapshot
#

Create a VM from an existing snapshot>

Create a VM from an existing snapshot #

Here I will show you how to create a virtual machine from an existing snapshot taken from an OS disk. Use the Get-AzSnapshot cmdlet to list all available snapshots within the resource group is obtained.

Get-AzSnapshot `
    -ResourceGroupName $resourceGroupName `
    | Select-Object Name, DiskSizeGB, Incremental,ProvisioningState,UniqueId

Snapshots Managed disks
Once the snapshot is identified, the snapshot needs to be converted to a virtual disk. To do this, you should use the following commands.

$snapshot = Get-AzSnapshot `
    -ResourceGroupName $resourceGroupName `
    -SnapshotName <SnapshotName>

$diskConfig = New-AzDiskConfig `
    -SkuName Standard_LRS `
    -Location $location `
    -CreateOption Copy `
    -SourceResourceId $snapshot.Id

$newDisk = New-AzDisk `
    -DiskName "NewOSDisk" `
    -Disk $diskConfig `
    -ResourceGroupName $resourceGroupName

Then you should set the characteristics of the new VM. In my case, I use the original VM as a reference.

$vm = get-azvm `
    -Name $vmName `
    -ResourceGroupName $resourceGroupName

Here we set the size of the new virtual machine and disable boot diagnostics.

$newVM = New-AzVMConfig `
    -VMName "VM-DEMO-COPY" `
    -VMSize $VM.HardwareProfile.VmSize

$newVM = Set-AzVMBootDiagnostic `
    -VM $newVM `
    -Disable

We use the Resource ID of the newly managed disk to attach it to the virtual machine. In the last parameter, you can change the operating system to Linux if the operating system disk has Linux OS

$newVM = Set-AzVMOSDisk `
    -VM $newVM `
    -ManagedDiskId $newDisk.Id `
    -CreateOption Attach `
    -Windows

Select and store in the variable $vnet, the virtual network where the virtual machine will be hosted.

$vnet = Get-AzVirtualNetwork `
    -Name "DEMO-VNET" `
    -ResourceGroupName $resourceGroupName

We create and add a network interface to the configuration of the new virtual machine.

$nic = New-AzNetworkInterface `
    -Name ('NIC-'+$newVM.Name) `
    -ResourceGroupName $resourceGroupName `
    -Location $snapshot.Location `
    -SubnetId $vnet.Subnets[0].Id

$newVM = Add-AzVMNetworkInterface `
    -VM $newVM `
    -Id $nic.Id

And finally, we create a virtual machine with the established configuration.

New-AzVM `
    -VM $newVM `
    -ResourceGroupName $resourceGroupName `
    -Location $snapshot.Location

New-AzVM
Another possibility to access the data stored in the snapshots is to convert the snapshots to virtual hard disks and then mount them as data disks in another VM.

Thanks for reading my post. I hope you find it helpful.

If you want to know more about Azure Managed Disks, check out this link.