Create an Azure Disk Snapshot — PowerShell
Working in Hyper-V before moving all of our resources from physical servers in datacenters to Azure IaaS or PaaS, we regularly took new snapshots of Virtual Machines (VMs) before testing major development changes, adding Windows updates, testing new application settings etc., to allow us to easily revert to the previous state of the VM if desired. In Azure, snapshots are taken of the virtual disks (vhd), not the VM instance itself. Snapshots are full, read-only copies of the vhds. A new VM is created with new managed disks created from stored snapshots of OS data disks; data disk snapshots are turned into managed data disks and then attached to a VM.
An Azure snapshot of a data or operating system (os) vhd can be used:
- For custom backup/restore of a VMs vhds
- For troubleshooting disk problems.
- To create a copy of production servers for use in development, or the opposite, copy a dev environment into production mode.
- To quickly duplicate a fresh VM instance. For example, we use specific single-tier and double-tier web server/sql server environments that need to be reproduced for various testing scenarios. A new ‘exact copy’ environment, with all accounts and applications in place, can be ready for access within 5 minutes if necessary, using stored Azure Snapshots of the OS and data disks. These are considered ‘specialized’ disks…
- To create a ‘repository’ of prepared OS and data disks for use in creating multiple VM copies.
- To create a dr backup repository of snapshots in a different region or subscription (or both), in case of accidental deletion of key OS or data disks.
- Backing up a VM before making a major change — although it is not possible to revert the VM to the previous state, the VM can be deleted and using the saved snapshots > create new managed OS and data disks > create a new VM using the previous VMs Nic, etc.
- While a snapshot can be taken of an active disk, it is recommended to shut the VM down before taking a snapshot
- Managed Disk snapshots are used to create a point in time copy of an existing disk that can then be used to create a new VM
- You need to create a managed disk from a snapshot before it can be used with a VM
- These directions are for taking a snapshot of a managed OS disk in these examples — ARMs default vhds.
- Add your own values for the parameters in the POSH below.
Take a vhd snapshot with PowerShell:
1. #Login to AzurePortal in PowerShell and select subscription
Select-AzureRmSubscription -SubscriptionName '<Your Subscription Name>'
2. #Declare Parameters
$resourceGroupName = "<Your Resource Group Name>"
$vmName="<Name of VM>"
$Location = "<Your Location>"
$snapshotName = "<Snapshot Name>"
3. #Get the VM of the managed disk
$vm = get-azureRmVm -ResourceGroupName $resourceGroupName -Name $vmName
4. #Create the Snapshot Configuration — (for the osDisk or for dataDisk(s)
$vmOSDisk=(Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName).StorageProfile.OsDisk.Name
$Disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $vmOSDisk
$SnapshotConfig = New-AzureRmSnapshotConfig -SourceUri $Disk.Id -CreateOption Copy -Location $Location
5. #Take the Snapshot
$Snapshot=New-AzureRmSnapshot -Snapshot $snapshotConfig -SnapshotName `
$snapshotName -ResourceGroupName $resourceGroupName
The new snapshot ‘2016DemoSnapshotOS’ is now showing in the list of Snapshots available in the Azure account:
Originally published at https://arlanblogs.alvarnet.com on April 27, 2018