Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. source .env
  4.  
  5. if [ -z "$1" ]
  6. then
  7. echo "Missing file to dump"
  8. exit 1
  9. fi
  10.  
  11. readonly URI_REGEX='^(([^:\/?#]+):)?(\/\/((([^:\/?#]+):)?(([^:\/?#]+)@)?([^:\/?#]+)(:([0-9]+))?))?(\/([^?#]*))(\?([^#]*))?(#(.*))?'
  12. # ↑↑ ↑ ↑↑↑ ↑↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
  13. # |2 scheme | ||6 userinfo |8 passinfo 9 host | 11 port | 13 rpath | 15 query | 17 fragment
  14. # 1 scheme: | |5 userinfo: 7 passinfo@ 10 :… 12 /path 14 ?… 16 #…
  15. # | 4
  16. # 3 //…
  17.  
  18. parse_scheme () {
  19. [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[2]}"
  20. }
  21.  
  22. parse_user () {
  23. [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[6]}"
  24. }
  25.  
  26. parse_password () {
  27. [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[8]}"
  28. }
  29.  
  30. parse_host () {
  31. [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[9]}"
  32. }
  33.  
  34. parse_port () {
  35. [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[11]}"
  36. }
  37.  
  38. parse_rpath () {
  39. [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[13]}"
  40. }
  41.  
  42. #DATABASE_URL="mysql://user:heslo@db:3306/database"
  43. #parse_scheme $DATABASE_URL
  44. DB_USER=$(parse_user $DATABASE_URL)
  45. DB_PASS=$(parse_password $DATABASE_URL)
  46. DB_HOST=$(parse_host $DATABASE_URL)
  47. DB_PORT=$(parse_port $DATABASE_URL)
  48. DB_NAME=$(parse_rpath $DATABASE_URL)
  49.  
  50. export MYSQL_PWD=$DB_PASS
  51. mysqldump -u $DB_USER -h $DB_HOST -P $DB_PORT --databases $DB_NAME > $1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement