Skip to content

Instantly share code, notes, and snippets.

@ivan-pinatti
Created January 30, 2018 15:40
Show Gist options
  • Save ivan-pinatti/510f58c84cab541b67b8e7ae8fb92d2b to your computer and use it in GitHub Desktop.
Save ivan-pinatti/510f58c84cab541b67b8e7ae8fb92d2b to your computer and use it in GitHub Desktop.
Export MySQL database with optimized options - #mysql #database #export #bash
#!/usr/bin/env bash
: ' A simple MySQL dump script with optimized options
'
# check if debug flag is set
if [ "${DEBUG}" = true ]; then
set -x # enable print commands and their arguments as they are executed.
export # show all declared variables (includes system variables)
whoami # print current user
else
# unset if flag is not set
unset DEBUG
fi
# bash default parameters
set -o errexit # make your script exit when a command fails
set -o pipefail # exit status of the last command that threw a non-zero exit code is returned
set -o nounset # exit when your script tries to use undeclared variables
# parameters
__mysql_user="${1:-"root"}"
__mysql_password="${2:-"password"}"
__mysql_port="${3:-"3306"}"
__mysql_host="${4:-"127.0.0.1"}"
__mysql_database="${5:-"database"}"
__output_file="${6:-"/tmp/${__mysql_database}.gz"}"
# binaries
__MYSQLDUMP=$(which mysqldump)
__GZIP=$(which gzip)
# create mysqldump string
readonly __mysql_dump_string="${__MYSQLDUMP} --user="${__mysql_user}" \
--password="${__mysql_password}" \
--host="${__mysql_host}" \
--port="${__mysql_port}" \
--skip-opt \
--skip-add-locks \
--create-options \
--no-create-db \
--single-transaction \
--quick \
--extended-insert \
--hex-blob \
--routines \
--set-charset \
--add-drop-table \
"${__mysql_database}" \
| ${__GZIP} --stdout > "${__output_file}""
# try to dump the file
echo "Dumping database "${__mysql_database}" to file "${__output_file}", please wait..."
if ! eval $__mysql_dump_string; then
echo "ERROR! MySQL could not perform dump for database "${__mysql_database}" to file "${__output_file}""
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment