将资源移到新的资源组或订阅Move resources to a new resource group or subscription

本文说明了如何将 Azure 资源移动到另一 Azure 订阅,或移动到同一订阅下的另一资源组。This article shows you how to move Azure resources to either another Azure subscription or another resource group under the same subscription. 可以使用 Azure 门户、Azure PowerShell、Azure CLI 或 REST API 移动资源。You can use the Azure portal, Azure PowerShell, Azure CLI, or the REST API to move resources.

在移动操作过程中,源组和目标组都会锁定。Both the source group and the target group are locked during the move operation. 在完成移动之前,将阻止对资源组执行写入和删除操作。Write and delete operations are blocked on the resource groups until the move completes. 此锁意味着将无法添加、更新或删除资源组中的资源。This lock means you can't add, update, or delete resources in the resource groups. 这并不意味着资源已被冻结。It doesn't mean the resources are frozen. 例如,如果将 Azure SQL 逻辑服务器及其数据库移到新的资源组或订阅,则使用这些数据库的应用程序不会受停机影响。For example, if you move an Azure SQL logical server and its databases to a new resource group or subscription, applications that use the databases experience no downtime. 它们仍可读取和写入数据库。They can still read and write to the databases. 锁定时间最长可达四小时,但大多数移动所需的完成时间要少得多。The lock can last for a maximum of four hours, but most moves complete in much less time.

移动资源只会将其移到新的资源组或订阅。Moving a resource only moves it to a new resource group or subscription. 但不会更改该资源的位置。It doesn't change the location of the resource.

更改的资源 IDChanged resource ID

移动资源时,会更改其资源 ID。When you move a resource, you change its resource ID. 资源 ID 的标准格式为 /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}The standard format for a resource ID is /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}. 将资源移到新的资源组或订阅时,会更改该路径中的一个或多个值。When you move a resource to a new resource group or subscription, you change one or more values in that path.

如果在任意位置使用资源 ID,则需要更改该值。If you use the resource ID anywhere, you'll need to change that value. 例如,如果门户中有引用资源 ID 的自定义仪表板,则需要更新该值。For example, if you have a custom dashboard in the portal that references a resource ID, you'll need to update that value. 查找需要针对新资源 ID 进行更新的任何脚本或模板。Look for any scripts or templates that need to be updated for the new resource ID.

移动资源前需查看的清单Checklist before moving resources

