Skip to content

Commit 9e2911d

Browse files
Add use_db_compression option for backup database dumps
Enable optional pg_dump compression (-Z 9) via use_db_compression boolean flag. Restore auto-detects compressed (.db.gz) or uncompressed (.db) backups for backward compatibility. Authored By: Christian M. Adams <chadams@redhat.com> Assisted By: Claude
1 parent c996c88 commit 9e2911d

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

config/crd/bases/awx.ansible.com_awxbackups.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ spec:
8888
pg_dump_suffix:
8989
description: Additional parameters for the pg_dump command
9090
type: string
91+
use_db_compression:
92+
description: Enable compression for database dumps. When true, pg_dump uses built-in compression. Default is false.
93+
type: boolean
94+
default: false
9195
postgres_label_selector:
9296
description: Label selector used to identify postgres pod for backing up data
9397
type: string

config/manifests/bases/awx-operator.clusterserviceversion.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ spec:
175175
path: additional_labels
176176
x-descriptors:
177177
- urn:alm:descriptor:com.tectonic.ui:advanced
178+
- description: Enable compression for database dumps using pg_dump built-in compression
179+
displayName: Use DB Compression
180+
path: use_db_compression
181+
x-descriptors:
182+
- urn:alm:descriptor:com.tectonic.ui:advanced
183+
- urn:alm:descriptor:com.tectonic.ui:booleanSwitch
178184
- displayName: Node Selector for backup management pod
179185
path: db_management_pod_node_selector
180186
x-descriptors:

roles/backup/defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ backup_resource_requirements:
4242
# Allow additional parameters to be added to the pg_dump backup command
4343
pg_dump_suffix: ''
4444

45+
# Enable compression for database dumps
46+
use_db_compression: false
47+
4548
# Labels defined on the resource, which should be propagated to child resources
4649
additional_labels: []
4750

roles/backup/tasks/postgres.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,17 @@
6464
command: >-
6565
mkdir -p {{ backup_dir }}
6666
67+
- name: Set database filename
68+
set_fact:
69+
_db_filename: "{{ use_db_compression | bool | ternary('tower.db.gz', 'tower.db') }}"
70+
6771
- name: Precreate file for database dump
6872
k8s_exec:
6973
namespace: "{{ backup_pvc_namespace }}"
7074
pod: "{{ ansible_operator_meta.name }}-db-management"
7175
container: "{{ ansible_operator_meta.name }}-db-management"
7276
command: >-
73-
touch {{ backup_dir }}/tower.db
77+
touch {{ backup_dir }}/{{ _db_filename }}
7478
7579
- name: Set resolvable_db_host
7680
set_fact:
@@ -121,6 +125,7 @@
121125
-d {{ awx_postgres_database }}
122126
-p {{ awx_postgres_port }}
123127
-F custom
128+
{{ use_db_compression | bool | ternary('-Z 9', '') }}
124129
{{ pg_dump_suffix }}
125130
no_log: "{{ no_log }}"
126131

@@ -147,7 +152,7 @@
147152
trap 'end_keepalive \"$keepalive_file\" \"$keepalive_pid\"' EXIT SIGINT SIGTERM
148153
echo keepalive_pid: $keepalive_pid
149154
set -e -o pipefail
150-
PGPASSWORD='{{ awx_postgres_pass }}' {{ pgdump }} > {{ backup_dir }}/tower.db
155+
PGPASSWORD='{{ awx_postgres_pass }}' {{ pgdump }} > {{ backup_dir }}/{{ _db_filename }}
151156
set +e +o pipefail
152157
echo 'Successful'
153158
"

roles/restore/tasks/postgres.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@
8080
-p {{ awx_postgres_port }}
8181
no_log: "{{ no_log }}"
8282

83+
- name: Find database file
84+
kubernetes.core.k8s_exec:
85+
namespace: "{{ backup_pvc_namespace }}"
86+
pod: "{{ ansible_operator_meta.name }}-db-management"
87+
container: "{{ ansible_operator_meta.name }}-db-management"
88+
command: >-
89+
bash -c "ls {{ backup_dir }}/tower.db.gz 2>/dev/null || ls {{ backup_dir }}/tower.db 2>/dev/null"
90+
register: _db_file_result
91+
92+
- name: Set database filename for restore
93+
set_fact:
94+
_db_filename: "{{ _db_file_result.stdout | trim | basename }}"
95+
8396
- name: Set pg_restore command
8497
set_fact:
8598
pg_restore: >-
@@ -159,7 +172,7 @@
159172
echo keepalive_pid: $keepalive_pid
160173
set -e -o pipefail
161174
{{ pg_drop_create }}
162-
cat {{ backup_dir }}/tower.db | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }}
175+
cat {{ backup_dir }}/{{ _db_filename }} | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }}
163176
PG_RC=$?
164177
set +e +o pipefail
165178
exit $PG_RC

0 commit comments

Comments
 (0)