Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. function join_by { local IFS="$1"; shift; echo "$*"; }
  3. # Check for commands used by this script
  4. command -v pv >/dev/null 2>&1 || { echo >&2 "I require pv but it's not installed. Aborting."; exit 1; }
  5. command -v mysqldump >/dev/null 2>&1 || { echo >&2 "I require mysqldump but it's not installed. Aborting."; exit 1; }
  6. command -v mysql >/dev/null 2>&1 || { echo >&2 "I require mysql but it's not installed. Aborting."; exit 1; }
  7.  
  8. stages=$(git remote | sed 's/origin//g' | tr '\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  9. stage_req=$1
  10.  
  11. availible_stages=$(IFS=, ; echo "${stages[*]}")
  12.  
  13. if [ "$1" == "" ]; then
  14. echo "Please give the stage to pull down: (${availible_stages})"
  15. echo "or give -h to display help"
  16. exit 1
  17. fi
  18. if [ "$1" == "-h" ]; then
  19. echo "This script is used to pull down the database for a stage!"
  20. echo "We expect heroku configured remotes with the name of the stages."
  21. echo "Can only be used with MySQL databases."
  22. exit 2
  23. fi
  24.  
  25. if [[ " ${stages[@]} " =~ " $stage_req " ]]; then
  26. echo "Gathering database information for stage ${stage_req}..."
  27. database_url=`heroku config -r ${stage_req} | grep DATABASE_URL | sed 's/DATABASE_URL://g' | sed 's/ //g'`
  28. database=`echo ${database_url} | sed -nE 's/.*\/{1}(.+)\?.*/\1/p'`
  29. dbhost=`echo ${database_url} | sed -nE 's/.*\@(.+)\/{1}.*/\1/p' | sed -nE 's/(.*)\/.*/\1/p'`
  30. dbuser=`echo ${database_url} | sed -nE 's/.*\/\/(.+)\:.*/\1/p'`
  31. dbpasswd=`echo ${database_url} | sed -nE 's/.*\:(.+)\@.*/\1/p'`
  32. else
  33. # is not correct
  34. echo "Please use a known stage! (${availible_stages})"
  35. echo "${stage_req} is not a known stage!"
  36. exit 3
  37. fi
  38.  
  39. if [ -f config/database.yml ]; then
  40. echo "Found database.yml, figuring out local development database..."
  41. local_db=$(ruby -r 'yaml' -e "print YAML::load(File.read('./config/database.yml'))['development']['database']")
  42. else
  43. local_db=""
  44. fi
  45.  
  46. if [[ $local_db != "" ]]; then
  47. echo "We can pull the database ${database} to the local development database (${local_db})."
  48. echo "THIS WILL OVERWRITE YOUR LOCAL DATABASE!!"
  49. read -p "Do you want to do that? (y/N) " -n 1 -r
  50. echo # (optional) move to a new line
  51. if [[ $REPLY =~ ^[Yy]$ ]]; then
  52. echo "Pulling the db ${database} into ${local_db}"
  53. mysqldump -h ${dbhost} -u ${dbuser} --password=${dbpasswd} --compress --skip-lock-tables --port=3306 --single-transaction --routines --triggers --add-drop-table ${database} | pv | mysql -uroot
  54. echo "Don't forget to rake migrate the database"
  55. else
  56. echo "Pulling down the ${database} database"
  57. mysqldump -h ${dbhost} -u ${dbuser} --password=${dbpasswd} --compress --skip-lock-tables --port=3306 --single-transaction --routines --triggers --databases ${database} | pv | mysql -uroot
  58. fi
  59. else
  60. echo "Pulling down the ${database} database"
  61. mysqldump -h ${dbhost} -u ${dbuser} --password=${dbpasswd} --compress --skip-lock-tables --port=3306 --single-transaction --routines --triggers --databases ${database} | pv | mysql -uroot
  62. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement