使用 PowerShell 克隆 Azure App Service 应用Azure App Service App Cloning Using PowerShell

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

在 Microsoft 发行的 Azure PowerShell 版本 1.1.0 中,为 New-AzWebApp 添加了新选项,可让用户将现有“应用服务”应用克隆到不同区域或相同区域中的新建应用。With the release of Microsoft Azure PowerShell version 1.1.0, a new option has been added to New-AzWebApp that lets you clone an existing App Service app to a newly created app in a different region or in the same region. 使用此选项,客户可跨不同区域快速轻松地部署多个应用。This option enables customers to deploy a number of apps across different regions quickly and easily.

标准、高级、高级 V2 和独立应用服务计划支持应用克隆。App cloning is supported for Standard, Premium, Premium V2, and Isolated app service plans. 新功能使用与应用服务备份功能相同的限制,具体请参阅在 Azure 应用服务中备份应用The new feature uses the same limitations as App Service Backup feature, see Back up an app in Azure App Service.

克隆现有应用Cloning an existing app

方案:你想要将位于中国东部区域的现有应用的内容克隆到位于中国北部区域的新应用。Scenario: An existing app in China East region, and you want to clone the contents to a new app in China North region. 结合 -SourceWebApp 选项使用 Azure 资源管理器版本的 PowerShell cmdlet 来创建新的应用,即可实现此目的。It can be accomplished by using the Azure Resource Manager version of the PowerShell cmdlet to create a new app with the -SourceWebApp option.

如果知道包含源应用的资源组名称,就可以使用以下 PowerShell 命令来获取源应用的信息(在本例中,该应用名为 source-webapp):Knowing the resource group name that contains the source app, you can use the following PowerShell command to get the source app's information (in this case named source-webapp):

$srcapp = Get-AzWebApp -ResourceGroupName SourceAzureResourceGroup -Name source-webapp

要创建新的应用服务计划,可按以下示例中所示来使用 New-AzAppServicePlan 命令To create a new App Service Plan, you can use New-AzAppServicePlan command as in the following example

    New-AzAppServicePlan -Location "China East" -ResourceGroupName DestinationAzureResourceGroup -Name DestinationAppServicePlan -Tier Standard

使用 New-AzWebApp 命令,可在中国北部区域中新建应用,并将其绑定到现有应用服务计划。Using the New-AzWebApp command, you can create the new app in the China North region, and tie it to an existing App Service Plan. 此外,还可以使用相同的资源组作为源应用,或定义新的资源组,如以下命令所示:Moreover,you can use the same resource group as the source app, or define a new resource group, as shown in the following command:

    $destapp = New-AzWebApp -ResourceGroupName DestinationAzureResourceGroup -Name dest-webapp -Location "China North" -AppServicePlan DestinationAppServicePlan -SourceWebApp $srcapp

要克隆现有应用(包括所有关联的部署槽位),需要使用 IncludeSourceWebAppSlots 参数。To clone an existing app including all associated deployment slots, you need to use the IncludeSourceWebAppSlots parameter. 以下 PowerShell 命令演示如何在 New-AzWebApp 命令中使用该参数:The following PowerShell command demonstrates the use of that parameter with the New-AzWebApp command:

    $destapp = New-AzWebApp -ResourceGroupName DestinationAzureResourceGroup -Name dest-webapp -Location "China North" -AppServicePlan DestinationAppServicePlan -SourceWebApp $srcapp -IncludeSourceWebAppSlots

要在同一区域中克隆现有应用,需要在同一区域中创建新资源组和新的应用服务计划,然后使用以下 PowerShell 命令来克隆应用:To clone an existing app within the same region, you need to create a new resource group and a new app service plan in the same region, and then use the following PowerShell command to clone the app:

    $destapp = New-AzWebApp -ResourceGroupName NewAzureResourceGroup -Name dest-webapp -Location "China East" -AppServicePlan NewAppServicePlan -SourceWebApp $srcapp

克隆现有的应用槽Cloning an existing App Slot

方案:希望将应用的现有部署槽位克隆到新应用或新槽位。Scenario: You want to clone an existing deployment slot of an app to either a new app or a new slot. 新的应用可与原始应用槽位于相同或不同的区域。The new app can be in the same region as the original app slot or in a different region.

