Introduction
Azure snapshots are point-in-time backups of managed disks. They are mainly used for:
- Creating backups for disaster recovery.
- Migrating disk data across different subscriptions or tenants.
- Creating new managed disks from existing snapshots.
However, manually handling snapshot migration, especially across tenants or subscriptions, can be tedious. Another point to note is that, snapshots could be very large depending on the size of the Virtual Machine(VM) it was created from.
In this guide, we’ll show how to migrate Azure snapshots efficiently; whether it’s a one-time task or handling multiple snapshots in bulk.
Why Migrate Snapshots?
There are a couple of reasons why you would want to migrate your snapshot to a different tenant. These include:
- Consolidation of services and licenses to improve efficiency and reduce expenses.
- Mergers and Acquisitions of companies hence the need for a centralized tenant.
- Ensuring backups for the snapshots in another tenant.
Prerequisites
- Access permissions: Access permissions to both source and destination snapshots and storage accounts respectively.
- Destination storage and container.
-
Key Information:
- Subscription ID of source account.
- Resource group name containing the snapshot.
- Storage name and access key for the destination.
Migrating a Single Snapshot
Follow these steps to migrate a single snapshot
-
Generate Shared Access Signature (SAS) for the snapshot.
az snapshot grant-access \ --resource-group <resource-group-name> \ --name <snapshot-name> \ --duration-in-seconds 3600 \ --query [accessSas] -o tsv
This generates a SAS URL for the snapshot, valid for the specified duration (3600 seconds in this case).
Here are the parameters explained,
resource-group-name
: Replace this with the resource group name which has the snapshot.
snapshot-name
: Replace this with the name of the snapshot. -
Copy snapshot to the Destination.
az storage blob copy start \ --destination-blob <destination-file-name>.vhd \ --destination-container <container-name> \ --account-name <storage-account-name> \ --account-key <storage-account-key> \ --source-uri <sas-url>
Parameters explained,
destination-file-name
: Give your it a name of your choice, for examplemysnapshot.vhd
.
storage-account-name
: Storage account name of destination tenant.
storage-account-key
: Access key of the storage account.
sas-url
: SAS Uri generated in STEP 1.
Migrating Multiple Snapshots
Now, let’s automate the migration for multiple snapshots with a Bash script. This approach is perfect for those managing multiple snapshots across large environments.
-
Create an empty file.
touch transfer_script
-
Open the file.
vim transfer_script
-
Add these to the file.
#!/bin/bash # Azure subscription ID subscriptionId="your-subscription-id" # Resource group containing snapshots resourceGroupName="your-resource-group" # SAS expiry duration (seconds) sasExpiryDuration=3600 # Destination storage account and key storageAccountName="your-storage-account" storageAccountKey="your-storage-key" # Destination container name storageContainerName="snapshots" # Snapshots array: ["snapshot-name destination-file-name"] snapshots=( "<snapshot1> <snapshot1.vhd>" "<snapshot2> <snapshot2.vhd>" "<snapshot3> <snapshot3.vhd>" ) # Set the Azure subscription az account set --subscription "$subscriptionId" # Loop through each snapshot and migrate for snapshot in "${snapshots[@]}"; do # Extract snapshot name and destination file name snapshotName=$(echo "$snapshot" | awk '{print $1}') destinationVHDFileName=$(echo "$snapshot" | awk '{print $2}') echo "Processing snapshot: $snapshotName -> $destinationVHDFileName" # Generate SAS URL sas=$(az snapshot grant-access \ --resource-group "$resourceGroupName" \ --name "$snapshotName" \ --duration-in-seconds "$sasExpiryDuration" \ --query [accessSas] -o tsv) if [ -z "$sas" ]; then echo "Failed to generate SAS for $snapshotName. Skipping..." continue fi # Copy to destination az storage blob copy start \ --destination-blob "$destinationVHDFileName" \ --destination-container "$storageContainerName" \ --account-name "$storageAccountName" \ --account-key "$storageAccountKey" \ --source-uri "$sas" if [ $? -eq 0 ]; then echo "Successfully copied $snapshotName to $destinationVHDFileName." else echo "Failed to copy $snapshotName." fi done
Extra parameters explained
snapshot1
andsnapshot1.vhd
are the names of the snapshot in the source file and how you would want to name them at the destination respectively. Do this for all. Press
ESC
, then:wq
to save the file.-
Change the file permissions to executable.
chmod +x transfer_script
-
Run the executable.
./transfer_script
Reference Links
Microsoft Official Documentation
Conclusion
Whether you’re migrating a single snapshot or handling multiple backups across subscriptions, automating the process saves time and ensures accuracy. With the script provided, you can adapt it to your specific needs and scale operations as required.
Reach out to me via LinkedIn , X and Email.
Cheers 🚀
Top comments (0)