Skip to content

Instantly share code, notes, and snippets.

@vkushnir
Last active June 23, 2020 06:48
Show Gist options
  • Save vkushnir/9d56c4f1a631aba1b0528e69cf97d54c to your computer and use it in GitHub Desktop.
Save vkushnir/9d56c4f1a631aba1b0528e69cf97d54c to your computer and use it in GitHub Desktop.
MySQLDump with progress bar
#!/bin/sh
#
# Usage: backup.sh -d <database name> [-d <database name>] [-l <login path>] [-f <backup file name>]
TEMP=`mktemp -d`
db_login="backup"
db_list=""
fn="backup"
dt=`date '+%Y%m%d'`
dump (){
db_size=$(mysql --login-path=$db_login --silent --skip-column-names \
-e "SELECT ROUND(SUM(data_length) / 1024 / 1024, 0) \
FROM information_schema.TABLES \
WHERE table_schema='$db_name';")
echo -e "\nDump database \"$db_name\" (${db_size}m)..."
mysqldump --login-path=$db_login \
--single-transaction --order-by-primary --routines --triggers \
$db_name | pv --progress --size "$db_size"m > "${TEMP}/${db_name}_${dt}.sql"
}
while getopts ":d:l:f:" opt; do
case $opt in
l) db_login=$OPTARG ;;
d) db_list="$db_list $OPTARG" ;;
f) fn=$OPTARG ;;
esac
done
if [ -z "$db_list" ]; then
db_list="billing billing_safe radius"
fi
for db_name in $db_list; do
dump
done
echo -e "\nCompress dumped files ..."
tar -cf - --directory=${TEMP} . | pv -s $(du -sb $TEMP | awk '{print $1}') | bzip2 > ${fn}_${dt}.tar.bz2
rm -rf $TEMP
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment