Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #! /bin/bash
  2. # ------------------------------------------------------------------------------
  3. # Assumes you are trying to retrieve a database dump from heroku which stores
  4. # the promoted DB connection string in the environment variable `DATABASE_URL`
  5. # We don't want to just use the password plainly in the `pg_dump` command so...
  6.  
  7. # If you are interested in restoring a local database then see:
  8. # `migrate_prod_to_local.sh` and if you are interested in loading prod to
  9. # staging then see `migrate_prdo_to_staging.sh`.
  10. # ------------------------------------------------------------------------------
  11. echo -e "\033[0;36mRetrieving the the DB export...\033[0m"
  12.  
  13. # Get the Heroku config from the git remote "prod"
  14. # Should probably make the remote an optional argument, because in most
  15. # situations it will be **heroku**, the default that all their documentation
  16. # uses and the default with `heroku create` and `heroku add`
  17. export `heroku config -s -r prod`
  18.  
  19. # Messily parse the DATABASE_URL to get the pieces we need
  20. # Leaving all this in just as a reference if anyone ever needs it
  21. clean_url="${DATABASE_URL//\'}"
  22. proto="$(echo $clean_url | grep :// | sed -e's,^\(.*://\).*,\1,g')"
  23. url="$(echo ${clean_url/$proto/})"
  24. user_pass="$(echo $url | grep @ | cut -d@ -f1)"
  25. pass="$(echo $user_pass | cut -d: -f2)"
  26. user="$(echo ${user_pass/:$pass})"
  27. host_port="$(echo ${url/$user_pass@/} | cut -d/ -f1)"
  28. port="$(echo $host_port | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
  29. host="$(echo ${host_port/:$port/} | cut -d/ -f1)"
  30. db_name="$(echo $url | grep / | cut -d/ -f2-)"
  31.  
  32. # Uncomment if you want to see the values we are retrieving from the
  33. # `$DATABASE_URL` and then parsing...
  34. # echo "url: $url"
  35. # echo " proto: $proto"
  36. # echo " user: $user"
  37. # echo " pass: $pass"
  38. # echo " host_port: $host_port"
  39. # echo " host: $host"
  40. # echo " port: $port"
  41. # echo " db: $db_name"
  42.  
  43. # Fearlessly retrieve a database and overwrite the existing file `latest.dump`...
  44. # add the `-v` flag to the pg_dump command to see output and remove the
  45. (PGPASSWORD=$pass \
  46. pg_dump -h $host -Fc -o --no-owner --no-acl -U $user $db_name > latest.dump) &
  47. pid=$! # Process Id of the previous running command
  48.  
  49. # ...you have to have a spinner...
  50. spin='-\|/'
  51.  
  52. i=0
  53. # while [ "$(ps a | awk '{print $1}' | grep $pid)" ]
  54. tput civis
  55. while [ "$(ps a | awk '{print $1}' | grep $pid)" ]
  56. do
  57. i=$(( (i+1) %4 ))
  58. printf "\r\033[0KReading from DB: \033[1;32m$host/\033[1;32m$db_name\033[0m [${spin:$i:1}]"
  59. sleep .1
  60. done
  61.  
  62. echo -en "\r\033[0KKnock. Knock."
  63. sleep .75
  64. echo -en "\r\033[0KWho is it?"
  65. sleep .75
  66. echo -en "\r\033[0KRemote database here."
  67. sleep .75
  68. echo
  69. tput cnorm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement