对 Azure VM 进行 PowerShell 复制时排除磁盘Exclude disks from PowerShell replication of Azure VMs

本文介绍如何在复制 Azure VM 时排除磁盘。This article describes how to exclude disks when you replicate Azure VMs. 可以通过排除磁盘来优化消耗的复制带宽,或者优化此类磁盘使用的目标端资源。You might exclude disks to optimize the consumed replication bandwidth or the target-side resources that those disks use. 目前,仅通过 Azure PowerShell 提供此功能。Currently, this capability is available only through Azure PowerShell.

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。This article has been updated to use the Azure Az PowerShell module. 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 AzTo learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

必备条件Prerequisites

开始之前:Before you start:

为什么要从复制中排除磁盘Why exclude disks from replication

需要从复制中排除磁盘可能是因为:You might need to exclude disks from replication because:

如何从复制中排除磁盘How to exclude disks from replication

在示例中,我们将中国东部区域中拥有一个 OS 和三个数据磁盘的虚拟机复制到中国北部区域。In our example, we replicate a virtual machine that has one OS and three data disks that's in the China East region to the China North region. 虚拟机的名称为“AzureDemoVM” 。The name of the virtual machine is AzureDemoVM. 我们排除磁盘 1,保留磁盘 2 和 3。We exclude disk 1 and keep disks 2 and 3.

获取要复制的虚拟机的详细信息Get details of the virtual machines to replicate

# Get details of the virtual machine
$VM = Get-AzVM -ResourceGroupName "A2AdemoRG" -Name "AzureDemoVM"

Write-Output $VM     
ResourceGroupName  : A2AdemoRG
Id                 : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/A2AdemoRG/providers/Microsoft.Compute/virtualMachines/AzureDemoVM
VmId               : 1b864902-c7ea-499a-ad0f-65da2930b81b
Name               : AzureDemoVM
Type               : Microsoft.Compute/virtualMachines
Location           : chinaeast
Tags               : {}
DiagnosticsProfile : {BootDiagnostics}
HardwareProfile    : {VmSize}
NetworkProfile     : {NetworkInterfaces}
OSProfile          : {ComputerName, AdminUsername, WindowsConfiguration, Secrets}
ProvisioningState  : Succeeded
StorageProfile     : {ImageReference, OsDisk, DataDisks}

获取虚拟机的磁盘的详细信息。Get details about the virtual machine's disks. 此信息会在以后启动 VM 复制时用到。This information will be used later when you start replication of the VM.

$OSDiskVhdURI = $VM.StorageProfile.OsDisk.Vhd
$DataDisk1VhdURI = $VM.StorageProfile.DataDisks[0].Vhd

复制 Azure 虚拟机Replicate an Azure virtual machine

对于以下示例,我们假设你已有缓存存储帐户、复制策略和映射。For the following example, we assume that you already have a cache storage account, replication policy, and mappings. 如果没有这些项目,请按使用 Azure PowerShell 为 Azure 虚拟机设置灾难恢复中介绍的过程操作。If you don't have these things, follow the process at Set up disaster recovery for Azure virtual machines using Azure PowerShell.

复制包含托管磁盘的 Azure 虚拟机。Replicate an Azure virtual machine with managed disks.


#Get the resource group that the virtual machine must be created in when failed over.
$RecoveryRG = Get-AzResourceGroup -Name "a2ademorecoveryrg" -Location "China North"

#Specify replication properties for each disk of the VM that is to be replicated (create disk replication configuration).

#OsDisk
$OSdiskId =  $vm.StorageProfile.OsDisk.ManagedDisk.Id
$RecoveryOSDiskAccountType = $vm.StorageProfile.OsDisk.ManagedDisk.StorageAccountType
$RecoveryReplicaDiskAccountType =  $vm.StorageProfile.OsDisk.ManagedDisk.StorageAccountType

$OSDiskReplicationConfig = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -ManagedDisk -LogStorageAccountId $ChinaEastCacheStorageAccount.Id `
         -DiskId $OSdiskId -RecoveryResourceGroupId  $RecoveryRG.ResourceId -RecoveryReplicaDiskAccountType  $RecoveryReplicaDiskAccountType `
         -RecoveryTargetDiskAccountType $RecoveryOSDiskAccountType

# Data Disk 1 i.e StorageProfile.DataDisks[0] is excluded, so we will provide it during the time of replication. 

# Data disk 2
$datadiskId2  = $vm.StorageProfile.DataDisks[1].ManagedDisk.id
$RecoveryReplicaDiskAccountType =  $vm.StorageProfile.DataDisks[1]. StorageAccountType
$RecoveryTargetDiskAccountType = $vm.StorageProfile.DataDisks[1]. StorageAccountType

$DataDisk2ReplicationConfig  = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -ManagedDisk -LogStorageAccountId $ChinaEastCacheStorageAccount.Id `
         -DiskId $datadiskId2 -RecoveryResourceGroupId  $RecoveryRG.ResourceId -RecoveryReplicaDiskAccountType  $RecoveryReplicaDiskAccountType `
         -RecoveryTargetDiskAccountType $RecoveryTargetDiskAccountType

# Data Disk 3

$datadiskId3  = $vm.StorageProfile.DataDisks[2].ManagedDisk.id
$RecoveryReplicaDiskAccountType =  $vm.StorageProfile.DataDisks[2]. StorageAccountType
$RecoveryTargetDiskAccountType = $vm.StorageProfile.DataDisks[2]. StorageAccountType

$DataDisk3ReplicationConfig  = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -ManagedDisk -LogStorageAccountId $ChinaEastCacheStorageAccount.Id `
         -DiskId $datadiskId3 -RecoveryResourceGroupId  $RecoveryRG.ResourceId -RecoveryReplicaDiskAccountType  $RecoveryReplicaDiskAccountType `
         -RecoveryTargetDiskAccountType $RecoveryTargetDiskAccountType

#Create a list of disk replication configuration objects for the disks of the virtual machine that are to be replicated.
$diskconfigs = @()
$diskconfigs += $OSDiskReplicationConfig, $DataDisk2ReplicationConfig, $DataDisk3ReplicationConfig

#Start replication by creating a replication protected item. Using a GUID for the name of the replication protected item to ensure uniqueness of name.
$TempASRJob = New-ASRReplicationProtectedItem -AzureToAzure -AzureVmId $VM.Id -Name (New-Guid).Guid -ProtectionContainerMapping $EusToWusPCMapping -AzureToAzureDiskReplicationConfiguration $diskconfigs -RecoveryResourceGroupId $RecoveryRG.ResourceId

启动复制操作成功后,VM 数据将复制到恢复区域。When the start-replication operation succeeds, the VM data is replicated to the recovery region.

转到 Azure 门户即可看到复制的 VM 位于“复制的项”下。You can go to the Azure portal and see the replicated VMs under "replicated items."

复制过程首先在恢复区域中植入虚拟机复制磁盘的副本。The replication process starts by seeding a copy of the replicating disks of the virtual machine in the recovery region. 此阶段中称为初始复制阶段。This phase is called the initial-replication phase.

初始复制完成后,复制过程进入差异同步阶段。After initial replication finishes, replication moves on to the differential-synchronization phase. 此时,虚拟机受到保护。At this point, the virtual machine is protected. 选择受保护的虚拟机,看是否排除了任何磁盘。Select the protected virtual machine to see if any disks are excluded.

后续步骤Next steps

了解如何运行测试性故障转移Learn about running a test failover.