Skip to content

Commit

Permalink
Merge pull request #2 from nsidc/fix-backup-restore-scripts
Browse files Browse the repository at this point in the history
Fix backup and restore scripts
  • Loading branch information
mfisher87 authored Sep 27, 2024
2 parents d451447 + 7b367a8 commit f5019d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 7 additions & 1 deletion scripts/db_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."
16 changes: 9 additions & 7 deletions scripts/db_restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."

0 comments on commit f5019d2

Please sign in to comment.