Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to force drop database before restore #1639

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/crd/bases/awx.ansible.com_awxrestores.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ spec:
description: Maintain some of the recommended `app.kubernetes.io/*` labels on the resource (self)
type: boolean
default: true
force_drop_db:
description: Force drop the database before restoring. USE WITH CAUTION!
type: boolean
default: false
status:
type: object
x-kubernetes-preserve-unknown-fields: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ spec:
path: db_management_pod_node_selector
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:advanced
- displayName: Force drop database before restore
path: force_drop_db
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:advanced
TheRealHaoLiu marked this conversation as resolved.
Show resolved Hide resolved
- urn:alm:descriptor:com.tectonic.ui:booleanSwitch
statusDescriptors:
- description: The state of the restore
displayName: Restore Status
Expand Down
24 changes: 24 additions & 0 deletions roles/restore/tasks/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand All @@ -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'
Expand Down
Loading