Cloud Skills Blog

Closing the Skills Gap One Student at a Time

Copying VHDs (Blobs) between Storage Accounts

A common question is how do I move Windows Azure Virtual Machines between storage accounts or subscriptions? The ability to copy VHDs (or any other blob for that matter) is built natively into the Windows Azure platform through the blob copy API. You can copy VHDs between storage accounts in the same region/data center or across regions/data centers and best of all the source and destination do not even have to be in the same subscription.

Before tackling this problem with the Windows Azure PowerShell cmdlets it is helpful to understand some of the behind the scenes architecture to explain the behaviors you will see in practice.

All VHDs in Windows Azure live within the Windows Azure Storage Service. Windows Azure Storage lives in various data centers throughout the world and within each data center WA Storage can be further divided into multiple storage stamps (clusters of servers and disks). The location and stamp placement of your source and destination storage accounts matters greatly when using the blob copy operation.

Multiple Stamps within the Same Data Center
storage-1
Stamps across Data Centers
storage-2

The reason to point this out is it makes a significant impact on the performance of copying VHDs (or other blobs).

Example Copy Operations Between Storage Accounts (127 GB OS Disk used for Testing)

Source Destination Result
mystorage1 mystorage1 Shadow Copy – instantaneous
mystorage1 mystorage2 Shadow Copy – instantaneous
mystorage1 mystorage3 Cross Stamp Copy – (12 Minutes and 3 seconds)
mystorage1 mystorage4 Cross Data Center Copy – (22 Minutes and 54 seconds)

TIP: You can tell if storage accounts are in the same storage stamp if they share the same IP Address

Initiating an Asynchronous Copy Request (anonymous source)

In this example I am copying the VHD from a non-authenticated source. This means I have opened up access on the source container in Windows Azure Storage to allow anonymous access. Note: the source URL does NOT have to be based in Windows Azure Storage. The source file can reside in any publicly accessible location.

Select-AzureSubscription "my subscription"  
 
### Source VHD (West US) - anonymous access container ###
$srcUri = "http://mwwestus1.blob.core.windows.net/source/testcopy1.vhd" 
 
### Target Storage Account (East US) ###
$storageAccount = "mweastus1"
$storageKey = "STORAGEACCOUNTKEY"
 
### Create the destination context for authenticating the copy
$destContext = New-AzureStorageContext  –StorageAccountName $storageAccount `
                                        -StorageAccountKey $storageKey  
 
### Target Container Name
$containerName = "copiedvhds"
 
### Create the target container in storage
New-AzureStorageContainer -Name $containerName -Context $destContext 
 
### Start the Asynchronous Copy ###
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
                                    -DestContainer $containerName `
                                    -DestBlob "testcopy1.vhd" `
                                    -DestContext $destContext

Initiating an Asynchronous Copy Request (authenticated source)

This example is essentially the same code except I am specifying a context object to access the source storage account. This allows you to copy VHDs from Windows Azure storage without opening up permissions to everyone.

Select-AzureSubscription "my subscription" 
 
### Source VHD (West US) - authenticated container ###
$srcUri = "http://mwwestus1.blob.core.windows.net/sourceauth/testcopy1.vhd" 
 
### Source Storage Account (West US) ###
$srcStorageAccount = "mwwestus1"
$srcStorageKey = "SOURCESTORAGEKEY"
 
### Target Storage Account (West US) ###
$destStorageAccount = "mwwestus2"
$destStorageKey = "DESTSTORAGEKEY"
 
### Create the source storage account context ### 
$srcContext = New-AzureStorageContext  –StorageAccountName $srcStorageAccount `
                                        -StorageAccountKey $srcStorageKey  
 
### Create the destination storage account context ### 
$destContext = New-AzureStorageContext  –StorageAccountName $destStorageAccount `
                                        -StorageAccountKey $destStorageKey  
 
### Destination Container Name ### 
$containerName = "copiedvhds"
 
### Create the container on the destination ### 
New-AzureStorageContainer -Name $containerName -Context $destContext 
 
### Start the asynchronous copy - specify the source authentication with -SrcContext ### 
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
                                    -SrcContext $srcContext `
                                    -DestContainer $containerName `
                                    -DestBlob "testcopy1.vhd" `
                                    -DestContext $destContext

Checking Copy Status
Once the copy begins it is completely asynchronous meaning you could immediately start other copy operations without waiting for the previous to complete. It is useful of course to determine the progress of your blob copy. This is made possible through the Get-AzureStorageBlobCopyState cmdlet. Pipe the blob object returned from Start-AzureStorageBlobCopy to Get-AzureStorageBlobCopyState and it will return another object with progress indicators as to how many bytes have been copied and the current status of the operation.

### Retrieve the current status of the copy operation ###
$status = $blob1 | Get-AzureStorageBlobCopyState 
 
### Print out status ### 
$status 
 
### Loop until complete ###                                    
While($status.Status -eq "Pending"){
  $status = $blob1 | Get-AzureStorageBlobCopyState 
  Start-Sleep 10
  ### Print out status ###
  $status
}

Example output from Get-AzureStorageBlobCopyState
blob-copy-status

leave a reply

Share this page

Categories

Connect with Opsgility

Opsgility instructor @beckybertram will walk through some #SharePoint Online basics on Wednesday—join her: https://t.co/UYHzyeg41z

What big splashes do you expect #Microsoft & their partners to make at #MSIgnite? Discuss w/ us on Sunday. https://t.co/ZKEEkwxHmg #OpsChat

.@razibinrais provides an introduction to #Windows #Containers in this beginner-level course: https://t.co/sdfDGP8T4i

Load More...