Skip to content

Instantly share code, notes, and snippets.

@ivan-pinatti
Last active April 19, 2021 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ivan-pinatti/98ec6b839e4ff79bb67d33a9f24ebf89 to your computer and use it in GitHub Desktop.
Save ivan-pinatti/98ec6b839e4ff79bb67d33a9f24ebf89 to your computer and use it in GitHub Desktop.
Import MySQL database with optimized options - #mysql #database #import #bash
#!/usr/bin/env bash
: ' A simple MySQL import 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"}"
__input_file="${6:-"/tmp/${__mysql_database}.gz"}"
# binaries
__MYSQL=$(which mysql)
__ZCAT=$(which zcat)
# create mysql import command
readonly __mysql_import_string="${__ZCAT} "${__input_file}" \
| ${__MYSQL} --user="${__mysql_user}" \
--password="${__mysql_password}" \
--host="${__mysql_host}" \
--port="${__mysql_port}" \
--database="${__mysql_database}""
# try to import dump file
echo "Importing database "${__mysql_database}" from file "${__input_file}", please wait..."
if ! eval $__mysql_import_string; then
echo "ERROR! MySQL could not perform import for dump file "${__input_file}" to database "${__mysql_database}""
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment