0

The azure Database for PostgreSQL Flexible server automatically back up the databases. In case of any accidental deletion of any databases we can restore the database by creating a new flexible server for the recovery process from the back up database .I know how do it from azure portal.Does the terraform code can also configure "backup and restore" for PostgreSQL Flexible server - Restore server.

The exact summary of the manual task documented in the azure doc:https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-restore-server-portal. Just i want do the task using terraform . In addition to that ensure appropriate login and database level permission

I really appreciate any support and help

enter image description here

eku
  • 11
  • 1
  • 2
  • 6
  • Hi eku, if komali's answer helps you, please [mark it as the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). This will help others who meet the similar situation. – Bowman Zhu-MSFT Nov 11 '22 at 06:05

1 Answers1

0

It is possible to create the azure database for PostgreSQL flexible server backup using terraform

Please use the below terraform code to restore the server

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "RG_NAME"
  location = "EASTUS"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vn"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "example" {
  name                 = "example-sn"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
  service_endpoints    = ["Microsoft.Storage"]
  delegation {
    name = "fs"
    service_delegation {
      name = "Microsoft.DBforPostgreSQL/flexibleServers"
      actions = [
        "Microsoft.Network/virtualNetworks/subnets/join/action",
      ]
    }
  }
}
resource "azurerm_private_dns_zone" "example" {
  name                = "example.postgres.database.azure.com"
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_private_dns_zone_virtual_network_link" "example" {
  name                  = "exampleVnetZone.com"
  private_dns_zone_name = azurerm_private_dns_zone.example.name
  virtual_network_id    = azurerm_virtual_network.example.id
  resource_group_name   = azurerm_resource_group.example.name
}

resource "azurerm_postgresql_flexible_server" "example" {
  name                   = "example-psqlflexibleserver"
  resource_group_name    = azurerm_resource_group.example.name
  location               = azurerm_resource_group.example.location
  version                = "12"
  delegated_subnet_id    = azurerm_subnet.example.id
  private_dns_zone_id    = azurerm_private_dns_zone.example.id
  administrator_login    = "psqladmin"
  administrator_password = "H@Sh1CoR3!"
  zone                   = "1"

  storage_mb = 32768
  backup_retention_days = 30
  geo_redundant_backup_enabled = true

  sku_name   = "GP_Standard_D4s_v3"
  depends_on = [azurerm_private_dns_zone_virtual_network_link.example]
 
}

Here I have mentioned the RG_name, subnet, VM, Vnet, db name, password and backup policy days

I have given the backup policy retention days are 30 the policy retention days should be in between 1 to 35 and the defaults value is 7 days

Before running the script we have to check the appropriate login server details

After the follow the below steps to execute the file

terraform init

It will initialize the file

enter image description here

Terraform plan

This will creates an execution plan and it will preview the changes that terraform plans to make the infrastructure

enter image description here

Terraform apply

This will creates or updates the infrastructure depending on the configuration

enter image description here

Previously it was default and the geo_redundant_backup_enabled is false I have set it to true and backup policy will be 30 days

For reference you can use this documentation

Komali Annem
  • 649
  • 1
  • 1
  • 7
  • only the defining the retention period will create new server with old database which is accidentally deleted. dont we need to define any create mode. something like mentioned in terraform :create_mode - (Optional) The creation mode which can be used to restore or replicate existing servers. Possible values are Default and PointInTimeRestore. Changing this forces a new PostgreSQL Flexible Server to be created. – eku Nov 08 '22 at 10:22
  • Thankyou for the reply @eku, If you need to get the backup without any particular retention period then we can also use create_mode, if we use create_mode we are not sure when it will get deleted, but for retention period we have atleast 35 days to backup as mentioned above, Create_mode also accepts parameters like `source_server_id, point_in_time_restore_time_in_utc` etc. – Komali Annem Nov 08 '22 at 10:53
  • Thank you @Komali Annem for your reply. To precisely understanding. if i create fresh pssql flex server with a databases name: "emloyee_db" with retention period 35 days using terraform. if the database get deleted accidentally. if i again apply the same code with retention period , it will automatically created a new server(restored server) with old database info and i will not loose any previous data inside the database.?? – eku Nov 08 '22 at 11:11
  • @eku, yes exactly and that will work fine. – Komali Annem Nov 08 '22 at 11:22
  • Thank yo so much :). I really appreciate that. @Komali Annem :) – eku Nov 08 '22 at 11:28