Guest User

Untitled

a guest
Feb 13th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. : ' A simple MySQL import script with optimized options
  4.  
  5. '
  6.  
  7. # check if debug flag is set
  8. if [ "${DEBUG}" = true ]; then
  9.  
  10. set -x # enable print commands and their arguments as they are executed.
  11. export # show all declared variables (includes system variables)
  12. whoami # print current user
  13.  
  14. else
  15.  
  16. # unset if flag is not set
  17. unset DEBUG
  18.  
  19. fi
  20.  
  21. # bash default parameters
  22. set -o errexit # make your script exit when a command fails
  23. set -o pipefail # exit status of the last command that threw a non-zero exit code is returned
  24. set -o nounset # exit when your script tries to use undeclared variables
  25.  
  26. # parameters
  27. __mysql_user="${1:-"root"}"
  28. __mysql_password="${2:-"password"}"
  29. __mysql_port="${3:-"3306"}"
  30. __mysql_host="${4:-"127.0.0.1"}"
  31. __mysql_database="${5:-"database"}"
  32. __input_file="${6:-"/tmp/${__mysql_database}.gz"}"
  33.  
  34. # binaries
  35. __MYSQL=$(which mysql)
  36. __ZCAT=$(which zcat)
  37.  
  38. # create mysql import command
  39. readonly __mysql_import_string="${__ZCAT} "${__input_file}" \
  40. | ${__MYSQL} --user="${__mysql_user}" \
  41. --password="${__mysql_password}" \
  42. --host="${__mysql_host}" \
  43. --port="${__mysql_port}" \
  44. --database="${__mysql_database}""
  45.  
  46. # try to import dump file
  47. echo "Importing database "${__mysql_database}" from file "${__input_file}", please wait..."
  48. if ! eval $__mysql_import_string; then
  49.  
  50. echo "ERROR! MySQL could not perform import for dump file "${__input_file}" to database "${__mysql_database}""
  51. exit 1
  52.  
  53. fi
Add Comment
Please, Sign In to add comment