From df0a1ae3b544bb3db231b3aa93292ef170db0c64 Mon Sep 17 00:00:00 2001 From: leethanh2112 <33690376+leethanh2112@users.noreply.github.com> Date: Thu, 16 Nov 2023 17:23:21 +0700 Subject: [PATCH] Create Backup Policy for SQLDatabase and SAPHanaDatabase (#1843) * init code for backup policy vm workload * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * update code * separate SQLDatabase and SAPHana * init code example * revert code * refactor backup policy code * add example to workflow 106 * refactor backup_policies_vm_workloads implementation * enforce required for backup block * fix policy_type error * fix protection_policy error * add saphana tfvars --------- Co-authored-by: thanhlcao Co-authored-by: khairi --- .github/workflows/standalone-scenarios.json | 1 + .../configuration.tfvars | 13 ++++ .../recovery_vaults.tfvars | 73 +++++++++++++++++ .../backup_policies_vm_workload.tf | 78 +++++++++++++++++++ modules/recovery_vault/outputs.tf | 1 + 5 files changed, 166 insertions(+) create mode 100644 examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/configuration.tfvars create mode 100644 examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/recovery_vaults.tfvars create mode 100644 modules/recovery_vault/backup_policies_vm_workload.tf diff --git a/.github/workflows/standalone-scenarios.json b/.github/workflows/standalone-scenarios.json index 38c6d74801..8e74ef48c4 100644 --- a/.github/workflows/standalone-scenarios.json +++ b/.github/workflows/standalone-scenarios.json @@ -136,6 +136,7 @@ "recovery_vault/103-asr-with-private-endpoint", "recovery_vault/104-backupvault-with-private-endpoint", "recovery_vault/105-asr-with-network-mapping", + "recovery_vault/106-backupvault-with-sqldatabase-saphana", "redis_cache/103-redis-private-endpoints", "role_mapping/100-simple-role-mapping", "role_mapping/101-function-app-managed-identity", diff --git a/examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/configuration.tfvars b/examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/configuration.tfvars new file mode 100644 index 0000000000..85e59b2476 --- /dev/null +++ b/examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/configuration.tfvars @@ -0,0 +1,13 @@ +global_settings = { + regions = { + region1 = "australiaeast" + } +} +resource_groups = { + primary = { + name = "backup_policy_sql" + region = "region1" + } +} + + diff --git a/examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/recovery_vaults.tfvars b/examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/recovery_vaults.tfvars new file mode 100644 index 0000000000..b744f026c2 --- /dev/null +++ b/examples/recovery_vault/106-backupvault-with-sqldatabase-saphana/recovery_vaults.tfvars @@ -0,0 +1,73 @@ +recovery_vaults = { + asr1 = { + name = "vault_re1" + resource_group_key = "primary" + region = "region1" + vnet_key = "vnet_region1" + subnet_key = "asr_subnet" + + soft_delete_enabled = false + backup_policies = { + vm_workloads = { + sql = { + name = "SQLTest" + workload_type = "SQLDataBase" + vault_key = "asr1" + rg_key = "primary" + timezone = "UTC" + compression_enabled = false + protection_policies = { + sqlfull = { + policy_type = "Full" + backup = { + frequency = "Daily" + time = "15:00" + } + retention_daily = { + count = 8 + } + } + sqllog = { + policy_type = "Log" + backup = { + frequency_in_minutes = 15 + } + simple_retention = { + count = 8 + } + } + } + } + saphana = { + name = "SAPHANATest" + workload_type = "SAPHanaDatabase" + vault_key = "asr1" + rg_key = "primary" + timezone = "UTC" + compression_enabled = false + protection_policies = { + saphanafull = { + policy_type = "Full" + backup = { + frequency = "Daily" + time = "15:00" + } + retention_daily = { + count = 8 + } + } + saphanalog = { + policy_type = "Log" + backup = { + frequency_in_minutes = 15 + } + simple_retention = { + count = 8 + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/modules/recovery_vault/backup_policies_vm_workload.tf b/modules/recovery_vault/backup_policies_vm_workload.tf new file mode 100644 index 0000000000..b40bffdeda --- /dev/null +++ b/modules/recovery_vault/backup_policies_vm_workload.tf @@ -0,0 +1,78 @@ +resource "azurerm_backup_policy_vm_workload" "vm_workload" { + for_each = try(var.settings.backup_policies.vm_workloads, {}) + + name = each.value.name + resource_group_name = local.resource_group_name + recovery_vault_name = azurerm_recovery_services_vault.asr.name + workload_type = each.value.workload_type + + settings { + time_zone = each.value.timezone + compression_enabled = each.value.compression_enabled + } + + dynamic "protection_policy" { + for_each = each.value.protection_policies + + content { + policy_type = protection_policy.value.policy_type + + backup { + frequency = try(protection_policy.value.backup.frequency, null) + frequency_in_minutes = try(protection_policy.value.backup.frequency_in_minutes, null) + time = try(protection_policy.value.backup.time, null) + weekdays = try(protection_policy.value.backup.weekdays, null) + } + + dynamic "retention_daily" { + for_each = lookup(protection_policy.value, "retention_daily", null) == null ? [] : [1] + + content { + count = protection_policy.value.retention_daily.count + } + } + + dynamic "retention_weekly" { + for_each = lookup(protection_policy.value, "retention_weekly", null) == null ? [] : [1] + + content { + count = protection_policy.value.retention_weekly.count + weekdays = protection_policy.value.retention_weekly.weekdays + } + } + + dynamic "retention_monthly" { + for_each = lookup(protection_policy.value, "retention_monthly", null) == null ? [] : [1] + + content { + count = protection_policy.value.retention_monthly.count + format_type = protection_policy.value.retention_monthly.format_type + monthdays = try(protection_policy.value.retention_monthly.monthdays, null) + weekdays = try(protection_policy.value.retention_monthly.weekdays, null) + weeks = try(protection_policy.value.retention_monthly.weeks, null) + } + } + + dynamic "retention_yearly" { + for_each = lookup(protection_policy.value, "retention_yearly", null) == null ? [] : [1] + + content { + count = protection_policy.value.retention_yearly.count + format_type = protection_policy.value.retention_yearly.format_type + months = protection_policy.value.retention_yearly.months + monthdays = try(protection_policy.value.retention_yearly.monthdays, null) + weekdays = try(protection_policy.value.retention_yearly.weekdays, null) + weeks = try(protection_policy.value.retention_yearly.weeks, null) + } + } + + dynamic "simple_retention" { + for_each = lookup(protection_policy.value, "simple_retention", null) == null ? [] : [1] + + content { + count = protection_policy.value.simple_retention.count + } + } + } + } +} diff --git a/modules/recovery_vault/outputs.tf b/modules/recovery_vault/outputs.tf index 936572ebe9..7ddb9fc6b3 100644 --- a/modules/recovery_vault/outputs.tf +++ b/modules/recovery_vault/outputs.tf @@ -16,6 +16,7 @@ output "backup_policies" { value = { virtual_machines = azurerm_backup_policy_vm.vm file_shares = azurerm_backup_policy_file_share.fs + vm_workloads = azurerm_backup_policy_vm_workload.vm_workload } }