function mdi { test -z "$2" && echo "Usage: mdi /path/to/dump.sql.gz [DB name]" && return # TODO # * fail gracefully if pv doesn't exist # * option for --drop so drop the db and re-create it? dbdump=$1 db=$2 if [[ ! -e $dbdump ]]; then echo "The file $dbdump doesn't exist. Maybe you're mixing up the order of options you're giving mdi" return 1 fi if ! mysql -s -u"$(grep ^rootdsn= /usr/local/interworx/iworx.ini | cut -d/ -f3 | cut -d: -f1)" -p"$(grep ^rootdsn= /home/interworx/iworx.ini | cut -d: -f3 | cut -d\@ -f1)" -e "USE ${db}" >/dev/null 2>&1; then echo "${db} database does not exist, you need to create it and set up the mysql users." return 1 fi if [ -x /usr/bin/unpigz ]; then GUNZIP='/usr/bin/unpigz' else GUNZIP='/bin/gunzip' fi if [ -x /usr/bin/pbunzip2 ]; then BUNZIP='/usr/bin/pbunzip2' else BUNZIP='/usr/bin/bunzip2' fi # go by what what file says the file is instead of what the extension is # since people do dumb stuff if [[ $(file -b $dbdump) =~ ^gzip ]]; then pv $dbdump | $GUNZIP -c | mysql -u"$(grep ^rootdsn= /usr/local/interworx/iworx.ini | cut -d/ -f3 | cut -d: -f1)" -p"$(grep ^rootdsn= /home/interworx/iworx.ini | cut -d: -f3 | cut -d\@ -f1)" "${db}" elif [[ $(file -b $dbdump) =~ ^bzip2 ]]; then pv $dbdump | $BUNZIP -c | mysql -u"$(grep ^rootdsn= /usr/local/interworx/iworx.ini | cut -d/ -f3 | cut -d: -f1)" -p"$(grep ^rootdsn= /home/interworx/iworx.ini | cut -d: -f3 | cut -d\@ -f1)" "${db}" else pv $dbdump | mysql -u"$(grep ^rootdsn= /usr/local/interworx/iworx.ini | cut -d/ -f3 | cut -d: -f1)" -p"$(grep ^rootdsn= /home/interworx/iworx.ini | cut -d: -f3 | cut -d\@ -f1)" "${db}" fi }