移动资源之前需执行的一些重要步骤。There are some important steps to do before moving a resource. 验证这些条件可以避免错误。By verifying these conditions, you can avoid errors.

  1. 要移动的资源必须支持移动操作。The resources you want to move must support the move operation. 有关支持移动的资源列表,请参阅资源的移动操作支持For a list of which resources support move, see Move operation support for resources.

  2. 某些服务在移动资源时有特定的限制或要求。Some services have specific limitations or requirements when moving resources. 如果要移动以下任何服务,请在移动之前查看该指南。If you're moving any of the following services, check that guidance before moving.

  3. 如果移动的资源具有直接分配给该资源(或子资源)的 Azure 角色,则该角色分配不会移动,将会变成孤立状态。If you move a resource that has an Azure role assigned directly to the resource (or a child resource), the role assignment isn't moved and becomes orphaned. 移动后必须重新创建角色分配。After the move, you must re-create the role assignment. 最终,孤立角色分配会被自动删除,但我们建议你在进行移动之前先删除角色分配。Eventually, the orphaned role assignment is automatically removed, but we recommend removing the role assignment before the move.

    若要了解如何管理角色分配,请参阅列出 Azure 角色分配分配 Azure 角色For information about how to manage role assignments, see List Azure role assignments and Assign Azure roles.

  4. 源订阅和目标订阅必须处于活动状态。The source and destination subscriptions must be active. 如果在启用已禁用的帐户时遇到问题,请创建 Azure 支持请求If you have trouble enabling an account that has been disabled, create an Azure support request. 支持类型 中选择“订阅管理” 作为问题类型。Select Subscription Management in Support type for the issue type.

  5. 源订阅与目标订阅必须在同一个 Azure Active Directory 租户中。The source and destination subscriptions must exist within the same Azure Active Directory tenant. 若要检查这两个订阅是否具有相同的租户 ID,请使用 Azure PowerShell 或 Azure CLI。To check that both subscriptions have the same tenant ID, use Azure PowerShell or Azure CLI.

    对于 Azure PowerShell,请使用:For Azure PowerShell, use:

    (Get-AzSubscription -SubscriptionName <your-source-subscription>).TenantId
    (Get-AzSubscription -SubscriptionName <your-destination-subscription>).TenantId
    

    对于 Azure CLI,请使用:For Azure CLI, use:

    az account show --subscription <your-source-subscription> --query tenantId
    az account show --subscription <your-destination-subscription> --query tenantId
    

    如果源订阅和目标订阅的租户 ID 不相同,可使用以下方法协调租户 ID:If the tenant IDs for the source and destination subscriptions aren't the same, use the following methods to reconcile the tenant IDs:

  6. 必须针对要移动的资源的资源提供程序注册目标订阅。The destination subscription must be registered for the resource provider of the resource being moved. 否则,会收到错误,指明 未针对资源类型注册订阅If not, you receive an error stating that the subscription is not registered for a resource type. 将资源移到新的订阅时,可能会看到此错误,但该订阅从未配合该资源类型使用。You might see this error when moving a resource to a new subscription, but that subscription has never been used with that resource type.

    对于 PowerShell,请使用以下命令来获取注册状态:For PowerShell, use the following commands to get the registration status:

    Set-AzContext -Subscription <destination-subscription-name-or-id>
    Get-AzResourceProvider -ListAvailable | Select-Object ProviderNamespace, RegistrationState
    

    若要注册资源提供程序,请使用:To register a resource provider, use:

    Register-AzResourceProvider -ProviderNamespace Microsoft.Batch
    

    对于 Azure CLI,请使用以下命令来获取注册状态:For Azure CLI, use the following commands to get the registration status:

    az account set -s <destination-subscription-name-or-id>
    az provider list --query "[].{Provider:namespace, Status:registrationState}" --out table
    

    若要注册资源提供程序,请使用:To register a resource provider, use:

    az provider register --namespace Microsoft.Batch
    
  7. 移动资源的帐户至少需要具备下列权限:The account moving the resources must have at least the following permissions:

    • 源资源组上的 Microsoft.Resources/subscriptions/resourceGroups/moveResources/action 权限。Microsoft.Resources/subscriptions/resourceGroups/moveResources/action on the source resource group.
    • 目标资源组上的 Microsoft.Resources/subscriptions/resourceGroups/write 权限。Microsoft.Resources/subscriptions/resourceGroups/write on the destination resource group.
  8. 在移动资源之前,请检查要将资源移动到的订阅的订阅配额。Before moving the resources, check the subscription quotas for the subscription you're moving the resources to. 如果移动资源意味着订阅将超出其限制,则需要检查是否可以请求增加配额。If moving the resources means the subscription will exceed its limits, you need to review whether you can request an increase in the quota. 有关限制的列表及如何请求增加配额的信息,请参阅 Azure 订阅和服务限制、配额与约束For a list of limits and how to request an increase, see Azure subscription and service limits, quotas, and constraints.

  9. 若要跨订阅移动,则资源及其依赖资源必须位于同一资源组中,并且必须一起移动。For a move across subscriptions, the resource and its dependent resources must be located in the same resource group and they must be moved together. 例如,如果 VM 带有托管磁盘,则 VM 和托管磁盘以及其他依赖资源必须一起移动。For example, a VM with managed disks would require the VM and the managed disks to be moved together, along with other dependent resources.

    如果要将某个资源移到新的订阅,请进行检查,看看该资源是否有任何依赖资源,以及它们是否位于同一资源组中。If you're moving a resource to a new subscription, check to see whether the resource has any dependent resources, and whether they're located in the same resource group. 如果这些资源不在同一资源组中,请看看能否将这些资源组合到同一资源组中。If the resources aren't in the same resource group, check to see whether the resources can be combined into the same resource group. 如果可以,请跨资源组使用一个移动操作,将所有这些资源并入同一资源组。If so, bring all these resources into the same resource group by using a move operation across resource groups.

    有关详细信息,请参阅跨订阅移动方案For more information, see Scenario for move across subscriptions.

跨订阅移动方案Scenario for move across subscriptions

将资源从一个订阅移到另一个订阅分为三步:Moving resources from one subscription to another is a three-step process:

跨订阅移动方案

为了演示方便,我们只有一个依赖资源。For illustration purposes, we have only one dependent resource.

  • 步骤 1:如果依赖资源分布在不同的资源组中,请先将它们移到一个资源组。Step 1: If dependent resources are distributed across different resource groups, first move them into one resource group.
  • 步骤 2:将资源和依赖资源一起从源订阅移到目标订阅。Step 2: Move the resource and dependent resources together from the source subscription to the target subscription.
  • 步骤 3:也可将依赖资源重新分布到目标订阅中的不同资源组。Step 3: Optionally, redistribute the dependent resources to different resource groups within the target subscription.

