Open In App

Microsoft Azure – Copy an Azure Managed Data Disk

In this article, we will learn about “How we can manage disk data from an existing disk to a new disk”. In this process, we will be using the Azure PowerShell Script to run this. What we are trying to achieve here is first of all we need a copy of an existing managed data disk from this we are copying the data to a new managed disk using the copy module from Azure PageBlob.

This scenario can be used for the following activities:



Pre-requisite: 

Implementation:

Follow the below steps to Copy an Azure Managed Data Disk:



Step 1: Log in to Azure Portal

Step 2: Once you have logged from azure menu access Azure Cloud Shell >> Switch to PowerShell Mode

Step 3: Now, Use the below following command create a new PowerShell File. Let’s say “copy-managed-data-disk.ps1”. 

touch copy-managed-data-disk.ps1

Step 4: To write into a file or to edit the file use the below command

code copy-managed-data-disk.ps1

Copy paste the below PowerShell Script in copy-managed-data-disk.ps1

$SubscriptionName = "_add_subscription_name"
Set-AzureRmContext -SubscriptionName "$SubscriptionName" | Out-Null

$sourceRG = "_add_source_rg_name_"
$sourceDiskName = "_add_source_datadisk_name_"

$targetDiskName = "_add_target_datadisk_name_"
$targetRG = "_add_target_rg_name_"
$targetLocate = "_add_target_location_"

#Expected value for OS is either "Windows" or "Linux"
$targetOS = "add_system_ostype_"

$SKUName = '_add_disk_sku_type_' #Premium_LRS or Standard_LRS

$sourceDisk = Get-AzDisk -ResourceGroupName $sourceRG -DiskName $sourceDiskName

# Adding the sizeInBytes with the 512 offset, and the -Upload flag
$targetDiskconfig = New-AzDiskConfig -SkuName -osType $targetOS `
-UploadSizeInBytes $($sourceDisk.DiskSizeBytes+512) `
-Location $targetLocate -CreateOption 'Upload'

$targetDisk = New-AzDisk -ResourceGroupName $targetRG -DiskName $targetDiskName -Disk $targetDiskconfig

$sourceDiskSas = Grant-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName `
-DurationInSecond 86400 -Access 'Read'

$targetDiskSas = Grant-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName `
-DurationInSecond 86400 -Access 'Write'

azcopy copy $sourceDiskSas.AccessSAS $targetDiskSas.AccessSAS --blob-type PageBlob

Revoke-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName

Revoke-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName

Now, fill up the details of the following in script

Note: Modify the changes according to your requirement for copying data to disk.

Step 5: After you make the changes in the script, use the below command to run the PowerShell script

./copy-managed-data-disk.ps1

Note: execution process will take time to depending on the size of the disk. On an average, for 50 GB of data it will process in 15-20, if you are using a Premium LRS. In case of Standard LRS it might take 30-40 minutes on an average for copying data from one disk to another.

Output: Once, the copying is done. Then the result will look the following:

 

Article Tags :