Export/Copy managed snapshots as VHD to a storage account in different region with CLI

This script exports a managed snapshot to a storage account in different region. It first generates the SAS URI of the snapshot and then uses it to copy it to a storage account in different region. Use this script to maintain backup of your managed disks in different region for disaster recovery.

To run this sample, make sure you have installed the latest Azure CLI 2.0. To start, run az login to create a connection with Azure.

This sample works in a Bash shell. For options on running Azure CLI scripts on Windows client, see Running the Azure CLI in Windows.

If you don't have an Azure subscription, create a free account before you begin.

Sample script

#Provide the subscription Id where snapshot is created

#Provide the name of your resource group where snapshot is created

#Provide the snapshot name 

#Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
#Know more about SAS here: https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1

#Provide storage account name where you want to copy the snapshot. 

#Name of the storage container where the downloaded snapshot will be stored

#Provide the key of the storage account where you want to copy snapshot. 

#Provide the name of the VHD file to which snapshot will be copied.

az account set --subscription $subscriptionId

sas=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)

az storage blob copy start --destination-blob $destinationVHDFileName --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas

Script explanation

This script uses following commands to generate SAS URI for a managed snapshot and copies the snapshot to a storage account using SAS URI. Each command in the table links to command specific documentation.

Command Notes
az snapshot grant-access Generates read-only SAS that is used to copy underlying VHD file to a storage account or download it to on-premises
az storage blob copy start Copies a blob asynchronously from one storage account to another

Next steps

Create a managed disk from a VHD

Create a virtual machine from a managed disk

For more information on the Azure CLI, see Azure CLI documentation.

Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.