验证移动Validate move

验证移动操作可以测试你的移动方案而无需实际移动资源。The validate move operation lets you test your move scenario without actually moving the resources. 使用此操作检查移动是否会成功。Use this operation to check if the move will succeed. 发送移动请求时会自动调用验证。Validation is automatically called when you send a move request. 仅当需要预先确定结果时才使用此操作。Use this operation only when you need to predetermine the results. 若要运行此操作,需要:To run this operation, you need the:

  • 源资源组的名称name of the source resource group
  • 目标资源组的资源 IDresource ID of the target resource group
  • 要移动的每个资源的资源 IDresource ID of each resource to move
  • 你的帐户的访问令牌the access token for your account

发送以下请求:Send the following request:

POST https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<source-group>/validateMoveResources?api-version=2019-05-10
Authorization: Bearer <access-token>
Content-type: application/json

包含请求正文:With a request body:

{
 "resources": ["<resource-id-1>", "<resource-id-2>"],
 "targetResourceGroup": "/subscriptions/<subscription-id>/resourceGroups/<target-group>"
}

如果请求格式正确,则操作将返回:If the request is formatted correctly, the operation returns:

Response Code: 202
cache-control: no-cache
pragma: no-cache
expires: -1
location: https://management.chinacloudapi.cn/subscriptions/<subscription-id>/operationresults/<operation-id>?api-version=2018-02-01
retry-after: 15
...

202 状态代码指示已接受验证请求,但尚未确定移动操作是否会成功。The 202 status code indicates the validation request was accepted, but it hasn't yet determined if the move operation will succeed. location 值包含用于检查长时间运行操作的状态的 URL。The location value contains a URL that you use to check the status of the long-running operation.

若要检查状态,请发送以下请求:To check the status, send the following request:

GET <location-url>
Authorization: Bearer <access-token>

操作仍在运行时,会继续收到 202 状态代码。While the operation is still running, you continue to receive the 202 status code. 请等待 retry-after 值中所示的秒数,然后重试。Wait the number of seconds indicated in the retry-after value before trying again. 如果移动操作验证成功,则会收到 204 状态代码。If the move operation validates successfully, you receive the 204 status code. 如果移动验证失败,则会收到错误消息,例如:If the move validation fails, you receive an error message, such as:

{"error":{"code":"ResourceMoveProviderValidationFailed","message":"<message>"...}}

使用门户Use the portal

若要移动资源,请选择包含那些资源的资源组。To move resources, select the resource group that contains those resources.

当你查看资源组时,移动选项处于禁用状态。When you view the resource group, the move option is disabled.

移动选项处于禁用状态

若要启用移动选项,请选择要移动的资源。To enable the move option, select the resources you want to move. 若要选择所有资源,请选中列表顶部的复选框。To select all of the resources, select the checkbox at the top of list. 或者,分别选择各个资源。Or, select resources individually. 选择资源后,将启用移动选项。After selecting resources, the move option is enabled.

选择资源

选择“移动”按钮Select the Move button.

移动选项

此按钮提供了三个选项:This button gives you three options:

选择是要将资源移到新资源组还是新订阅。Select whether you're moving the resources to a new resource group or a new subscription.

选择目标资源组。Select the destination resource group. 确认需要更新这些资源的脚本,选择“确定” 。Acknowledge that you need to update scripts for these resources and select OK. 如果你选择了移动到新的订阅,则还必须选择目标订阅。If you selected to move to a new subscription, you must also select the destination subscription.

选择目标

验证资源是否可以移动后,你会看到一个通知,指示移动操作正在运行。After validating that the resources can be moved, you see a notification that the move operation is running.

通知

操作完成后,你会获得结果通知。When it has completed, you're notified of the result.

使用 Azure PowerShellUse Azure PowerShell

要将现有资源移到另一个资源组或订阅,请使用 Move-AzResource 命令。To move existing resources to another resource group or subscription, use the Move-AzResource command. 下面的示例演示了如何将多个资源移动到新的资源组。The following example shows how to move several resources to a new resource group.

$webapp = Get-AzResource -ResourceGroupName OldRG -ResourceName ExampleSite
$plan = Get-AzResource -ResourceGroupName OldRG -ResourceName ExamplePlan
Move-AzResource -DestinationResourceGroupName NewRG -ResourceId $webapp.ResourceId, $plan.ResourceId

