Skip to content

Instantly share code, notes, and snippets.

@bdombro
Last active August 29, 2015 14:01
Show Gist options
  • Save bdombro/5d009afe5846298b3940 to your computer and use it in GitHub Desktop.
Save bdombro/5d009afe5846298b3940 to your computer and use it in GitHub Desktop.
#!/bin/bash
usage() {
echo "
Version 1.0
Fancy mysqldump for full DBs that shows progress.
Syntax: bmysqldump [-u <username>] [-p] [-h <hostname>] <database>
"
exit 0
}
if [[ "$#" == 0 ]]; then
usage
fi
########## Parse and get params
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
user=""
mpwd=""
pwd_flag=0
host=""
mysqlargs=""
zip_flag=0
while getopts "u:ph:z" opt; do
case "$opt" in
# h|\?)
# usage
# ;;
u) user=$OPTARG
mysqlargs="$mysqlargs -u $user"
;;
p) pwd_flag=1;;
h) host=$OPTARG
mysqlargs="$mysqlargs -h $host"
;;
z) zip_flag=1;;
?)
usage;;
esac
done
shift $((OPTIND-1))
db=$1
if [[ $pwd_flag == 1 ]]; then
read -s -p "Enter Mysql PWD: " mpwd; echo
mysqlargs="$mysqlargs -p$mpwd"
fi
######### Perform dump
dbsize=$(mysql $mysqlargs -Bse "SELECT SUM(data_length) 'DB Size in B' FROM information_schema.TABLES WHERE table_schema = '$db'" 2> /dev/null)
if [[ $zip_flag == 1 ]]; then
mysqldump $mysqlargs --routines --triggers --quick $db | pv -s $dbsize | gzip > $db.sql.gz
else
mysqldump $mysqlargs --routines --triggers --quick $db | pv -s $dbsize
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment