Last active
August 29, 2015 13:57
-
-
Save antongorodezkiy/9488469 to your computer and use it in GitHub Desktop.
Simple mysql backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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