Skip to content

Instantly share code, notes, and snippets.

@antongorodezkiy
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antongorodezkiy/9488469 to your computer and use it in GitHub Desktop.
Save antongorodezkiy/9488469 to your computer and use it in GitHub Desktop.
Simple mysql backup
#!/bin/bash
# crontab
# 1 1 * * * root /bin/bash /path/to/script/mysql_backup.sh > /var/log/cron.log
### MySQL Server Login Info ###
MUSER="root" # mysql username, change this if needed
MPASS="root_pass" # mysql user password, change this
MHOST="localhost" # mysql host, change this if needed
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/path/to/backup/dir/" # path to backups dir, change this
GZIP="$(which gzip)"
NOW=$(date +"%d-%m-%Y")
### See comments below ###
### [ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/* ###
[ ! -d "$BAK" ] && mkdir -p "$BAK"
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
if [[ "$db" != "information_schema" && "$db" != "performance_schema" ]];
then
FILE=$BAK/$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment