Disk snapshots are a useful backup and recovery feature of Hyper-V and VMware virtual machines (VMs), but move those VMs to Azure, and you're in for a rude awakening. When you're used to right-clicking on a VM to create a point-in-time snapshot of the VM at any time and restoring that VM to the same spot with a few clicks or keystrokes, it can be frustrating to figure out how to bring that functionality to Azure VMs.

Adam Bertram

Adam Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at adamtheautomator.com, or follow TechSnips on Twitter at @techsnips_io.

In PowerShell, Azure disk snapshots are possible using the New-AzSnapshot command. This command takes a snapshot of a virtual hard disk (VHD). You can also "restore" this snapshot by creating a new VM in Azure, but most of the time when you need to restore a snapshot, you'd prefer to use the same VM just like a Hyper-V checkpoint.

Luckily, this snapshot or checkpoint functionality is possible to some degree with the OS disk on Azure VMs using a little bit of PowerShell.

To create, restore, and manage these snapshots, I've created a PowerShell module called AzVmSnapshots. This module implements much of the code to create and restore Azure snapshots but also can attach snapshots to existing VMs, removing the requirement to create a new one.

To use the AzVmSnapshots module, you'll need to install the Az module via Install-Module -Name Az. After installation, you'll then need to connect to your Azure subscription using Connect-AzAccount. When authenticated, download the AzVmSnapshots module by running Install-Module AzVmSnapshots.

After the download, check out the commands available in this module.

Azure VM snapshot commands

Azure VM snapshot commands

You'll see we've got a few options. The New-AzVmSnapshot command creates a VHD snapshot for a particular VM. The Get-AzVmSnapshot command returns snapshots created with New-AzVmSnapshot, and Restore-AzVmSnapshot applies a saved snapshot to a specific VM.

To create a new snapshot, you can pass the VM name and resource group the VM exists in to the New-AzVmSnapshot command. This will stop the VM, create the VHD snapshot attached to the VM, and bring the VM back up.

When the snapshot is complete, you can then run Get-AzVmSnapshot to query all the snapshots you've created using the AzVmSnapshots module. New‑AzVmSnapshot creates VHD snapshots using the format AzVmSnapshot--. It applies this naming convention to differentiate snapshots taken with the AzVMSnapshots module from other snapshots you may have in your subscription.

Query snapshots

Query snapshots

Let's say you've now made some changes to your VM and you want to restore it to the same state as when you created the snapshot. We'll use Restore-AzVmSnapshot. To use this command, we can pipe the output of Get-AzVmSnapshot to Restore-AzVmSnapshot. This will find the snapshot created earlier and send it to be restored to the same VM it was taken on.

You'll also see that it'll prompt you to restore the snapshot because it shuts down the VM and relinks the snapshot VHD. You can also use the RemoveOriginalDisk parameter to remove the VHD previously attached to the VM.

A lot of PowerShell goes on inside these functions to make it happen, but by using the functions in the AzureVmSnapshots module, you don't have to worry about all of that.

At this point, you should now have a VHD attached to your original VM in the same state as when the snapshot was taken!

Win the monthly 4sysops member prize for IT pros

0
Share
11 Comments
  1. PaulR 3 months ago

    Adam, can you discuss pros/cons of using this module vs the native Azure Backup restore and Replace Existing Disks? I know one is the per-instance cost of Azure Backup, but are there other factors to consider?

    Thanks

    PaulR

    1+

  2. Swapnil Kambli 3 months ago

    Some high-level comparison points between snapshots vs azure backup as a method of BC/DR

    1. Azure Backup is a SaaS product and thus bounded by various thresholds e.g. Max 16 parallel backups at a time or number of backups of a disk per day etc. 
       Whereas Snapshots thresholds are in the scale of thousands(e.g. Number of snapshots in azure account) and hence any production system can fit within default limits. 

    2. Azure Backup is well integrated with VSS and hence produces application consistent backups. Whereas for taking snapshots VM shutdown is recommended.
       On the same line snapshots for RAID kind of disk configuration may result in unusable backups.

    3. Azure snapshots is basically a storage cost and hence comparatively cheaper than Azure backup.

    4. Azure snapshots are basically a read-only copy of the full disk. And multiple disk snapshots means multiple full disk copies.
       Azure Backups are incremental in nature. And hence over the long term can save storage and cost.

    5. In my opinion, snapshots are suited for Dev environment for quick access. Whereas for any production environment Azure Backup is a recommended solution. 

    3+

  3. Leos Marek 3 months ago

    Calling snapshot a backup? 

    1+

    Users who have LIKED this comment:

    • avatar
    • Swapnil Kambli 3 months ago

      AWS/Azure snapshots have been used as backup solutions in Dev/Staging environment since they evolved from blob snapshot. However, its use as BC/DR solution in PROD environment should be restricted. 

      0

      • Leos Marek 3 months ago

        As I see it snapshot is simply not a backup. Backup implies that you are able to restore the operation in case of HW failure or data loss. This is not what snapshot offers 🙂 Of course there are products that perform snapshot based backup, but thats different story 🙂

        2+

        Users who have LIKED this comment:

        • avatar
        • Swapnil Kambli 3 months ago

          True. There will always be a tradeoff between cost and consistency. Also with modern IT, requirements & factors for the backup solution may vary considerably. For example, application having less frequent data changes e.g. Big Data, static public website etc. snapshots are being used over backup solutions for outperforming BC/DR indicators such as RTO, RPO.

          0

  4. Joshua Joseph 2 months ago

    Adam, Is the module AzVmSnapshots still available? I tried to install it and getting no match found.

    0

  5. Swapnil Kambli 2 months ago

    Just to touch base on Azure Portal experience, if we are using Azure 'Managed Disks', Azure Portal provides sophisticated UI for 'Managed Disks' snapshot creation and management. We can migrate from unmanaged disks to Managed disks to leverage many storage management features including Snapshot on the portal.

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account