若要移到新订阅,请包含 DestinationSubscriptionId 参数的值。To move to a new subscription, include a value for the DestinationSubscriptionId parameter.

使用 Azure CLIUse Azure CLI

若要将现有资源移动到另一个资源组或订阅,请使用 az resource move 命令。To move existing resources to another resource group or subscription, use the az resource move command. 提供要移动的资源的资源 ID。Provide the resource IDs of the resources to move. 下面的示例演示了如何将多个资源移动到新的资源组。The following example shows how to move several resources to a new resource group. --ids 参数中,提供要移动的资源 ID 的空格分隔列表。In the --ids parameter, provide a space-separated list of the resource IDs to move.

webapp=$(az resource show -g OldRG -n ExampleSite --resource-type "Microsoft.Web/sites" --query id --output tsv)
plan=$(az resource show -g OldRG -n ExamplePlan --resource-type "Microsoft.Web/serverfarms" --query id --output tsv)
az resource move --destination-group newgroup --ids $webapp $plan

若要移到新订阅,请提供 --destination-subscription-id 参数。To move to a new subscription, provide the --destination-subscription-id parameter.

使用 REST APIUse REST API

要将现有资源移到另一个资源组或订阅,请使用移动资源操作。To move existing resources to another resource group or subscription, use the Move resources operation.

POST https://management.chinacloudapi.cn/subscriptions/{source-subscription-id}/resourcegroups/{source-resource-group-name}/moveResources?api-version={api-version}

在请求正文中,指定目标资源组和要移动的资源。In the request body, you specify the target resource group and the resources to move.

{
 "resources": ["<resource-id-1>", "<resource-id-2>"],
 "targetResourceGroup": "/subscriptions/<subscription-id>/resourceGroups/<target-group>"
}

常见问题Frequently asked questions

问:我的资源移动操作通常只需几分钟的时间,但这次已运行了几乎一个小时。出现了错误?Question: My resource move operation, which usually takes a few minutes, has been running for almost an hour. Is there something wrong?

移动资源是一项复杂的操作,包含不同的阶段。Moving a resource is a complex operation that has different phases. 它不仅仅涉及你尝试移动的资源的资源提供程序。It can involve more than just the resource provider of the resource you're trying to move. 由于资源提供程序之间的依赖关系,Azure 资源管理器允许在 4 小时内完成操作。Because of the dependencies between resource providers, Azure Resource Manager allows 4 hours for the operation to complete. 此时间段为资源提供程序提供了从暂时性问题中恢复的机会。This time period gives resource providers a chance to recover from transient issues. 如果你的移动请求还在四小时期间内,则操作会继续尝试完成,并且仍可能会成功。If your move request is within the four-hour period, the operation keeps trying to complete and may still succeed. 源和目标资源组在这段时间内被锁定,以避免出现一致性问题。The source and destination resource groups are locked during this time to avoid consistency issues.

问:为什么我的资源组在资源移动期间锁定了四个小时?Question: Why is my resource group locked for four hours during resource move?

最多允许移动请求花费四个小时来完成。A move request is allowed a maximum of four hours to complete. 为了防止对正在移动的资源进行修改,在资源移动期间,源资源组和目标资源组都会被锁定。To prevent modifications on the resources being moved, both the source and destination resource groups are locked during the resource move.

移动请求中有两个阶段。There are two phases in a move request. 在第一阶段中,将移动资源。In the first phase, the resource is moved. 在第二阶段中,将向依赖于被移动资源的其他资源提供程序发送通知。In the second phase, notifications are sent to other resource providers that are dependent on the resource being moved. 如果某个资源提供程序在任一阶段失败,则资源组可能会被锁定整整四个小时。A resource group can be locked for the entire four hours when a resource provider fails either phase. 在允许的时间内,资源管理器将重试失败的步骤。During the allowed time, Resource Manager retries the failed step.

如果资源无法在四小时内移动,则资源管理器会解除对这两个资源组的锁定。If a resource can't be moved within four hours, Resource Manager unlocks both resource groups. 已成功移动的资源位于目标资源组中。Resources that were successfully moved are in the destination resource group. 未能移动的资源留在源资源组中。Resources that failed to move are left the source resource group.

问:资源移动过程中锁定的源和目标资源组有哪些影响?Question: What are the implications of the source and destination resource groups being locked during the resource move?

锁定会阻止你删除任一资源组,阻止在任一资源组中创建新资源或删除移动中涉及的任何资源。The lock prevents you from deleting either resource group, creating a new resource in either resource group, or deleting any of the resources involved in the move.

