From 9eb116502a691535b5eec66d85f7006f34c9321c Mon Sep 17 00:00:00 2001 From: DJ Gillespie Date: Wed, 28 Oct 2020 17:47:09 -0600 Subject: [PATCH] added restore staging db script --- restore-staging-db-with-production.sh | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 restore-staging-db-with-production.sh diff --git a/restore-staging-db-with-production.sh b/restore-staging-db-with-production.sh new file mode 100755 index 0000000..c7fd977 --- /dev/null +++ b/restore-staging-db-with-production.sh @@ -0,0 +1,48 @@ +function indent() { + sed 's/^/ /'; +} + +function set_maintenance() { + # app_name mode + heroku maintenance:$2 --app $1 +} + +function scale_down() { + # app_name + heroku ps --app $1 | grep === | while read line ; do + name=$( echo "$line" |cut -d ' ' -f2 ) + heroku ps:scale $name=0 --app $1 + done +} + +function copy_db { + # from_app, from_db, to_app, to_db + # heroku pg:copy $1::$2 $4 --app $3 --confirm $3 # this is too slow + heroku pg:backups capture $2 --app $1 # capture backup # not required if we want to use the latest backup + backup_id=`heroku pg:backups --app $1 | grep $2 | head -n1 | cut -d " " -f 1` + echo " RESTORING BACKUP ID $backup_id ($1/$2) to $3/$4" + heroku pg:backups restore "`heroku pg:backups public-url $backup_id --app $1`" $4 --app $3 --confirm $3 # restore backup +} + +function scale_web_dyno_to_one() { + # app_name + heroku ps:scale web=1 --app $1 +} + +echo -e "\n*** ENABLING MAINTENANCE MODE ***" +set_maintenance qrtr-services-staging on + +echo -e "\n*** TURN OFF DYNOS ***" +scale_down qrtr-services-staging + +echo -e "\n*** EMPTYING DB ***" +heroku pg:reset DATABASE --app qrtr-services-staging --confirm qrtr-services-staging + +echo -e "\n*** RESTORING DB ***" +copy_db qrtr-services DATABASE qrtr-services-staging DATABASE | indent + +echo -e "\n*** TURN ON WEB DYNOS ***" +scale_web_dyno_to_one qrtr-services-staging + +echo -e "\n*** DISABLING MAINTENANCE MODE ***" +set_maintenance qrtr-services-staging off