Copying Azure Managed disks between regions

I recently had the need to copy some Azure managed disks to another region. Since managed disks are not backed by storage accounts like unmanaged disks, you cannot simply do a blob copy and be done. You have to copy your managed disk to a temporary storage account in your target region and create a managed disk from it.

I could not find something that did what I wanted, so I had to roll my own in the form of a PowerShell script. The script does the following:

  • Takes a source and target resource group, a target location, a set of managed disks and a target SKU as parameters. (Source and target resource groups can be the same.)
  • Creates a temporary storage account with a random name (mdcopynnnnn) in the target resource group in the target location. A new storage account is created each time the script runs.
  • Copies each disk into the target storage account using a blob copy with a SAS.
  • Creates new managed disk resources in the target resource group and target location, with the same names as the source disks.

Important notes:

  • Only supports OS disks for now, since I could not be bothered to write code to check if the disks are OS or data. Maybe later…
  • By default the target storage account is PremiumLRS, you can change this in the variables.
  • I use a 3600 seconds (1 hour) duration for the Shared Access Signature. If you think your disk takes longer to copy than that; increase the value in the variables.
  • There is a timeout, set to the same value as the SAS duration, so the script will continue after the copy has been going on for longer.
  • Assumes you have already logged into Azure and selected the correct subscription.
  • Only copies disks within the same subscription.
  • Assumes your target resource group already exists.
  • Neither deletes the temporary storage accounts or the source managed disks. I will not delete your data!
  • Hardly any error checking.
  • Absolutely no warranty whatsoever; use at your own risk!

https://gist.github.com/morgansimonsen/203983b7c52d1fce497c3a9fd4c24b21

M

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.