本快速入門說明如何使用 Terraform 範本來設定適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器的備份。
Azure 備份 可讓您使用多個用戶端備份 Azure PostgreSQL - 彈性伺服器,例如 Azure 入口網站、PowerShell、CLI、Azure Resource Manager、Bicep 等。
先決條件
設定適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器的備份之前,請確定符合下列必要條件:
您需要具有有效訂用帳戶的 Azure 帳戶。 如果您沒有帳戶,可免費建立帳戶。
安裝及設定 Terraform。
登入您的 Azure 帳戶,並 向 Azure 進行驗證。
注意
Terraform 只能支援使用 Azure CLI 向 Azure 進行驗證。 不支援使用 Azure PowerShell 進行驗證。 因此,雖然您可以在執行 Terraform 工作時使用 Azure PowerShell 模組,但您必須先向 Azure 進行驗證。
實作 Terraform 程式碼
注意
建立可用來測試範例 Terraform 程式碼的目錄,並將其設為您目前的目錄。
建立名為
providers.tf的檔案,並插入下列程式碼:terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "3.99.0" } } } provider "azurerm" { features {} subscription_id = "<azure_subscription_id>" tenant_id = "<azure_subscription_tenant_id>" }建立名為
main.tf的檔案,並插入下列程式碼:# Step 1: Create the Backup Vault resource "azurerm_data_protection_backup_vault" "backup_vault" { name = var.backup_vault_name resource_group_name = var.backup_vault_resource_group location = var.region identity { type = "SystemAssigned" } storage_settings { datastore_type = "VaultStore" type = "LocallyRedundant" } } # Step 2: Create Backup Policy for PostgreSQL resource "azurerm_data_protection_backup_policy" "postgresql_backup_policy" { name = var.policy_name resource_group_name = var.backup_vault_resource_group vault_name = azurerm_data_protection_backup_vault.backup_vault.name rule { name = "BackupSchedule" backup_parameters { object_type = "AzureBackupParams" } trigger { schedule { recurrence_rule { frequency = "Weekly" interval = var.backup_schedule_frequency } } } data_store { datastore_type = "VaultStore" } } retention_rule { name = "RetentionRule" is_default = true lifecycle { delete_after { object_type = "AbsoluteDeleteOption" duration = format("P%dM", var.retention_duration_in_months) } } } depends_on = [ azurerm_data_protection_backup_vault.backup_vault ] } # Step 3: Role Assignment for PostgreSQL Flexible Server Long Term Retention Backup Role data "azurerm_postgresql_flexible_server" "postgresql_server" { name = var.postgresql_server_name resource_group_name = var.postgresql_resource_group } resource "azurerm_role_assignment" "backup_role" { principal_id = azurerm_data_protection_backup_vault.backup_vault.identity[0].principal_id role_definition_name = "PostgreSQL Flexible Server Long Term Retention Backup Role" scope = data.azurerm_postgresql_flexible_server.PostgreSQL_server.id depends_on = [ azurerm_data_protection_backup_policy.postgresql_backup_policy ] } # Step 4: Role Assignment for Reader on Resource Group data "azurerm_resource_group" "postgresql_resource_group" { name = var.postgresql_resource_group } resource "azurerm_role_assignment" "reader_role" { principal_id = azurerm_data_protection_backup_vault.backup_vault.identity[0].principal_id role_definition_name = "Reader" scope = data.azurerm_resource_group.postgresql_resource_group.id depends_on = [ azurerm_role_assignment.backup_role ] } # Step 5: Create Backup Instance for PostgreSQL resource "azurerm_data_protection_backup_instance" "postgresql_backup_instance" { name = "PostgreSQLBackupInstance" resource_group_name = var.backup_vault_resource_group vault_name = azurerm_data_protection_backup_vault.backup_vault.name location = var.region datasource { object_type = "Datasource" datasource_type = "AzureDatabaseForPostgreSQLFlexibleServer" resource_id = data.azurerm_PostgreSQL_flexible_server.postgresql_server.id } policy_id = azurerm_data_protection_backup_policy.postgresql_backup_policy.id depends_on = [ azurerm_role_assignment.reader_role ] }建立名為
variables.tf的檔案,並插入下列程式碼:
variable "backup_vault_name" {
type = string
default = "BackupVaultTF"
description = "Name of the Backup Vault"
}
variable "backup_vault_resource_group" {
type = string
default = "Contoso_TF_RG"
description = "Name of the resource group to which backup vault belongs to"
}
variable "postgresql_server_name" {
type = string
default = "Contoso_PostgreSQL_TF"
description = "Name of the PostgreSQL server"
}
variable "postgresql_resource_group" {
type = string
default = "Contoso_TF_RG"
description = "Name of the resource group to which PostgreSQL server belongs to"
}
variable "region" {
type = string
default = "eastus"
description = "Location of the PostgreSQL server"
}
variable "policy_name" {
type = string
default = "PostgreSQLbackuppolicytfv1"
description = "Name of the backup policy"
}
variable "backup_schedule_frequency" {
type = string
default = "1"
description = "Schedule frequency for backup"
}
variable "retention_duration_in_months" {
type = string
default = "3"
description = "Retention duration for backup in month"
}
初始化 Terraform
執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點︰
-
-upgrade參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。
建立 Terraform 執行計畫
執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點︰
-
terraform plan命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。 - 選用的
-out參數可讓您指定計畫的輸出檔。 使用-out參數可確保您所檢閱的方案就是所套用的方案。
套用 Terraform 執行計畫
執行 terraform apply 將執行計畫套用至您的雲端基礎結構。
terraform apply main.tfplan
重點︰
- 範例
terraform apply命令假設您之前已執行過terraform plan -out main.tfplan。 - 如果您為
-out參數指定了不同的檔案名稱,請在呼叫terraform apply時使用該檔案名稱。 - 若您未使用
-out參數,請呼叫terraform apply,不需要使用參數。
對 Azure 上的 Terraform 進行疑難排解
針對在 Azure 上使用 Terraform 時的常見問題進行疑難排解。