Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment