Search code examples
azurecommand-line-interfacediskmanaged

How do I clone an Azure Managed Disk into a different subscription?


Using Azure VMs and managed disks (using the ARM deployment model), I have recently run into the following problem I would like to solve: In order to get production data out from a managed disk for testing purposes, I would like to clone a production data disk from the "Production Subscription" into a managed disk in the "Development Subscription", where I can play around with the data in a safe way.

We are talking quite a lot of data (200 GB+), so that an actual "copying" process would take far too much time. I want to be able to automate things and provision new environments in - let's say, under half an hour.

Cloning a managed disk within a subscription (given it's in the same region) is very simple and fast, I just have to specify a --source to the az disk create command. This does not work across subscriptions obviously, at least because the logged in user/service principal for the development subscription does not have access to the production subscription resources.

What I have tried so far:

  • Using az disk grant-access to retrieve an SAS URI for the managed disk; this thing is not accepted as a --source for az disk create though (it says VHD SAS links would work though...)

Any ideas?


Solution

  • I did this:

    $RG = "youresourcegroup"
    $Location = "West US 2"
    $StorageAccName = "yourstorage"
    $SkuName = "Standard_LRS"
    $Containername = "images"
    $Destdiskname = “yorblob.vhd”
    $SourceSASurl = "https://yoursaasurl"
    
    Login-AzureRmAccount
    
    New-AzureRmResourceGroup -Name $RG -Location $Location
    New-AzureRmStorageAccount -ResourceGroupName $RG -Name $StorageAccName -SkuName $SkuName -kind Storage -Location $Location 
    
    $Storageacccountkey = Get-AzureRmStorageAccountKey -ResourceGroupName $RG -Name $StorageAccName
    $Storagectx = New-AzureStorageContext -StorageAccountName $StorageAccName -StorageAccountKey $Storageacccountkey[0].Value
    
    $Targetcontainer = New-AzureStorageContainer -Name $Containername -Context $storagectx -Permission Blob
    
    
    $sourceSASurl = $mdiskURL.AccessSAS
    $ops = Start-AzureStorageBlobCopy -AbsoluteUri $SourceSASurl -DestBlob $Destdiskname -DestContainer $Containername -DestContext $Storagectx
    Get-AzureStorageBlobCopyState -Container $Containername -Blob $Destdiskname -Context $Storagectx -WaitForComplete
    

    After this you will have a copy of managed disk in your subscription stored as a regular blob.

    Be careful, you should obtain SAS URL from Production subscription, but in the script you should login to a Development subscription.

    Next you can go to the Azure Portal and convert the blob to managed disk.

    1. Go to Azure portal --> More Services --> Disks or directly browse this URL https://portal.azure.com/#create/Microsoft.ManagedDisk-ARM

    2. Click +Add

    3. Select source as storage blob

    4. Select your vhd using source blob field.