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

使用 Azure 资源管理器模板备份 Azure PostgreSQL - 灵活服务器(预览版)

借助 Azure 备份,可以使用多种选项备份 Azure PostgreSQL - 灵活服务器,这些选项包括 Azure 门户、PowerShell、CLI、Azure 资源管理器、Bicep 等。 本文介绍如何使用 Azure 资源管理器模板和 Azure PowerShell 来备份 Azure PostgreSQL - 灵活服务器。 本快速入门重点介绍如何部署 Azure 资源管理器 (ARM) 模板来创建备份保管库,然后为 Azure PostgreSQL - 灵活服务器配置备份。 有关开发 ARM 模板的详细信息,请参阅 Azure 资源管理器文档

Azure 资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。

先决条件

若要设置环境以进行 Bicep 开发,请参阅安装 Bicep 工具

注意

如文章中所述,安装最新的 Azure PowerShell 模块和 Bicep CLI。

查看模板

使用此模板,可以配置 Azure PostgreSQL - 灵活服务器的备份。 在此模板中,我们将创建一个备份保管库,并在其中包含 PostgreSQL 服务器的备份策略,计划每周备份一次,且保留期为三个月

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "backupVaultName": {
      "type": "string"
    },
    "backupVaultResourceGroup": {
      "type": "string"
    },
    "postgreSQLServerName": {
      "type": "string"
    },
    "postgreSQLResourceGroup": {
      "type": "string"
    },
    "region": {
      "type": "string"
    },
    "policyName": {
      "type": "string"
    },
    "backupScheduleFrequency": {
      "type": "string"
    },
    "retentionDurationInMonths": {
      "type": "int"
    },
    "targetResourceGroupName": {
      "type": "string"
    }
  },
  "resources": [
    {
      "type": "Microsoft.DataProtection/backupVaults",
      "apiVersion": "2023-01-01",
      "name": "[parameters('backupVaultName')]",
      "___location": "[parameters('region')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "properties": {
        "storageSettings": [
          {
            "datastoreType": "VaultStore",
            "type": "LocallyRedundant"
          }
        ]
      }
    },
    {
      "type": "Microsoft.DataProtection/backupVaults/backupPolicies",
      "apiVersion": "2023-01-01",
      "name": "[concat(parameters('backupVaultName'), '/', parameters('policyName'))]",
      "___location": "[parameters('region')]",
      "properties": {
        "datasourceTypes": [
          "AzureDatabaseForPostgreSQLFlexibleServer"
        ],
        "policyRules": [
          {
            "name": "BackupSchedule",
            "objectType": "AzureBackupRule",
            "backupParameters": {
              "objectType": "AzureBackupParams"
            },
            "trigger": {
              "schedule": {
                "recurrenceRule": {
                  "frequency": "Hourly",
                  "interval": "[parameters('backupScheduleFrequency')]"
                }
              }
            },
            "dataStore": {
              "datastoreType": "VaultStore"
            }
          },
          {
            "name": "RetentionRule",
            "objectType": "AzureRetentionRule",
            "isDefault": true,
            "lifecycle": {
              "deleteAfter": {
                "objectType": "AbsoluteDeleteOption",
                "duration": "[concat('P', parameters('retentionDurationInMonths'), 'M')]"
              }
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "name": "[guid(subscription().id, 'PostgreSQLFlexibleServerLongTermRetentionBackupRole
')]",
      "properties": {
        "principalId": "[reference(concat(resourceId(parameters('backupVaultResourceGroup'), 'Microsoft.DataProtection/backupVaults', parameters('backupVaultName')), '/providers/Microsoft.ManagedIdentity/Identities/default'), '2020-12-01').principalId]",
        "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e')]",
        "scope": "[resourceId(parameters('postgreSQLResourceGroup'), 'Microsoft.DBforPostgreSQL/flexibleServers', parameters('postgreSQLServerName'))]"
      }
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "name": "[guid(subscription().id, 'Reader')]",
      "properties": {
        "principalId": "[reference(concat(resourceId(parameters('backupVaultResourceGroup'), 'Microsoft.DataProtection/backupVaults', parameters('backupVaultName')), '/providers/Microsoft.ManagedIdentity/Identities/default'), '2020-12-01').principalId]",
        "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e')]",
        "scope": "[resourceId(parameters('targetResourceGroupName'))]"
      }
    },
    {
      "type": "Microsoft.DataProtection/backupVaults/backupInstances",
      "apiVersion": "2023-01-01",
      "name": "PostgreSQLBackupInstance",
      "___location": "[parameters('region')]",
      "properties": {
        "datasourceInfo": {
          "datasourceType": "AzureDatabaseForPostgreSQLFlexibleServer",
          "objectType": "Datasource",
          "resourceId": "[resourceId(parameters('postgreSQLResourceGroup'), 'Microsoft.DBforPostgreSQL/flexibleServers', parameters('postgreSQLServerName'))]"
        },
        "policyInfo": {
          "policyId": "[resourceId(parameters('backupVaultResourceGroup'), 'Microsoft.DataProtection/backupVaults/backupPolicies', parameters('backupVaultName'), parameters('policyName'))]"
        }
      }
    }
  ]
}

部署模板

要部署模板,请将模板存储在 GitHub 存储库中,然后将以下 PowerShell 脚本粘贴到 shell 窗口中。

$projectName = Read-Host -Prompt "Enter a project name (limited to eight characters) that is used to generate Azure resource names"
$___location = Read-Host -Prompt "Enter the ___location (for example, centralus)"

$resourceGroupName = "${projectName}rg"
$templateUri = "https//templateuri"

New-AzResourceGroup -Name $resourceGroupName -Location $___location
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -projectName 

后续步骤