Skip to content

Instantly share code, notes, and snippets.

@duanearnett
Last active October 11, 2016 17:34
Show Gist options
  • Save duanearnett/bc37cba4e82a1eeffcbde1e2541007a8 to your computer and use it in GitHub Desktop.
Save duanearnett/bc37cba4e82a1eeffcbde1e2541007a8 to your computer and use it in GitHub Desktop.
Read Heroku config and pull a DB export
#! /bin/bash
# ------------------------------------------------------------------------------
# Assumes you are trying to retrieve a database dump from heroku which stores
# the promoted DB connection string in the environment variable `DATABASE_URL`
# We don't want to just use the password plainly in the `pg_dump` command so...
# If you are interested in restoring a local database then see:
# `migrate_prod_to_local.sh` and if you are interested in loading prod to
# staging then see `migrate_prdo_to_staging.sh`.
# ------------------------------------------------------------------------------
echo -e "\033[0;36mRetrieving the the DB export...\033[0m"
# Get the Heroku config from the git remote "prod"
# Should probably make the remote an optional argument, because in most
# situations it will be **heroku**, the default that all their documentation
# uses and the default with `heroku create` and `heroku add`
export `heroku config -s -r prod`
# Messily parse the DATABASE_URL to get the pieces we need
# Leaving all this in just as a reference if anyone ever needs it
clean_url="${DATABASE_URL//\'}"
proto="$(echo $clean_url | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="$(echo ${clean_url/$proto/})"
user_pass="$(echo $url | grep @ | cut -d@ -f1)"
pass="$(echo $user_pass | cut -d: -f2)"
user="$(echo ${user_pass/:$pass})"
host_port="$(echo ${url/$user_pass@/} | cut -d/ -f1)"
port="$(echo $host_port | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
host="$(echo ${host_port/:$port/} | cut -d/ -f1)"
db_name="$(echo $url | grep / | cut -d/ -f2-)"
# Uncomment if you want to see the values we are retrieving from the
# `$DATABASE_URL` and then parsing...
# echo "url: $url"
# echo " proto: $proto"
# echo " user: $user"
# echo " pass: $pass"
# echo " host_port: $host_port"
# echo " host: $host"
# echo " port: $port"
# echo " db: $db_name"
# Fearlessly retrieve a database and overwrite the existing file `latest.dump`...
# add the `-v` flag to the pg_dump command to see output and remove the
(PGPASSWORD=$pass \
pg_dump -h $host -Fc -o --no-owner --no-acl -U $user $db_name > latest.dump) &
pid=$! # Process Id of the previous running command
# ...you have to have a spinner...
spin='-\|/'
i=0
# while [ "$(ps a | awk '{print $1}' | grep $pid)" ]
tput civis
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]
do
i=$(( (i+1) %4 ))
printf "\r\033[0KReading from DB: \033[1;32m$host/\033[1;32m$db_name\033[0m [${spin:$i:1}]"
sleep .1
done
echo -en "\r\033[0KKnock. Knock."
sleep .75
echo -en "\r\033[0KWho is it?"
sleep .75
echo -en "\r\033[0KRemote database here."
sleep .75
echo
tput cnorm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment