本文介绍如何使用Terraform创建备份、查看备份、修改备份设置以及删除备份。
前提条件
已创建RDS PostgreSQL实例,详情请参见创建RDS PostgreSQL实例。
实例状态为运行中,您可以通过如下两种方式查看:
参见查询实例详情查看参数status,如果取值为Runing则表示实例状态为运行中。
前往RDS管理控制台,切换到目标地域,找到指定实例后,查看实例状态。
创建备份
在terraform.tf文件中增加
resource "alicloud_rds_backup" "instance" {}
配置项,具体配置如下:... resource "alicloud_rds_backup" "instance" { db_instance_id = alicloud_db_instance.instance.id remove_from_state = true }
运行
terraform apply
。出现如下配置信息后,确认配置信息并输入yes,开始创建备份。
alicloud_db_instance.instance: Refreshing state... [id=pgm-****] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_rds_backup.instance will be created + resource "alicloud_rds_backup" "instance" { + backup_id = (known after apply) + backup_method = (known after apply) + backup_type = (known after apply) + db_instance_id = "pgm-****" + id = (known after apply) + remove_from_state = true + store_status = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
出现类似如下日志时,表示配置成功。
alicloud_rds_backup.instance: Creating... alicloud_rds_backup.instance: Still creating... [10s elapsed] alicloud_rds_backup.instance: Still creating... [20s elapsed] ... alicloud_rds_backup.instance: Still creating... [4m30s elapsed] alicloud_rds_backup.instance: Creation complete after 4m33s [id=pgm-****] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
查看结果。
运行
terraform show
查看创建的备份。... # alicloud_rds_backup.instance: resource "alicloud_rds_backup" "instance" { backup_id = "1498535549" backup_method = "Snapshot" backup_type = "FullBackup" db_instance_id = "pgm-bp1668g9bm4bm9p5" id = "pgm-bp1668g9bm4bm9p5:1498535549" remove_from_state = true store_status = "Enabled" }
登录RDS控制台查看创建的备份。
查看备份
在terraform.tf文件中增加
data "alicloud_rds_backups" "querybackups" {}
配置项,具体配置如下:... data "alicloud_rds_backups" "querybackups" { db_instance_id = alicloud_db_instance.instance.id }
运行
terraform apply
查询备份。出现类似如下日志时,表示查询成功。
alicloud_db_instance.instance: Refreshing state... [id=pgm-****] alicloud_rds_backup.instance: Refreshing state... [id=pgm-****:1498535549] data.alicloud_rds_backups.querybackups: Reading... data.alicloud_rds_backups.querybackups: Read complete after 0s [id=52064****] No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
运行
terraform show
查看备份清单。# data.alicloud_rds_backups.querybackups: data "alicloud_rds_backups" "querybackups" { backups = [ { backup_download_url = "" backup_end_time = "2022-09-29T04:10:15Z" backup_id = "1498535549" backup_initiator = "User" backup_intranet_download_url = "" backup_method = "Snapshot" backup_mode = "Manual" backup_size = "53687091200" backup_start_time = "2022-09-29T04:08:34Z" backup_status = "Success" backup_type = "FullBackup" consistent_time = "1664424514" copy_only_backup = "" db_instance_id = "pgm-****" encryption = jsonencode({}) host_instance_id = "22839235" id = "pgm-****:1498535549" is_avail = 1 meta_status = "" storage_class = "0" store_status = "Enabled" }, { backup_download_url = "" backup_end_time = "2022-09-29T02:55:24Z" backup_id = "1498486764" backup_initiator = "User" backup_intranet_download_url = "" backup_method = "Snapshot" backup_mode = "Manual" backup_size = "53687091200" backup_start_time = "2022-09-29T02:50:24Z" backup_status = "Success" backup_type = "FullBackup" consistent_time = "0" copy_only_backup = "" db_instance_id = "pgm-****" encryption = jsonencode({}) host_instance_id = "22839234" id = "pgm-****:1498486764" is_avail = 1 meta_status = "" storage_class = "0" store_status = "Enabled" }, ] db_instance_id = "pgm-****" id = "520646901" ids = [ "pgm-****:1498535549", "pgm-****:1498486764", ] }
修改备份设值
在terraform.tf文件中增加
resource "alicloud_db_backup_policy" "instance" {}
配置项,具体配置如下:... resource "alicloud_db_backup_policy" "instance" { instance_id = alicloud_db_instance.instance.id preferred_backup_time = "00:00Z-01:00Z" }
运行
terraform apply
。出现如下配置信息后,确认配置信息并输入yes,开始修改RDS PostgreSQL备份设置。
alicloud_db_instance.instance: Refreshing state... [id=pgm-****] alicloud_rds_backup.instance: Refreshing state... [id=pgm-****:1498535549] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_db_backup_policy.instance will be created + resource "alicloud_db_backup_policy" "instance" { + archive_backup_keep_count = (known after apply) + archive_backup_keep_policy = (known after apply) + archive_backup_retention_period = (known after apply) + backup_period = (known after apply) + backup_retention_period = 7 + backup_time = (known after apply) + compress_type = (known after apply) + enable_backup_log = (known after apply) + id = (known after apply) + instance_id = "pgm-****" + local_log_retention_hours = (known after apply) + local_log_retention_space = (known after apply) + log_backup = (known after apply) + log_backup_frequency = (known after apply) + log_backup_retention_period = (known after apply) + log_retention_period = (known after apply) + preferred_backup_period = (known after apply) + preferred_backup_time = "00:00Z-01:00Z" + released_keep_policy = (known after apply) + retention_period = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
出现类似如下日志时,表示配置成功。
alicloud_db_backup_policy.instance: Creating... alicloud_db_backup_policy.instance: Creation complete after 2s [id=pgm-****] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
查看结果。
运行
terraform show
查看备份设置。# alicloud_db_backup_policy.instance: resource "alicloud_db_backup_policy" "instance" { archive_backup_keep_count = 1 archive_backup_keep_policy = "ByMonth" archive_backup_retention_period = 0 backup_period = [ "Friday", "Monday", "Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday", ] backup_retention_period = 7 backup_time = "00:00Z-01:00Z" compress_type = "1" enable_backup_log = true high_space_usage_protection = "Enable" id = "pgm-****" instance_id = "pgm-****" local_log_retention_hours = 0 local_log_retention_space = 30 log_backup = true log_backup_retention_period = 7 log_retention_period = 7 preferred_backup_period = [ "Friday", "Monday", "Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday", ] preferred_backup_time = "00:00Z-01:00Z" released_keep_policy = "None" retention_period = 7 } ...
登录RDS控制台查看备份设置。
删除备份
在terraform.tf文件中删除
resource "alicloud_rds_backup" "instance" {}
配置项的内容。例如,删除如下信息:... resource "alicloud_rds_backup" "instance" { db_instance_id = alicloud_db_instance.instance.id remove_from_state = true }
运行
terraform apply
。出现如下配置信息后,确认配置信息并输入yes,开始删除备份。
alicloud_rds_backup.instance: Refreshing state... [id=pgm-****:1498535549] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] alicloud_db_backup_policy.instance: Refreshing state... [id=pgm-****] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # alicloud_rds_backup.instance will be destroyed # (because alicloud_rds_backup.instance is not in configuration) - resource "alicloud_rds_backup" "instance" { - backup_id = "1498535549" -> null - backup_method = "Snapshot" -> null - backup_type = "FullBackup" -> null - db_instance_id = "pgm-****" -> null - id = "pgm-****:1498535549" -> null - remove_from_state = true -> null - store_status = "Enabled" -> null } Plan: 0 to add, 0 to change, 1 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
出现类似如下日志时,表示删除成功。
alicloud_rds_backup.instance: Destroying... [id=pgm-****:1498535549] alicloud_rds_backup.instance: Destruction complete after 0s Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
查看修改结果。
运行
terraform show
查看备份是否删除。... # data.alicloud_rds_backups.querybackups: data "alicloud_rds_backups" "querybackups" { backups = [] db_instance_id = "pgm-****" id = "0" ids = [] }
登录RDS控制台查看备份是否删除。