Memulihkan aplikasi web dari cadangan di langganan lain menggunakan PowerShell

Contoh skrip ini mengambil cadangan yang telah selesai sebelumnya dari aplikasi web yang ada dan memulihkannya ke aplikasi web di langganan lain.

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.

# 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

Menghapus penyebaran

Jika Anda tidak memerlukan aplikasi web lagi, gunakan perintah berikut untuk menghapus grup sumber daya, aplikasi web, dan semua sumber daya terkait.

Remove-AzResourceGroup -Name $resourceGroupName -Force

Penjelasan skrip

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

Perintah Catatan
Add-AzAccount Menambahkan akun terautentikasi untuk digunakan untuk permintaan cmdlet Azure Resource Manager.
Get-AzWebAppBackupList Mendapatkan daftar cadangan untuk aplikasi web.
Get-AzWebAppBackup Mendapatkan cadangan untuk aplikasi web menggunakan ID cadangan.
Get-AzWebAppBackupConfiguration Mendapatkan konfigurasi cadangan untuk aplikasi web.
New-AzWebApp Membuat aplikasi web
Restore-AzWebAppBackup Memulihkan aplikasi web dari cadangan yang telah selesai sebelumnya.

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.