Skip to content

Instantly share code, notes, and snippets.

@dreamingechoes
Last active October 13, 2016 11:35
Show Gist options
  • Save dreamingechoes/14b063bc5b6602aae4c62a92de984235 to your computer and use it in GitHub Desktop.
Save dreamingechoes/14b063bc5b6602aae4c62a92de984235 to your computer and use it in GitHub Desktop.
Script to generate dumps of all our local databases on MySQL.
#!/bin/bash
# Suffix added at the end of the filename
NOW=$(date +"%d%m%Y")
# Destination folder of all the dumps
DEST="backups"
# Set mysql login info
MUSER="root"
MPASS=""
MHOST="127.0.0.1"
# Guess binary names
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
[ ! -d "${DEST}" ] && mkdir -p "${DEST}"
# Get all db names
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=${DEST}/${db}_${NOW}.gz
$MYSQLDUMP --single-transaction -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment