Skip to content

Commit

Permalink
feat: Provide an option to compress backups for PostreSQL and MySQL (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
gleichda committed Aug 9, 2022
1 parent e26861e commit b1ef34d
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ tags:
substitutions:
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '1.3'
_API_ACTIVATION_SECONDS_WAIT: '600'
_API_ACTIVATION_SECONDS_WAIT: '900'
1 change: 1 addition & 0 deletions examples/mysql-backup-create-service-account/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ module "backup" {
backup_retention_time = 1
backup_schedule = "5 * * * *"
export_schedule = "10 * * * *"
compress_export = true
}
5 changes: 5 additions & 0 deletions examples/mysql-backup-create-service-account/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ output "instance_name" {
value = module.mysql.instance_name
description = "The name of the SQL instance"
}

output "mysql-password" {
value = module.mysql.generated_user_password
sensitive = true
}
1 change: 1 addition & 0 deletions modules/backup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fetch workflows.googleapis.com/Workflow
|------|-------------|------|---------|:--------:|
| backup\_retention\_time | The number of days backups should be kept | `number` | `30` | no |
| backup\_schedule | The cron schedule to execute the internal backup | `string` | `"45 2 * * *"` | no |
| compress\_export | Whether or not to compress the export when storing in the bucket; Only valid for MySQL and PostgreSQL | `bool` | `false` | no |
| enable\_export\_backup | Weather to create exports to GCS Buckets with this module | `bool` | `true` | no |
| enable\_internal\_backup | Wether to create internal backups with this module | `bool` | `true` | no |
| export\_databases | The list of databases that should be exported - if is an empty set all databases will be exported | `set(string)` | `[]` | no |
Expand Down
1 change: 1 addition & 0 deletions modules/backup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ resource "google_workflows_workflow" "sql_export" {
databases = jsonencode(var.export_databases)
gcsBucket = var.export_uri
dbType = split("_", data.google_sql_database_instance.backup_instance.database_version)[0]
compressExport = var.compress_export
})
}

Expand Down
4 changes: 2 additions & 2 deletions modules/backup/templates/export.yaml.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ main:
body:
exportContext:
databases: [$${database}]
uri: $${"${gcsBucket}/${instanceName}-" + database + "-" + backupTime + ".sql"}
uri: $${"${gcsBucket}/${instanceName}-" + database + "-" + backupTime + %{ if compressExport == true }".sql.gz"%{ else }".sql"%{ endif }}
%{ endif }

%{ if dbType == "SQLSERVER" }
Expand Down Expand Up @@ -85,6 +85,6 @@ main:
body:
exportContext:
databases: $${databases}
uri: $${"${gcsBucket}/${instanceName}-" + backupTime + ".sql"}
uri: $${"${gcsBucket}/${instanceName}-" + backupTime + %{ if compressExport == true }".sql.gz"%{ else }".sql"%{ endif }}
%{ endif }

9 changes: 8 additions & 1 deletion modules/backup/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ variable "export_uri" {
type = string
validation {
condition = can(regex("^gs:\\/\\/", var.export_uri))
error_message = "Must be a full GCS URI starting with gs://." #TODO: test
error_message = "Must be a full GCS URI starting with gs://."
}
}

#TODO: gleichda change default to true on next major release (#336)
variable "compress_export" {
description = "Whether or not to compress the export when storing in the bucket; Only valid for MySQL and PostgreSQL"
type = bool
default = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestMySqlBackupModuleCreateServiceAccount(t *testing.T) {
fmt.Sprintf("instance: %s", instanceName),
fmt.Sprintf("project: %s", projectID),
"- backupTime: ${string(int(sys.now()))}",
fmt.Sprintf("uri: ${\"gs://%s-backup/%[1]s-\" + backupTime + \".sql\"}", instanceName),
fmt.Sprintf("uri: ${\"gs://%s-backup/%[1]s-\" + backupTime + \".sql.gz\"}", instanceName),
}

for _, expected := range backupContainsExpecations {
Expand Down

0 comments on commit b1ef34d

Please sign in to comment.