如何将 Azure 托管磁盘克隆到不同的订阅中?

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

使用 Azure VM 和托管磁盘(使用 ARM 部署模型),我最近 运行 遇到以下我想解决的问题:为了从托管磁盘中获取生产数据以进行测试目的,我想将生产数据磁盘从 "Production Subscription" 克隆到 "Development Subscription" 中的托管磁盘,在那里我可以安全地处理数据。

我们正在谈论大量数据 (200 GB+),因此实际 "copying" 过程将花费太多时间。我希望能够在 - 比方说半小时内实现自动化并提供新环境。

在订阅中克隆托管磁盘(假设它在同一区域)非常简单快速,我只需在 az disk create 命令中指定一个 --source。这显然不适用于订阅,至少因为开发订阅的登录 user/service 主体无权访问生产订阅资源。

到目前为止我尝试了什么:

有什么想法吗?

我这样做了:

$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

在此之后,您的订阅中将有一份托管磁盘副本存储为常规 blob。

注意,您应该从生产订阅中获取 SAS URL,但在脚本中您应该登录到开发订阅。

接下来您可以转到 Azure 门户并将 blob 转换为托管磁盘。

  1. 转到 Azure 门户 --> 更多服务 --> 磁盘或直接浏览此 URL https://portal.azure.com/#create/Microsoft.ManagedDisk-ARM

  2. 点击+添加

  3. Select 源作为存储 blob

  4. Select 您的 vhd 使用源 blob 字段。

这是我编写的脚本,用于将每个 VM 的所有托管磁盘从一个订阅迁移到另一个订阅。希望对你有帮助。

# This script will get ALL VMs in a subscription and then migrate the disks 
if the VM has managed disks
# Created by Joey Brakefield -- @kfprugger & https://www.linkedin.com/in/joeybrakefield/

#set global variables
$sourceSubscriptionId='6a1b5e5e-df06-4608-a7a2-6984f7abacd8'
select-azurermsubscription -subscriptionid $sourceSubscriptionId
$vms = get-azurermvm
$targetSubscriptionId='929e0340-bf36-45a2-8347-47f86b4715de'


#looping logic for each of the VMs that have managed disks
foreach ($vm in $vms) {
select-azurermsubscription -subscriptionid $sourceSubscriptionId

$vmrg = get-azurermresourcegroup -name $vm.ResourceGroupName
$vmname = $vm.name

Write-Host = "Working with: " $vmname " in " $vmrg -foregroundcolor Green 
Write-Host ""

#This command will only target managed disks because unmanaged use the storage account locations rather than the /disks provider URIs

if (Get-AzureRmDisk | ? {$_.OwnerId -like "/subscriptions/"+$sourceSubscriptionId +"/resourceGroups/"+$vmrg.resourcegroupname+"/providers/Microsoft.Compute/virtualMachines/"+$vm.name})
{
#Sanity Check
#Read-host "Look correct? If not, CTRL-C to Break"
$manageddisk =  Get-AzureRmDisk | ? {$_.OwnerId -like "/subscriptions/"+$sourceSubscriptionId +"/resourceGroups/"+$vmrg.resourcegroupname+"/providers/Microsoft.Compute/virtualMachines/"+$vm.name}
Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId
#check to see if RG exists in the new CSP/Subscription 

Get-AzureRmResourceGroup -Name $vmrg.resourcegroupname -ev notPresent -ea 0
write-host "Checking to see if"$vmrg.resourcegroupname"exists in subscriptionid"$targetSubscriptionId -foregroundcolor Cyan
Write-Host ""
if ($notPresent)
{
    new-azurermresourcegroup -name $vmrg.resourcegroupname -location $vmrg.location
    "Resource Group " + $vmrg.resourcegroupname + " has been created"
    } else {"Resource Group " + $vmrg.resourcegroupname +  " already exists"}
    # Move the disks after all checks are done
    foreach ($disk in $managedDisk){
        $managedDiskName = $disk.Name
        $targetResourceGroupName = $vmrg.resourcegroupname

        $diskConfig = New-AzureRmDiskConfig -SourceResourceId $disk.Id -Location $disk.Location -CreateOption Copy 

        New-AzureRmDisk -Disk $diskConfig -DiskName $Disk.Name -ResourceGroupName $targetResourceGroupName}
}
}

您可以在A​​zure CLI中使用以下命令-

# Source storage account name
STORAGE1=sourcestorage

#Security key of the source storage account
STORAGEKEY1= SampleKey0qNzttE/EX3hHfcFIzkQQmqXklRU2Z2uANICw==

#Container containing the source VHD
CONTAINER1=sourcevhds

# Name of VHD to be copied (name only, not full url)
DISK=DiskToBeCopied.vhd

#Specify the above properties for target
STORAGE2=targetstorage
STORAGEKEY2= SampleKeyAb6FYP3EqFVEcN2cc5wO QHzXvdc7Gzh1qRt0FXKq6w==
CONTAINER2= targetvhds

设置以上参数后,在Azure CLI中执行以下命令-

azure storage blob copy start --account-name $STORAGE1 --account-key $STORAGEKEY1 --source-container $CONTAINER1 --source-blob $Disk --dest-account-name $STORAGE2 --dest-account-key $STORAGEKEY2 --dest-container $CONTAINER2