Membuat cadangan terjadwal untuk aplikasi web menggunakan PowerShell

Skrip sampel ini membuat aplikasi web di Azure App Service dengan sumber daya terkaitnya, lalu membuat cadangan terjadwal untuk sumber daya tersebut.

Jika diperlukan, pasang Azure PowerShell menggunakan instruksi yang ditemukan di panduan Azure PowerShell, lalu jalankan Connect-AzAccount untuk membuat koneksi dengan Azure.

Skrip sampel

Catatan

Kami menyarankan agar Anda menggunakan modul Azure Az PowerShell untuk berinteraksi dengan Azure. Lihat Menginstal Azure PowerShell untuk memulai. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

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

# 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 1 year.
# 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).AddYears(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

# Schedule a backup every day, beginning in one hour, and retain for 10 days
Edit-AzWebAppBackupConfiguration -ResourceGroupName myResourceGroup -Name $webappname `
-StorageAccountUrl $sasUrl -FrequencyInterval 1 -FrequencyUnit Day -KeepAtLeastOneBackup `
-StartTime (Get-Date).AddHours(1) -RetentionPeriodInDays 10

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

# (OPTIONAL) Change the backup schedule to every 2 days
$configuration = Get-AzWebAppBackupConfiguration -ResourceGroupName myResourceGroup `
-Name $webappname
$configuration.FrequencyInterval = 2
$configuration | Edit-AzWebAppBackupConfiguration

Menghapus penyebaran

Setelah sampel skrip dijalankan, perintah berikut ini dapat digunakan untuk menghapus grup sumber daya, aplikasi web, dan semua sumber daya yang terkait.

Remove-AzResourceGroup -Name myResourceGroup -Force

Penjelasan skrip

Skrip ini menggunakan perintah berikut. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik perintah.

Perintah Catatan
Baru-AzResourceGroup Membuat grup sumber daya tempat semua sumber daya disimpan.
New-AzStorageAccount Membuat akun penyimpanan.
New-AzStorageContainer Membuat kontainer penyimpanan Azure.
New-AzStorageContainerSASToken Membuat token SAS untuk kontainer penyimpanan Azure.
New-AzAppServicePlan Buat paket App Service.
New-AzWebApp Membuat aplikasi web.
Edit-AzWebAppBackupConfiguration Mengedit konfigurasi cadangan untuk aplikasi web.
Get-AzWebAppBackupList Mendapatkan daftar cadangan untuk aplikasi web.
Get-AzWebAppBackupConfiguration Mengedit konfigurasi cadangan untuk aplikasi web.

Langkah berikutnya

Untuk mengetahui informasi selengkapnya tentang modul Azure PowerShell, lihat dokumentasi Azure PowerShell.

Sampel Azure Powershell tambahan untuk Azure App Service Web Apps dapat ditemukan dalam sampel Azure PowerShell.