你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Terraform 模板为 Azure Database for PostgreSQL 灵活服务器配置备份

本快速入门介绍如何使用 Terraform 模板为 Azure Database for PostgreSQL 灵活服务器配置备份。

借助 Azure 备份,可以使用多种客户端备份 Azure PostgreSQL - 灵活服务器,这些选项包括 Azure 门户、PowerShell、CLI、Azure 资源管理器、Bicep 等。

先决条件

在为 Azure Database for PostgreSQL 灵活服务器配置备份之前,请确保满足以下先决条件:

  • 需要一个具有活动订阅的 Azure 帐户。 如果没有帐户,请免费创建一个

  • 安装和配置 Terraform

  • 登录到 Azure 帐户并向 Azure 进行身份验证

    注意

    Terraform 仅支持通过 Azure CLI 向 Azure 进行身份验证。 不支持使用 Azure PowerShell 进行身份验证。 因此,虽然可以在进行 Terraform 工作时使用 Azure PowerShell 模块,不过首先需要向 Azure 进行身份验证。

实现 Terraform 代码

  1. 创建用于测试示例 Terraform 代码的目录,并将其设为当前目录。

  2. 创建名为 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>"
    }
    
  3. 创建名为 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
      ]
    }
    
    
  4. 创建名为 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 时遇到的常见问题

后续步骤

使用 Azure CLI 还原 Azure Database for PostgreSQL - 灵活服务器