使用 PowerShell 從另一個訂用帳戶中的備份還原 Web 應用程式

此範例指令碼會從現有的 Web 應用程式擷取先前完成的備份,並將它還原到另一個訂用帳戶中的 Web 應用程式。

您可以視需要使用 Azure PowerShell 指南 \(英文\) 中的指示來安裝 Azure PowerShell,然後執行 Connect-AzAccount 來建立與 Azure 的連線。

範例指令碼

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何遷移至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 遷移至 Az。

# This sample script retrieves a previously completed backup from an existing web app and restores it to a web app in another subscription.
# If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount to create a connection with Azure.

$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 = "West Europe"


# 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-AzWebAppBackup -ResourceGroupName $resourceGroupNameSub1 -Name $webAppNameSub1 -BackupId '<replace-with-BackupID>')

# Get the storage account URL of the backup configuration
$url = (Get-AzWebAppBackupConfiguration -ResourceGroupName $resourceGroupNameSub1 -Name $webAppNameSub1).StorageAccountUrl

# 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 $url -BlobName $backup.BlobName -Overwrite

清除部署

如果您不再需要 Web 應用程式,請使用下列命令來移除資源群組、Web 應用程式和所有相關資源。

Remove-AzResourceGroup -Name $resourceGroupName -Force

指令碼說明

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
Add-AzAccount 新增已驗證的帳戶以用於 Azure Resource Manager Cmdlet 要求。
Get-AzWebAppBackupList 取得 Web 應用程式的備份清單。
Get-AzWebAppBackup 使用備份識別碼取得 Web 應用程式的備份。
Get-AzWebAppBackupConfiguration 取得 Web 應用程式的備份組態。
New-AzWebApp 建立 Web 應用程式
Restore-AzWebAppBackup 從先前完成的備份中還原 Web 應用程式。

後續步驟

如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件

您可以在 Azure PowerShell 範例中找到適用於 App Service Web Apps 的其他 Azure PowerShell 範例。