If you’re used to right-clicking on a VM in your favorite hypervisor and creating a snapshot, you’ll be in for a rude awakening when running VMs in the Microsoft Azure cloud. But, don’t fret, with the know-how you’ll learn in this tutorial, you can build your own tooling to create easy Azure VM snapshots!
Taking a snapshot or a backup of a VM is commonplace. There are always times when you need to back up a VM at a point in time and restore it at some later date. In the Azure cloud, it’s not quite as easy as clicking a few times though.
When a process isn’t as easy as you think it should be is a great time to build and use your own custom tool. You can rarely go wrong picking PowerShell as the scripting language of choice to make it happen.
In this article, you’re going to learn how to both back up and restore Azure virtual machines using a community PowerShell module called AzureVMSnapshots.
Table of Contents
Before You Start
This article will be a tutorial showing you, step-by-step how to perform some tasks. If you’d like to follow along, please be sure you have met the following prerequisites:
- Windows PowerShell 5.1 or PowerShell 6+. The examples were only tested on PowerShell Core 6.2.3.
- An existing Azure VM – This VM will be used as the guinea pig to back up.
- The Azure PowerShell modules installed –
Install-Module Azin your PowerShell console
- Connected to an Azure subscription
Install the AzureVMSnapshots Module
Installing the AzureVMSnapshots module is easy since it’s available via the PowerShell Gallery. Run
Install-Module AzureVMSnapshots to get the module downloaded and installed.
Creating an Azure VM Snapshot
Once you’ve got a PowerShell console opened and authenticated to the Azure subscription where your VM lives, create a new snapshot with
New-AzVmSnapshot only requires two parameters – the name of the VM and the resource group its located in. You can see an example below of creating a snapshot for a VM called VM1 in the Demo resource group.
Once executed, this may take a couple of minutes. In the background, Azure is stopping the VM and creating a snapshot. This cmdlet takes a snapshot of the OS disk.
New-AzVmSnapshot -VmName VM1 -ResourceGroupName 'Demo'
If you don’t want to shut down the VM, do NOT run this command!
Finding Available Snapshots
When you create a snapshot, they will be saved with a specific schema in the name. Regardless if you’ve created other snapshots another way, the AzureVMASnapshot module’s
Get-AzVmSnapshot command will only return snapshots created with the module.
Below you can see an example of retrieving all snapshots stored in the Demo resource group.
Get-AzVmSnapshot -ResourceGroupName 'Demo'
Perhaps you’ve made some changes to the VM you didn’t meant to and would like to restore a snapshot. To restore snapshots, you can pipe a snapshot returned via
Get-AzVmSnapshot to the
Restore-AzVmSnapshot command like the example below.
Get-AzVmSnapshot -ResourceGroupName 'Demo' -VmName VM1 | Restore-AzVmSnapshot
When this command runs, you will then get prompted if you’d really like to restore the VM. If so, the VM will be shut down and the original source disk replaced with the snapshot disk.
Note: By default,
Restore-AzVmSnapshotwill leave the old disk. If you’d like to remove it after restoration is complete, use the
Finally, if you don’t need a snapshot or set of snapshots anymore, you can remove them with the
Remove-AzVmSnapshot command. The concept to remove a snapshot is the same as restoring one – pipe the snapshot from
Get-AzVmSnapshot -ResourceGroupName 'Demo' -VmName VM1 | Remove-AzVmSnapshot
Using a community PowerShell module and just a few commands, you can easily create, manage, restore and remove Azure VM snapshots with just a few keystrokes!