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..5270722 100755 --- a/scripts/db_restore.sh +++ b/scripts/db_restore.sh @@ -6,21 +6,23 @@ 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 # 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 -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 dbuser +gunzip -c $bkpfile | docker exec --interactive db psql --username "$DB_USER" echo "Restored from $bkpfile."