From 851f4760803e7e00df4cb1c56e1210e912a024df Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Fri, 27 Sep 2024 15:45:55 -0600 Subject: [PATCH 1/3] Fix backup and restore scripts dbuser is a Unix user in the Docker image, so it looks right. $DB_USER contains the username ("role") that's configured in the database itself. Removed the ability to automatically restore from an unspecified backup file. That's dangerous. If we want this logic to find the latest backup file, we should be doing it in a calling script, not as a default behavior! --- scripts/db_backup.sh | 8 +++++++- scripts/db_restore.sh | 10 ++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/scripts/db_backup.sh b/scripts/db_backup.sh index 47dc478..bd2795a 100755 --- a/scripts/db_backup.sh +++ b/scripts/db_backup.sh @@ -3,6 +3,12 @@ set -e if [ -z "${BACKUP_DIR}" ]; then echo "\$BACKUP_DIR must be set." + exit 1 +fi + +if [ -z "${DB_USER}" ]; then + echo "\$DB_USER must be set." + exit 1 fi if (facter cname | grep blue); then @@ -11,5 +17,5 @@ else bkpfile="$BACKUP_DIR/usaon-benefit-tool-db-backup.sql.gz" fi -docker exec --tty db pg_dumpall --clean --username dbuser | gzip > $bkpfile +docker exec --tty db pg_dumpall --clean --username "$DB_USER" | gzip > $bkpfile echo "Backup saved to $bkpfile." diff --git a/scripts/db_restore.sh b/scripts/db_restore.sh index ccb9726..1553dac 100755 --- a/scripts/db_restore.sh +++ b/scripts/db_restore.sh @@ -6,12 +6,14 @@ SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" if [ -n "$1" ]; then bkpfile="$1" -elif [ -n "${BACKUP_DIR}" ]; then - bkpfile="$(find $BACKUP_DIR -name "usaon-benefit-tool-db-backup.sql.gz*" | sort -r | head -1)" fi if [ -z "$bkpfile" ]; then - echo "Please pass a backup file as argument or set \$BACKUP_DIR and ensure a backup file exists there" + echo "Please pass a path to a backup file as argument." + + if [ -n "$BACKUP_DIR" ]; then + echo "They can most likely be found in ${BACKUP_DIR}." + fi exit 1 fi @@ -22,5 +24,5 @@ $SCRIPTPATH/wait_for_db.sh docker exec db dropdb usaon-benefit-tool || true docker exec db createdb usaon-benefit-tool -gunzip -c $bkpfile | docker exec --interactive db psql --username dbuser +gunzip -c $bkpfile | docker exec --interactive db psql --username "$DB_USER" echo "Restored from $bkpfile." From 938a6bb38dac03d1ae139995a4fece6054ba55be Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Fri, 27 Sep 2024 16:08:51 -0600 Subject: [PATCH 2/3] Use v2 docker compose CLI --- scripts/db_restore.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/db_restore.sh b/scripts/db_restore.sh index 1553dac..b0f97bd 100755 --- a/scripts/db_restore.sh +++ b/scripts/db_restore.sh @@ -18,7 +18,7 @@ if [ -z "$bkpfile" ]; then fi # restart to boot any active sesssions -docker-compose restart +docker compose restart $SCRIPTPATH/wait_for_db.sh docker exec db dropdb usaon-benefit-tool || true From 7b367a8da242f45390357e7f5d4b8fbdf7c70ab3 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Fri, 27 Sep 2024 17:08:17 -0600 Subject: [PATCH 3/3] Fixup restore script --- scripts/db_restore.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/db_restore.sh b/scripts/db_restore.sh index b0f97bd..5270722 100755 --- a/scripts/db_restore.sh +++ b/scripts/db_restore.sh @@ -21,8 +21,8 @@ fi docker compose restart $SCRIPTPATH/wait_for_db.sh -docker exec db dropdb usaon-benefit-tool || true -docker exec db createdb usaon-benefit-tool +docker exec db dropdb --user="${DB_USER}" usaon-benefit-tool || true +docker exec db createdb --user="${DB_USER}" usaon-benefit-tool gunzip -c $bkpfile | docker exec --interactive db psql --username "$DB_USER" echo "Restored from $bkpfile."