Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article contains two scripts. The first script copies a snapshot of a managed disk that was using platform-managed keys to the same or a different subscription. The second script copies a snapshot of a managed disk that was using customer-managed keys to the same or a different subscription. These scripts can be used for the following scenarios:
- Migrate a snapshot in Premium storage (Premium_LRS) to Standard storage (Standard_LRS or Standard_ZRS) to reduce your cost.
- Migrate a snapshot from locally redundant storage (Premium_LRS, Standard_LRS) to zone redundant storage (Standard_ZRS) to benefit from the higher reliability of ZRS storage.
- Move a snapshot to different subscription in the same region for longer retention.
Note
Both subscriptions must be located under the same tenant
If you don't have an Azure subscription, create a trial account before you begin.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Use the following script to sign in using a different subscription, replacing <Subscription ID>
with your Azure Subscription ID.
If you don't have an Azure trial subscription, create an Azure trial subscription before you begin.
az cloud set -n AzureChinaCloud
az login
subscription="<subscriptionId>" # add subscription here
az account set -s $subscription # ...or use 'az login'
For more information, see set active subscription or log in.
# Verified per Raman Kumar as of 2/23/2022
# <FullScript>
#Provide the subscription Id of the subscription where snapshot exists
sourceSubscriptionId="<subscriptionId>"
#Provide the name of your resource group where snapshot exists
sourceResourceGroupName=mySourceResourceGroupName
#Provide the name of the snapshot
snapshotName=mySnapshotName
#Set the context to the subscription Id where snapshot exists
az account set --subscription $sourceSubscriptionId
#Get the snapshot Id
snapshotId=$(az snapshot show --name $snapshotName --resource-group $sourceResourceGroupName --query [id] -o tsv)
#If snapshotId is blank then it means that snapshot does not exist.
echo 'source snapshot Id is: ' $snapshotId
#Provide the subscription Id of the subscription where snapshot will be copied to
#If snapshot is copied to the same subscription then you can skip this step
targetSubscriptionId=6492b1f7-f219-446b-b509-314e17e1efb0
#Name of the resource group where snapshot will be copied to
targetResourceGroupName=mytargetResourceGroupName
#Set the context to the subscription Id where snapshot will be copied to
#If snapshot is copied to the same subscription then you can skip this step
az account set --subscription $targetSubscriptionId
#Copy snapshot to different subscription using the snapshot Id
#We recommend you to store your snapshots in Standard storage to reduce cost. Please use Standard_ZRS in regions where zone redundant storage (ZRS) is available, otherwise use Standard_LRS
#Please check out the availability of ZRS here: /storage/common/storage-redundancy-zrs#support-coverage-and-regional-availability
az snapshot create --resource-group $targetResourceGroupName --name $snapshotName --source $snapshotId --sku Standard_LRS
# </FullScript>
#Provide the subscription Id of the subscription where snapshot exists
sourceSubscriptionId="<subscriptionId>"
#Provide the name of your resource group where snapshot exists
sourceResourceGroupName=mySourceResourceGroupName
#Provide the name of the target disk encryption set
diskEncryptionSetName=myName
#Provide the target disk encryption set resource group
diskEncryptionResourceGroup=myGroup
#Provide the name of the snapshot
snapshotName=mySnapshotName
#Set the context to the subscription Id where snapshot exists
az account set --subscription $sourceSubscriptionId
#Get the snapshot Id
snapshotId=$(az snapshot show --name $snapshotName --resource-group $sourceResourceGroupName --query [id] -o tsv)
#If snapshotId is blank then it means that snapshot does not exist.
echo 'source snapshot Id is: ' $snapshotId
#Get the disk encryption set ID
diskEncryptionSetId=$(az disk-encryption-set show --name $diskEncryptionSetName --resource-group $diskEncryptionResourceGroup)
#Provide the subscription Id of the subscription where snapshot will be copied to
#If snapshot is copied to the same subscription then you can skip this step
targetSubscriptionId=6492b1f7-f219-446b-b509-314e17e1efb0
#Name of the resource group where snapshot will be copied to
targetResourceGroupName=mytargetResourceGroupName
#Set the context to the subscription Id where snapshot will be copied to
#If snapshot is copied to the same subscription then you can skip this step
az account set --subscription $targetSubscriptionId
#Copy snapshot to different subscription using the snapshot Id
#We recommend you to store your snapshots in Standard storage to reduce cost. Please use Standard_ZRS in regions where zone redundant storage (ZRS) is available, otherwise use Standard_LRS
#Please check out the availability of ZRS here: /storage/common/storage-redundancy-zrs#support-coverage-and-regional-availability
#To change the region, use the --location parameter
az snapshot create -g $targetResourceGroupName -n $snapshotName --source $snapshotId --disk-encryption-set $diskEncryptionSetID --sku Standard_LRS --encryption-type EncryptionAtRestWithCustomerKey
Run the following command to remove the resource group, VM, and all related resources.
az group delete --name mySourceResourceGroupName
This script uses following commands to create a snapshot in the target subscription using the Id
of the source snapshot. Each command in the table links to command specific documentation.
Command | Notes |
---|---|
az snapshot show | Gets all the properties of a snapshot using the name and resource group properties of the snapshot. The Id property is used to copy the snapshot to different subscription. |
az snapshot create | Copies a snapshot by creating a snapshot in different subscription using the Id and name of the parent snapshot. |
Create a virtual machine from a snapshot
For more information on the Azure CLI, see Azure CLI documentation.
More virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.