Skip to content

Instantly share code, notes, and snippets.

@windelicato
Last active May 30, 2024 18:50
Show Gist options
  • Save windelicato/7518f4db1ce2c5e6a47761d2170f5d37 to your computer and use it in GitHub Desktop.
Save windelicato/7518f4db1ce2c5e6a47761d2170f5d37 to your computer and use it in GitHub Desktop.
Helper for importing sql dumps into RDS database over SSO connection
#!/bin/bash
# NAME
#
# aws-mysql-import
#
# DESCRIPTION
#
# Helper for importing SQL dumps into mysql server running in AWS.
#
# INSTALLATION
#
# Make script executable and add to your $PATH:
#
# chmod +x /path/to/aws-mysql-import
# mv /path/to/aws-mysql-import /directory/in/your/$PATH
#
# For a progress bar display, install pv:
#
# homebrew install pv
#
# EXAMPLE
#
# aws-mysql-import ~/path/to/dump.sql -P 3307 -u USERNAME -d DATABASE -p PASSWORD --recreate
#
usage() {
echo "Usage:"
echo " docker-mysql-import [/path/to/data.sql]"
echo " [-u user] [-p password] [-d database]"
echo " [-c container_id]"
echo
echo "Required:"
echo " -d , --database"
echo " The MySQL database to import the supplied"
echo " sql file into."
echo
echo "Options:"
echo " -u , --user"
echo " The MySQL user name to use when connecting"
echo " to the container. Defaults to 'root'"
echo
echo " -P , --port"
echo " The MySQL port to use when connecting"
echo " to the container. Defaults to '3306'"
echo
echo " -h , --host"
echo " The MySQL password to use when connecting"
echo " to the container. Defaults to '127.0.0.1'"
echo
echo " -p , --password"
echo " The MySQL password to use when connecting"
echo " to the container. Defaults to 'root'"
echo
echo " --recreate"
echo " Drops database and creates it before import"
echo
exit
}
err() {
echo "$1"
exit 1
}
DATABASE=''
PASSWORD='root'
USER='root'
HOST='127.0.0.1'
PORT='3306'
SQLFILE=''
RECREATE=false
PARAMS=""
while (( "$#" )); do
case "$1" in
-u|--user)
USER=$2
shift 2
;;
-p|--password)
PASSWORD=$2
shift 2
;;
-d|--database)
DATABASE=$2
shift 2
;;
-h|--host)
HOST=$2
shift 2
;;
-P|--port)
PORT=$2
shift 2
;;
--recreate)
RECREATE=true;
shift 1
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
return 0
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# Set positional arguments in their proper place
eval set -- "$PARAMS"
# Ensure sql file path is set
if [[ $# -ne 1 ]] ; then
usage
exit 1
fi
SQL_PATH="$1"
# Support pv if installed for progress monitoring
PIPE="cat"
if hash pv 2>/dev/null; then
PIPE="pv"
fi
MYSQL=`which mysql`
# Do the dang thing
MYSQL_COMMAND="$MYSQL -h $HOST -P $PORT -u $USER -p$PASSWORD"
if $RECREATE; then
echo "Dropping database $DATABASE..."
{ echo "DROP DATABASE IF EXISTS $DATABASE;"\
| $MYSQL_COMMAND; } 2>/dev/null;
echo "Creating database $DATABASE..."
{ echo "CREATE DATABASE IF NOT EXISTS $DATABASE;"\
| $MYSQL_COMMAND; } 2>/dev/null;
fi
echo "Importing $SQL_PATH into database $DATABASE...";
$PIPE $SQL_PATH | $MYSQL_COMMAND $DATABASE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment