Created
June 13, 2019 07:13
-
-
Save zajca/4d00702ec6beeb4a970ddf22f4861f3c to your computer and use it in GitHub Desktop.
dump mysql database using query string from .env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
source .env | |
if [ -z "$1" ] | |
then | |
echo "Missing file to dump" | |
exit 1 | |
fi | |
readonly URI_REGEX='^(([^:\/?#]+):)?(\/\/((([^:\/?#]+):)?(([^:\/?#]+)@)?([^:\/?#]+)(:([0-9]+))?))?(\/([^?#]*))(\?([^#]*))?(#(.*))?' | |
# ↑↑ ↑ ↑↑↑ ↑↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ | |
# |2 scheme | ||6 userinfo |8 passinfo 9 host | 11 port | 13 rpath | 15 query | 17 fragment | |
# 1 scheme: | |5 userinfo: 7 passinfo@ 10 :… 12 /path 14 ?… 16 #… | |
# | 4 | |
# 3 //… | |
parse_scheme () { | |
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[2]}" | |
} | |
parse_user () { | |
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[6]}" | |
} | |
parse_password () { | |
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[8]}" | |
} | |
parse_host () { | |
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[9]}" | |
} | |
parse_port () { | |
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[11]}" | |
} | |
parse_rpath () { | |
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[13]}" | |
} | |
#DATABASE_URL="mysql://user:heslo@db:3306/database" | |
#parse_scheme $DATABASE_URL | |
DB_USER=$(parse_user $DATABASE_URL) | |
DB_PASS=$(parse_password $DATABASE_URL) | |
DB_HOST=$(parse_host $DATABASE_URL) | |
DB_PORT=$(parse_port $DATABASE_URL) | |
DB_NAME=$(parse_rpath $DATABASE_URL) | |
export MYSQL_PWD=$DB_PASS | |
mysqldump -u $DB_USER -h $DB_HOST -P $DB_PORT --databases $DB_NAME > $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment