我如何克隆Azure将磁盘管理到其他订阅中



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

我们正在谈论很多数据(200 GB ),因此实际的"复制"过程将需要太多时间。我希望能够在不到半小时的时间内自动化事物并提供新的环境。

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

我到目前为止尝试过的是:

  • 使用az disk grant-access检索托管磁盘的SAS URI;不过,这件事不接受az disk create--source(它说VHD SAS链接可以起作用...)

有什么想法?

我做到了:

$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

之后,您将在您的订阅中以常规斑点存储托管磁盘的副本。

请小心,您应该从生产订阅中获得SAS URL,但是在脚本中,您应该登录开发订阅。

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

  1. 转到Azure Portal->更多服务 ->磁盘或直接浏览此URL https://portal.azure.com/#create/microsoft.manageddisk-arm

  2. 单击 添加

  3. 选择源为存储blob

  4. 使用源斑点字段选择您的VHD。

这是我编写的脚本,以迁移每个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}
}
}

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

相关内容

  • 没有找到相关文章

最新更新