Add option to force drop database before restore (#1639)

This commit is contained in:
Hao Liu
2023-11-27 12:52:07 -06:00
committed by GitHub
parent a9245454a4
commit 0f0ff32bf6
3 changed files with 33 additions and 0 deletions

View File

@@ -82,6 +82,26 @@
-p {{ awx_postgres_port }}
no_log: "{{ no_log }}"
- name: Set drop db command
set_fact:
pg_drop_db: >-
echo 'DROP DATABASE {{ awx_postgres_database }} WITH (FORCE);' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-U {{ awx_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ awx_postgres_port }}
no_log: "{{ no_log }}"
- name: Set create db command
set_fact:
pg_create_db: >-
echo 'CREATE DATABASE {{ awx_postgres_database }} WITH OWNER = {{ awx_postgres_user }};' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-U {{ awx_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ awx_postgres_port }}
no_log: "{{ no_log }}"
- name: Restore database dump to the new postgresql container
k8s_exec:
namespace: "{{ backup_pvc_namespace }}"
@@ -104,6 +124,10 @@
trap 'end_keepalive \"$keepalive_file\" \"$keepalive_pid\"' EXIT SIGINT SIGTERM
echo keepalive_pid: $keepalive_pid
set -e -o pipefail
if {{ force_drop_db }}; then
{{ pg_drop_db }}
{{ pg_create_db }}
fi
cat {{ backup_dir }}/tower.db | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }}
set +e +o pipefail
echo 'Successful'