Skip to content

Instantly share code, notes, and snippets.

@codeachange
Last active December 17, 2015 20:28
Show Gist options
  • Save codeachange/5667398 to your computer and use it in GitHub Desktop.
Save codeachange/5667398 to your computer and use it in GitHub Desktop.
#!/bin/bash
# MySQL Server Info
MyUSER="root"
MyPASS="passwd"
MyHOST="localhost"
# Linux bin paths, change this if it can not be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
# Backup Dest directory, change this if you have someother location
DEST="/var/www/backup"
# Main directory where backup will be stored
MBD="$DEST/mysql"
# Get data in yyyy-mm-dd format
NOW="$(date +"%Y-%m-%d")"
# File to store current backup file
FILE=""
# Store list of databases
DBS=""
# DO NOT BACKUP these databases
IGGY="information_schema mysql performance_schema"
[ ! -d $MBD ] && mkdir -p $MBD || :
# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
skipdb=-1
if [ "$IGGY" != "" ]; then
for i in $IGGY
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi
if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.$NOW.sql.gz"
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS --opt $db | $GZIP -9 > $FILE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment