Guest User

Untitled

a guest
Feb 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. : ' A simple MySQL dump 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. __output_file="${6:-"/tmp/${__mysql_database}.gz"}"
  33.  
  34. # binaries
  35. __MYSQLDUMP=$(which mysqldump)
  36. __GZIP=$(which gzip)
  37.  
  38. # create mysqldump string
  39. readonly __mysql_dump_string="${__MYSQLDUMP} --user="${__mysql_user}" \
  40. --password="${__mysql_password}" \
  41. --host="${__mysql_host}" \
  42. --port="${__mysql_port}" \
  43. --skip-opt \
  44. --skip-add-locks \
  45. --create-options \
  46. --no-create-db \
  47. --single-transaction \
  48. --quick \
  49. --extended-insert \
  50. --hex-blob \
  51. --routines \
  52. --set-charset \
  53. --add-drop-table \
  54. "${__mysql_database}" \
  55. | ${__GZIP} --stdout > "${__output_file}""
  56.  
  57. # try to dump the file
  58. echo "Dumping database "${__mysql_database}" to file "${__output_file}", please wait..."
  59. if ! eval $__mysql_dump_string; then
  60.  
  61. echo "ERROR! MySQL could not perform dump for database "${__mysql_database}" to file "${__output_file}""
  62. exit 1
  63.  
  64. fi
Add Comment
Please, Sign In to add comment