Skip to content
Related Articles

Related Articles

Microsoft Azure – Copy an Azure Managed Data Disk

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Mar, 2022

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:

  • Cloning the Managed Disk Data and using across multiple azure servers. 
  • Data Transfer from one region to another region as a backup or for any other project needs
  • For Backup Needs
  • For cleaning the data in the existing managed disks. 
  • In a few cases, this process can also be used for versioning.

Pre-requisite: 

  • You must have an Existing Data Disk to Copy in Azure
  • Owner, Contributor Access is required.
  • If you were using Windows PowerShell you should install AzCopy v10.

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

  • $SubscriptionName = “_add_subscription_name”
  • $sourceRG = “_add_source_rg_name_”
  • $sourceDiskName = “_add_source_datadisk_name_”
  • $targetDiskName = “_add_target_datadisk_name_”
  • $targetRG = “_add_target_rg_name_”
  • $targetLocate = “_add_target_location_”
  • $targetOS = “add_system_ostype_” #either “Windows” or “Linux”
  • $SKUName = ‘_add_disk_sku_type_’ #either “Premium_LRS” or “Standard_LRS” or any other.

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:

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!