使用 PowerShell 備份 Web 應用程式

此範例指令碼會在 App Service 中建立 Web 應用程式及其相關資源,然後為其建立一次性的備份。

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

範例指令碼

注意

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

# This sample script creates a web app in App Service with its related resources, and then creates a one-time backup for it.
# 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.

$webappname="mywebapp$(Get-Random -Minimum 100000 -Maximum 999999)"
$storagename="$($webappname)storage"
$container="appbackup"
$location="West Europe"
$backupname="backup1"

# Create a resource group.
New-AzResourceGroup -Name myResourceGroup -Location $location

# Create a storage account.
$storage = New-AzStorageAccount -ResourceGroupName myResourceGroup `
-Name $storagename -SkuName Standard_LRS -Location $location

# Create a storage container.
New-AzStorageContainer -Name $container -Context $storage.Context

# Generates an SAS token for the storage container, valid for one month.
# NOTE: You can use the same SAS token to make backups in Web Apps until -ExpiryTime
$sasUrl = New-AzStorageContainerSASToken -Name $container -Permission rwdl `
-Context $storage.Context -ExpiryTime (Get-Date).AddMonths(1) -FullUri

# Create an App Service plan in Standard tier. Standard tier allows one backup per day.
New-AzAppServicePlan -ResourceGroupName myResourceGroup -Name $webappname `
-Location $location -Tier Standard

# Create a web app.
New-AzWebApp -ResourceGroupName myResourceGroup -Name $webappname `
-Location $location -AppServicePlan $webappname

# Create a one-time backup
New-AzWebAppBackup -ResourceGroupName myResourceGroup -Name $webappname `
-StorageAccountUrl $sasUrl -BackupName $backupname

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

清除部署

在執行過指令碼範例之後,您可以使用下列命令來移除資源群組、Web 應用程式和所有相關資源。

Remove-AzResourceGroup -Name myResourceGroup -Force

指令碼說明

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

Command 注意
New-AzResourceGroup 建立用來存放所有資源的資源群組。
New-AzStorageAccount 建立儲存體帳戶。
New-AzStorageContainer 建立 Azure 儲存體容器。
New-AzStorageContainerSASToken 產生 Azure 儲存體容器的 SAS 權杖。
New-AzAppServicePlan 建立 App Service 方案。
New-AzWebApp 建立 Web 應用程式。
New-AzWebAppBackup 建立 Web 應用程式的備份。
Get-AzWebAppBackupList 取得 Web 應用程式的備份清單。

下一步

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

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