Skip to content

Instantly share code, notes, and snippets.

@davidakachaos
Created September 12, 2017 15:02
Show Gist options
  • Save davidakachaos/f2b9f89216c34cb649804bcb08dfc807 to your computer and use it in GitHub Desktop.
Save davidakachaos/f2b9f89216c34cb649804bcb08dfc807 to your computer and use it in GitHub Desktop.
Pull Heroku MySQL database to local (possible overwrite local development database)
#!/usr/bin/env bash
function join_by { local IFS="$1"; shift; echo "$*"; }
# Check for commands used by this script
command -v pv >/dev/null 2>&1 || { echo >&2 "I require pv but it's not installed. Aborting."; exit 1; }
command -v mysqldump >/dev/null 2>&1 || { echo >&2 "I require mysqldump but it's not installed. Aborting."; exit 1; }
command -v mysql >/dev/null 2>&1 || { echo >&2 "I require mysql but it's not installed. Aborting."; exit 1; }
stages=$(git remote | sed 's/origin//g' | tr '\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
stage_req=$1
availible_stages=$(IFS=, ; echo "${stages[*]}")
if [ "$1" == "" ]; then
echo "Please give the stage to pull down: (${availible_stages})"
echo "or give -h to display help"
exit 1
fi
if [ "$1" == "-h" ]; then
echo "This script is used to pull down the database for a stage!"
echo "We expect heroku configured remotes with the name of the stages."
echo "Can only be used with MySQL databases."
exit 2
fi
if [[ " ${stages[@]} " =~ " $stage_req " ]]; then
echo "Gathering database information for stage ${stage_req}..."
database_url=`heroku config -r ${stage_req} | grep DATABASE_URL | sed 's/DATABASE_URL://g' | sed 's/ //g'`
database=`echo ${database_url} | sed -nE 's/.*\/{1}(.+)\?.*/\1/p'`
dbhost=`echo ${database_url} | sed -nE 's/.*\@(.+)\/{1}.*/\1/p' | sed -nE 's/(.*)\/.*/\1/p'`
dbuser=`echo ${database_url} | sed -nE 's/.*\/\/(.+)\:.*/\1/p'`
dbpasswd=`echo ${database_url} | sed -nE 's/.*\:(.+)\@.*/\1/p'`
else
# is not correct
echo "Please use a known stage! (${availible_stages})"
echo "${stage_req} is not a known stage!"
exit 3
fi
if [ -f config/database.yml ]; then
echo "Found database.yml, figuring out local development database..."
local_db=$(ruby -r 'yaml' -e "print YAML::load(File.read('./config/database.yml'))['development']['database']")
else
local_db=""
fi
if [[ $local_db != "" ]]; then
echo "We can pull the database ${database} to the local development database (${local_db})."
echo "THIS WILL OVERWRITE YOUR LOCAL DATABASE!!"
read -p "Do you want to do that? (y/N) " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Pulling the db ${database} into ${local_db}"
mysqldump -h ${dbhost} -u ${dbuser} --password=${dbpasswd} --compress --skip-lock-tables --port=3306 --single-transaction --routines --triggers --add-drop-table ${database} | pv | mysql -uroot
echo "Don't forget to rake migrate the database"
else
echo "Pulling down the ${database} database"
mysqldump -h ${dbhost} -u ${dbuser} --password=${dbpasswd} --compress --skip-lock-tables --port=3306 --single-transaction --routines --triggers --databases ${database} | pv | mysql -uroot
fi
else
echo "Pulling down the ${database} database"
mysqldump -h ${dbhost} -u ${dbuser} --password=${dbpasswd} --compress --skip-lock-tables --port=3306 --single-transaction --routines --triggers --databases ${database} | pv | mysql -uroot
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment