Skip to content

Instantly share code, notes, and snippets.

@mlebkowski
Created April 26, 2016 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mlebkowski/380eef1acb809c5a035bf6341481b1ba to your computer and use it in GitHub Desktop.
Save mlebkowski/380eef1acb809c5a035bf6341481b1ba to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
parse_dsn() {
declare dsn=$1 part=$2
php -r 'echo parse_url($_SERVER["argv"][1], constant("PHP_URL_" . strtoupper($_SERVER["argv"][2])));' -- "$dsn" $part
}
main() {
declare name=${1:-$(basename $(pwd) ".dev")}
name=${name//./_}_heroku
local url=$(heroku config|grep mysql | awk '{print $2}')
if [ $? -gt 0 ] || [ -z "$url" ]; then
echo "Cannot get database URL" >&2
return 255
fi
local host=$(parse_dsn "$url" host)
local port=$(parse_dsn "$url" port)
local database=$(parse_dsn "$url" path)
local user=$(parse_dsn "$url" user)
local password=$(parse_dsn "$url" pass)
(
echo "DROP DATABASE IF EXISTS $name;";
echo "CREATE DATABASE $name;";
echo "USE $name;"
mysqldump --user=$user --password=$password --host=$host --port=${port:-3306} --no-create-db -- "${database##/}"
) | mysql
return 0
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment