Skip to content

Commit b1ef34d

Browse files
authored
feat: Provide an option to compress backups for PostreSQL and MySQL (#335)
* Provide an option to compress backups for PostreSQL and MySQL * Fix formatting * Extend API Activation waiting time in order to make more build succeed * Fix Whitespaces and adapt test for provided backup
1 parent e26861e commit b1ef34d

File tree

8 files changed

+20
-5
lines changed

8 files changed

+20
-5
lines changed

build/int.cloudbuild.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,4 @@ tags:
192192
substitutions:
193193
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
194194
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '1.3'
195-
_API_ACTIVATION_SECONDS_WAIT: '600'
195+
_API_ACTIVATION_SECONDS_WAIT: '900'

examples/mysql-backup-create-service-account/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ module "backup" {
5151
backup_retention_time = 1
5252
backup_schedule = "5 * * * *"
5353
export_schedule = "10 * * * *"
54+
compress_export = true
5455
}

examples/mysql-backup-create-service-account/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ output "instance_name" {
4343
value = module.mysql.instance_name
4444
description = "The name of the SQL instance"
4545
}
46+
47+
output "mysql-password" {
48+
value = module.mysql.generated_user_password
49+
sensitive = true
50+
}

modules/backup/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ fetch workflows.googleapis.com/Workflow
5555
|------|-------------|------|---------|:--------:|
5656
| backup\_retention\_time | The number of days backups should be kept | `number` | `30` | no |
5757
| backup\_schedule | The cron schedule to execute the internal backup | `string` | `"45 2 * * *"` | no |
58+
| compress\_export | Whether or not to compress the export when storing in the bucket; Only valid for MySQL and PostgreSQL | `bool` | `false` | no |
5859
| enable\_export\_backup | Weather to create exports to GCS Buckets with this module | `bool` | `true` | no |
5960
| enable\_internal\_backup | Wether to create internal backups with this module | `bool` | `true` | no |
6061
| export\_databases | The list of databases that should be exported - if is an empty set all databases will be exported | `set(string)` | `[]` | no |

modules/backup/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ resource "google_workflows_workflow" "sql_export" {
109109
databases = jsonencode(var.export_databases)
110110
gcsBucket = var.export_uri
111111
dbType = split("_", data.google_sql_database_instance.backup_instance.database_version)[0]
112+
compressExport = var.compress_export
112113
})
113114
}
114115

modules/backup/templates/export.yaml.tftpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ main:
5151
body:
5252
exportContext:
5353
databases: [$${database}]
54-
uri: $${"${gcsBucket}/${instanceName}-" + database + "-" + backupTime + ".sql"}
54+
uri: $${"${gcsBucket}/${instanceName}-" + database + "-" + backupTime + %{ if compressExport == true }".sql.gz"%{ else }".sql"%{ endif }}
5555
%{ endif }
5656

5757
%{ if dbType == "SQLSERVER" }
@@ -85,6 +85,6 @@ main:
8585
body:
8686
exportContext:
8787
databases: $${databases}
88-
uri: $${"${gcsBucket}/${instanceName}-" + backupTime + ".sql"}
88+
uri: $${"${gcsBucket}/${instanceName}-" + backupTime + %{ if compressExport == true }".sql.gz"%{ else }".sql"%{ endif }}
8989
%{ endif }
9090

modules/backup/variables.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ variable "export_uri" {
8484
type = string
8585
validation {
8686
condition = can(regex("^gs:\\/\\/", var.export_uri))
87-
error_message = "Must be a full GCS URI starting with gs://." #TODO: test
87+
error_message = "Must be a full GCS URI starting with gs://."
8888
}
8989
}
90+
91+
#TODO: gleichda change default to true on next major release (#336)
92+
variable "compress_export" {
93+
description = "Whether or not to compress the export when storing in the bucket; Only valid for MySQL and PostgreSQL"
94+
type = bool
95+
default = false
96+
}

test/integration/mysql-backup-create-service-account/mysql_backup_create_service_account_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestMySqlBackupModuleCreateServiceAccount(t *testing.T) {
5555
fmt.Sprintf("instance: %s", instanceName),
5656
fmt.Sprintf("project: %s", projectID),
5757
"- backupTime: ${string(int(sys.now()))}",
58-
fmt.Sprintf("uri: ${\"gs://%s-backup/%[1]s-\" + backupTime + \".sql\"}", instanceName),
58+
fmt.Sprintf("uri: ${\"gs://%s-backup/%[1]s-\" + backupTime + \".sql.gz\"}", instanceName),
5959
}
6060

6161
for _, expected := range backupContainsExpecations {

0 commit comments

Comments
 (0)