使用 PowerShell 从另一订阅中的备份还原 Web 应用Restore a web app from a backup in another subscription using PowerShell

此示例脚本从现有的 Web 应用中检索以前已完成的备份,然后将其还原到另一订阅中的 Web 应用。This sample script retrieves a previously completed backup from an existing web app and restores it to a web app in another subscription.

必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell,并运行 Connect-AzAccount -Environment AzureChinaCloud 创建与 Azure 的连接。If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

示例脚本Sample script

备注

本文已经过更新,以便使用 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.

$resourceGroupNameSub1 = "<replace-with-your-group-name>"
$resourceGroupNameSub2 = "<replace-with-desired-new-group-name>"
$webAppNameSub1 = "<replace-with-your-app-name>"
$webAppNameSub2 = "<replace-with-desired-new-app-name>"
$appServicePlanSub2 = "<replace-with-desired-new-plan-name>"
$locationSub2 = "China North"


# Log into the subscription with the backup
Add-AzAccount

# List statuses of all backups that are complete or currently executing.
Get-AzWebAppBackupList -ResourceGroupName $resourceGroupNameSub1 -Name $webAppNameSub1

# Note the BackupID property of the backup you want to restore

# Get the backup object that you want to restore by specifying the BackupID
$backup = (Get-AzWebAppBackupList -ResourceGroupName $resourceGroupNameSub1 -Name $webAppNameSub1 | where {$_.BackupId -eq <replace-with-BackupID>}) 

# Log into the subscription that you want to restore the app to
Add-AzAccount

# Create a new web app
New-AzWebApp -ResourceGroupName $resourceGroupNameSub2 -AppServicePlan $appServicePlanSub2 -Name $webAppNameSub2 -Location $locationSub2

# Restore the app by overwriting it with the backup data
Restore-AzWebAppBackup -ResourceGroupName $resourceGroupNameSub2 -Name $webAppNameSub2 -StorageAccountUrl $backup.StorageAccountUrl -BlobName $backup.BlobName -Overwrite

清理部署Clean up deployment

如果不再需要 Web 应用,请使用以下命令删除资源组、Web 应用和所有相关的资源。If you don't need the web app anymore, use the following command to remove the resource group, web app, and all related resources.

Remove-AzResourceGroup -Name $resourceGroupName -Force

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

CommandCommand 说明Notes
Add-AzAccountAdd-AzAccount 添加一个经身份验证的帐户,用于 Azure 资源管理器 cmdlet 请求。Adds an authenticated account to use for Azure Resource Manager cmdlet requests.
Get-AzWebAppBackupListGet-AzWebAppBackupList 获取 Web 应用的备份列表。Gets a list of backups for a web app.
New-AzWebAppNew-AzWebApp 创建 Web 应用Creates a web app
Restore-AzWebAppBackupRestore-AzWebAppBackup 从以前完成的备份中还原 Web 应用。Restores a web app from a previously completed backup.

后续步骤Next steps

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.

可以在 Azure PowerShell 示例中找到 Azure 应用服务 Web 应用的其他 Azure Powershell 示例。Additional Azure Powershell samples for Azure App Service Web Apps can be found in the Azure PowerShell samples.