将资源移到新的资源组或订阅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, but it doesn't mean the resources are frozen. 例如,如果将 SQL Server 及其数据库移到新的资源组中,使用数据库的应用程序体验不到停机,For example, if you move a SQL Server and its database to a new resource group, an application that uses the database experiences no downtime. 仍可读取和写入到数据库。It can still read and write to the database.

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

移动资源前需查看的清单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've moving any of the following services, check that guidance before moving.

  3. 源订阅和目标订阅必须处于活动状态。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 for the issue type.

  4. 源订阅与目标订阅必须在同一个 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:

  5. 必须针对要移动的资源的资源提供程序注册目标订阅。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
    
  6. 移动资源的帐户至少需要具备下列权限: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.
  7. 在移动资源之前,请检查要将资源移动到的订阅的订阅配额。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.

  8. 若要跨订阅移动,则资源及其依赖资源必须位于同一资源组中,并且必须一起移动。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 consolidated 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 with those resources, and then select the Move button.

移动资源

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

选择要移动的资源和目标资源组。Select the resources to move and the destination resource group. 确认需要更新这些资源的脚本,选择“确定” 。Acknowledge that you need to update scripts for these resources and select OK. 如果在上一步中已选择“编辑订阅”图标,则还必须选择目标订阅。If you selected the edit subscription icon in the previous step, you must also select the destination subscription.

选择目标

在“通知” 中,可以看到移动操作正在运行。In Notifications, you see that the move operation is running.

显示移动状态

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

显示移动结果

如果出现错误,请参阅排查将 Azure 资源移到新的资源组或订阅时遇到的问题If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.

使用 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 资源移到新的资源组或订阅时遇到的问题If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.

使用 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.

如果出现错误,请参阅排查将 Azure 资源移到新的资源组或订阅时遇到的问题If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.

使用 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>"
}

如果出现错误,请参阅排查将 Azure 资源移到新的资源组或订阅时遇到的问题If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.

后续步骤Next steps

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