下图显示了当用户尝试删除正在移动的资源组时来自 Azure 门户的错误消息。The following image shows an error message from the Azure portal when a user tries to delete a resource group that is part of an ongoing move.

移动错误消息“正在尝试删除”

问:错误代码“MissingMoveDependentResources”的含义是什么?Question: What does the error code "MissingMoveDependentResources" mean?

移动资源时,依赖的资源必须存在于目标资源组或订阅中,或者包含在移动请求中。When moving a resource, its dependent resources must either exist in the destination resource group or subscription, or be included in the move request. 当依赖的资源不满足此要求时,你将收到 MissingMoveDependentResources 错误代码。You get the MissingMoveDependentResources error code when a dependent resource doesn't meet this requirement. 错误消息包含有关需要在移动请求中包括的依赖资源的详细信息。The error message has details about the dependent resource that needs to be included in the move request.

例如,移动某个虚拟机可能需要移动具有三个不同资源提供程序的七个资源类型。For example, moving a virtual machine could require moving seven resource types with three different resource providers. 这些资源提供程序和类型为:Those resource providers and types are:

  • Microsoft.ComputeMicrosoft.Compute

    • virtualMachinesvirtualMachines
    • disksdisks
  • Microsoft.NetworkMicrosoft.Network

    • networkInterfacesnetworkInterfaces
    • publicIPAddressespublicIPAddresses
    • networkSecurityGroupsnetworkSecurityGroups
    • virtualNetworksvirtualNetworks
  • Microsoft.StorageMicrosoft.Storage

    • storageAccountsstorageAccounts

另一个常见示例涉及移动虚拟网络。Another common example involves moving a virtual network. 可能必须移动与该虚拟网络关联的多个其他资源。You may have to move several other resources associated with that virtual network. 移动请求可能要求移动公共 IP 地址、路由表、虚拟网络网关、网络安全组和其他项。The move request could require moving public IP addresses, route tables, virtual network gateways, network security groups, and others.

问:为何无法移动 Azure 中的某个资源?Question: Why can't I move some resources in Azure?

目前,并非 Azure 中的所有资源都支持移动。Currently, not all resources in Azure support move. 有关支持移动的资源列表,请参阅资源的移动操作支持For a list of resources that support move, see Move operation support for resources.

问:可以在单个操作中移动多少资源?Question: How many resources can I move in a single operation?

在可能的情况下,将大型移动分为单独的移动操作。When possible, break large moves into separate move operations. 在一次操作中有 800 多项资源时,资源管理器会立即返回错误。Resource Manager immediately returns an error when there are more than 800 resources in a single operation. 但是,移动 800 项以下的资源也可能因超时而失败。However, moving less than 800 resources may also fail by timing out.

问:“资源未处于成功状态”此错误的意思是什么?Question: What is the meaning of the error that a resource isn't in succeeded state?

如果收到一条错误消息,指出由于资源未处于成功状态而无法移动资源,则它可能实际上是阻止移动的依赖资源。When you get an error message that indicates a resource can't be moved because it isn't in a succeeded state, it may actually be a dependent resource that is blocking the move. 通常,错误代码为 MoveCannotProceedWithResourcesNotInSucceededStateTypically, the error code is MoveCannotProceedWithResourcesNotInSucceededState.

如果源或目标资源组包含虚拟网络,则会在移动过程中检查虚拟网络的所有依赖资源的状态。If the source or target resource group contains a virtual network, the states of all dependent resources for the virtual network are checked during the move. 该检查包括直接和间接依赖于虚拟网络的这些资源。The check includes those resources directly and indirectly dependent on the virtual network. 如果这些资源中有任何资源处于故障状态,则会阻止移动。If any of those resources are in a failed state, the move is blocked. 例如,如果某个使用虚拟网络的虚拟机故障,则会阻止移动。For example, if a virtual machine that uses the virtual network has failed, the move is blocked. 即使该虚拟机不是要移动的资源之一,也不在要移动的资源组之一中,系统也会阻止移动。The move is blocked even when the virtual machine isn't one of the resources being moved and isn't in one of the resource groups for the move.

收到此错误时,你有两个选择。When you receive this error, you have two options. 将资源移到没有虚拟网络的资源组,或联系支持人员Either move your resources to a resource group that doesn't have a virtual network, or contact support.

后续步骤Next steps

有关支持移动的资源列表,请参阅资源的移动操作支持For a list of which resources support move, see Move operation support for resources.