如果知道包含源应用的资源组名称,就可以使用以下 PowerShell 命令来获取与 source-app 相关的源应用的槽位信息(在本例中,该应用名为 source-appslot):Knowing the resource group name that contains the source app, you can use the following PowerShell command to get the source app slot's information (in this case named source-appslot) tied to source-app:

$srcappslot = Get-AzWebAppSlot -ResourceGroupName SourceAzureResourceGroup -Name source-app -Slot source-appslot

以下命令演示如何在新应用中创建源应用的克隆:The following command demonstrates creating a clone of the source app to a new app:

    $destapp = New-AzWebApp -ResourceGroupName DestinationAzureResourceGroup -Name dest-app -Location "China North" -AppServicePlan DestinationAppServicePlan -SourceWebApp $srcappslot

在克隆应用时配置流量管理器Configuring Traffic Manager while cloning an app

通过创建多区域应用和配置 Azure 流量管理器来将流量路由到所有这些应用是一个重要的方案,可确保客户应用的高可用性。Creating multi-region apps and configuring Azure Traffic Manager to route traffic to all these apps, is an important scenario to ensure that customers' apps are highly available. 克隆现有应用时,可以选择将两个应用都连接到新的或现有的流量管理器配置文件。When cloning an existing app, you have the option to connect both apps to either a new traffic manager profile or an existing one. 仅支持 Azure 资源管理器版本的流量管理器。Only Azure Resource Manager version of Traffic Manager is supported.

在克隆应用时创建新的流量管理器配置文件Creating a new Traffic Manager profile while cloning an app

方案:用户想要将应用克隆到另一个区域,同时配置包含这两个应用的 Azure 资源管理器流量管理器配置文件。Scenario: You want to clone an app to another region, while configuring an Azure Resource Manager traffic manager profile that includes both apps. 以下命令演示如何在新应用中创建源应用的克隆,同时配置新的流量管理器配置文件:The following command demonstrates creating a clone of the source app to a new app while configuring a new Traffic Manager profile:

    $destapp = New-AzWebApp -ResourceGroupName DestinationAzureResourceGroup -Name dest-webapp -Location "China East" -AppServicePlan DestinationAppServicePlan -SourceWebApp $srcapp -TrafficManagerProfileName newTrafficManagerProfile

将新克隆的应用添加到现有的流量管理器配置文件Adding new cloned app to an existing Traffic Manager profile

方案:用户已有一个 Azure 资源管理器流量管理器配置文件,现在想要将两个应用都添加为终结点。Scenario: You already have an Azure Resource Manager traffic manager profile and want to add both apps as endpoints. 要执行此操作,首先需要组合现有流量管理器配置文件 ID。To do so, you first need to assemble the existing traffic manager profile ID. 需要订阅 ID、资源组名称和现有流量管理器配置文件名称。You need the subscription ID, the resource group name, and the existing traffic manager profile name.

$TMProfileID = "/subscriptions/<Your subscription ID goes here>/resourceGroups/<Your resource group name goes here>/providers/Microsoft.TrafficManagerProfiles/ExistingTrafficManagerProfileName"

获取流量管理器 ID 之后,根据以下命令演示在新应用中创建源应用的克隆,同时将它们添加到现有流量管理器配置文件:After having the traffic manger ID, the following command demonstrates creating a clone of the source app to a new app while adding them to an existing Traffic Manager profile:

    $destapp = New-AzWebApp -ResourceGroupName <Resource group name> -Name dest-webapp -Location "China East" -AppServicePlan DestinationAppServicePlan -SourceWebApp $srcapp -TrafficManagerProfileId $TMProfileID

当前限制Current Restrictions

下面是应用克隆的已知限制:Here are the known restrictions of app cloning:

  • 不会克隆自动缩放设置Auto scale settings are not cloned
  • 不会克隆备份计划设置Backup schedule settings are not cloned
  • 不会克隆 VNET 设置VNET settings are not cloned
  • 不会自动在目标应用上设置 App InsightsApp Insights are not automatically set up on the destination app
  • 不会克隆简易身份验证设置Easy Auth settings are not cloned
  • 不会克隆 Kudu 扩展Kudu Extension are not cloned
  • 不会克隆 TiP 规则TiP rules are not cloned
  • 不会克隆数据库内容Database content is not cloned
  • 如果克隆到不同的缩放单元,出站 IP 地址会更改Outbound IP Addresses changes if cloning to a different scale unit
  • 不适用于 Linux 应用Not available for Linux Apps

参考References