Säkerhetskopiera en webbapp med PowerShell

Det här exempelskriptet skapar en webbapp i App Service med dess relaterade resurser, och skapar sedan en tidsbegränsad säkerhetskopia för webbappen.

Om det behövs installerar du Azure PowerShell med hjälp av instruktionen i Azure PowerShell-guiden och kör Connect-AzAccount sedan för att skapa en anslutning till Azure.

Exempelskript

Anteckning

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Se Installera Azure PowerShell för att komma igång. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till 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

Rensa distribution

När skriptet har körts kan följande kommando användas för att ta bort resursgruppen, webbappen och alla relaterade resurser.

Remove-AzResourceGroup -Name myResourceGroup -Force

Förklaring av skript

Det här skriptet använder följande kommandon. Varje kommando i tabellen länkar till kommandospecifik dokumentation.

Kommando Kommentarer
New-AzResourceGroup Skapar en resursgrupp där alla resurser lagras.
New-AzStorageAccount Skapar ett lagringskonto.
New-AzStorageContainer Skapar en Azure-lagringscontainer.
New-AzStorageContainerSASToken Genererar en SAS-token för en Azure-lagringscontainer.
New-AzAppServicePlan Skapar en App Service-plan.
New-AzWebApp Skapar en webbapp.
New-AzWebAppBackup Skapar en säkerhetskopia för en webbapp.
Get-AzWebAppBackupList Hämtar en lista över säkerhetskopior för en webbapp.

Nästa steg

Mer information om Azure PowerShell-modulen finns i Azure PowerShell-dokumentationen.

Ytterligare Azure PowerShell exempel för Azure App Service Web Apps finns i de Azure PowerShell exemplen.