如何将Azure托管磁盘克隆到其他订阅?

时间:2017-03-03 06:48:19

标签: azure command-line-interface disk managed

使用Azure VM和托管磁盘(使用ARM部署模型),我最近遇到了以下要解决的问题:为了测试目的从托管磁盘中获取生产数据,我想从"生产订阅"克隆生产数据磁盘进入"开发订阅"中的托管磁盘,我可以安全地使用数据。

我们正在谈论相当多的数据(200 GB +),因此实际的"复制"过程需要花费太多时间。我希望能够实现自动化并提供新的环境 - 比如半小时之内。

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

到目前为止我尝试过:

  • 使用az disk grant-access检索托管磁盘的SAS URI;这个东西不被--source az disk create接受(虽然说VHD SAS链接可以工作......)

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

我这样做了:

$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。

请注意,您应该从Production订阅中获取SAS URL,但在脚本中您应该登录Development订阅。

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

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

  2. 点击+添加

  3. 选择来源作为存储blob

  4. 使用source blob字段选择您的vhd。

答案 1 :(得分:0)

这是我编写的脚本,用于将每个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}
}
}

答案 2 :(得分:0)

您可以在 Azure 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