Snapshots are a tried and trusted way to save a point-in-time copy of a virtual machine that users can easily revert to when needed. In Microsoft Azure, however, snapshots are not the same as what a seasoned on-site administrator might expect.
Admins can't revert a VM to a snapshot with just a few clicks using standard Azure tools like they would on premises. Instead, they need to build a new Azure machine and use the snapshot disk. However, unlike traditional snapshots, Azure snapshots can create new VMs, which is useful for testing and one-off backups.
Below, we'll learn how to create and use Azure VM snapshots for custom backup.
What are Azure snapshots and how do they work?
Azure snapshots exist as a backup tool to ensure the administrator has a good copy of the virtual machine disks if needed. Azure snapshots can also be cloned into additional VMs. Users can create Azure VM snapshots using PowerShell or Azure CLI.
Keep these best practices in mind when creating and using Azure snapshots:
- Where possible, create the Azure snapshot when the machine is powered off -- it makes the disk more consistent. If the VM is not powered off already, it will be when the snapshot is taken.
- Don't keep snapshots for longer than needed; they cost money and can negatively affect performance.
- Don't use snapshots as a long-term backup strategy. Instead, use Azure Backup -- it doesn't require human interaction, which reduces human error and other risks.
IT teams can use an Azure snapshot to complete tasks such as:
- custom backup;
- restore and rebuild a VM;
- disaster recovery;
- overwrite OS disks without recreating a VM;
- make a copy of a VM; and
- troubleshoot VM issues.
Creating point-in-time copies manually isn't always the best method for backup, given the steps involved. This is where Azure Backup comes into play.
Admins can use Azure Backup to restore the entire VM -- a process that is not automatically available with Azure Virtual Machines.
Azure Backup functions much the same as manual snapshots. It operates while the VM is still running, so you don't have to power down every fifteen minutes to take a consistent snapshot.
Create a snapshot with Azure Portal
To create an Azure VM snapshot, navigate to the Azure Portal and select Create a resource. Use the search function and type in "snap" to take a basic snapshot. This will highlight the snapshot blade. Click on the link to open the required blade, as shown in Figure 1.
Click the Add button to create the new Azure snapshot. Once the snapshot wizard is open, select the following properties for your snapshot:
- Select the resource group to which the server belongs.
- Give the snapshot a name.
- Select which disks you need. Select all the disks if you are creating the snapshot as an impromptu backup.
Finish by clicking Review and create.
Create a snapshot with PowerShell
Admins can also create snapshots with PowerShell. They'll need to set several variables, call the New-AzSnapshot and pass those variables as parameters. This approach requires admins to install the Azure PowerShell plugin.
Microsoft offers a sample script for handling this task in PowerShell. To run it for your VMs, just replace the first three variables in the first step below. You'll also have to address additional configuration variables to create the new snapshot on the last command.
The code below, provided by Microsoft, shows how to copy the VHD disk and create the snapshot configuration. Follow these four steps:
1. Set parameters.
$resourceGroup = 'MyTTExample'
$vmName = "SnapExample"
$snapshotName = 'ExampleSnap'
2. Retrieve the VM.
$vm = Get-AzVM `
3. Create the snapshot configurations.
$snapshot = New-AzSnapshotConfig `
-SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id `
-Location $location `
4. Take the snapshot.
-Snapshot $snapshot `
-SnapshotName $snapshotName `
See an example of running this script below, in Figure 2.
Create a snapshot with Azure CLI
The last way to create a snapshot is with Azure's command line interface. This can be done using the az set of commands. To create a snapshot, the user will need to install the Azure CLI and log in, or use Cloud Shell.
In this example, the az snapshot create command is used with the --source-disk parameter. This OS disk backup snapshot example is created in two steps:
1. Use az vm show to retrieve the disk ID.
osDiskId=$(az vm show \
-g myResourceGroup \
-n myVM \
--query "storageProfile.osDisk.managedDisk.id" \
2. Use az snapshot to create a snapshot named Disk-backup.
az snapshot create \
-g myResourceGroup \
--source "$DiskId" \
Azure Managed Disks
Admins can also use Azure Managed Disks snapshots to rebuild a VM or perform a point-in-time restoration based on a previous snapshot. Azure Disk Backup, which is separate from Azure Backup and is currently in preview, protects data in managed disks and provides snapshot lifecycle management.
Admins can download the disks from Azure. In the GUI, navigate to the snapshot blade, select the desired snapshot and click Snapshot export, then follow the wizard.
Use the generate download URL. This will provide an HTTP link to download the image for other uses and safe keeping. It's best to use a download tool that supports resumption because it's a big file to have to re-download if it breaks and doesn't auto-resume.
Checking your snapshot is simple. Use the Get-AzSnapshot command and it will list the snapshots within that subscription.
To convert the snapshotted disk to a fully managed disk, navigate to the snapshot and click create disk, as shown in Figure 3. The only additional item required is a disk name. Give it an appropriate name and submit the request.
Once the disk is created, the next step is to create the VM. Click on the newly created disk, shown under the disks blade in Figure 4. As long as the data disk is valid, there will be an option to create a VM from the disk.
Once initiated, the wizard will provide the standard VM setup options to change as required, using the snapshotted disk. The image below used a separate Azure snapshot disk from another disk, but is at the same stage of the process.