Last active
May 29, 2019 12:18
-
-
Save dimitri-koenig/d06fdabc27b72af15f0200e078797340 to your computer and use it in GitHub Desktop.
Backup mysql db both as a whole and with single tables
This file contains 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/sh | |
BACKUP_BASE_DIR=/var/www/backups/db/ | |
MYSQL_USER="" | |
MYSQL_PWD="" | |
MYSQL_DB="" | |
MYSQL_HOST="localhost" | |
TAR="$(which tar)" | |
GZIP="$(which gzip)" | |
MYSQL="$(which mysql)" | |
MYSQLDUMP="$(which mysqldump)" | |
NOW=$(date +"%Y-%m-%d-%H-%M-%S") | |
BACKUPDIR=$BACKUP_BASE_DIR/$MYSQL_DB/$NOW | |
mkdir -p $BACKUPDIR | |
# dump whole db into one file | |
$MYSQLDUMP --add-drop-table --allow-keywords -q --single-transaction -c -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PWD $MYSQL_DB | $GZIP > $BACKUPDIR.sql.gz | |
# additionally dump every table into separate files | |
for db_table in `echo "show tables" | $MYSQL -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PWD $MYSQL_DB|grep -v Tables_in_`; | |
do | |
FILE=$BACKUPDIR/$db_table.sql.gz | |
echo $db_table; $MYSQLDUMP --add-drop-table --allow-keywords -q --single-transaction -c -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PWD $MYSQL_DB $db_table | $GZIP > $FILE | |
done | |
# delete everything older than 30 days | |
find $BACKUP_BASE_DIR/$MYSQL_DB/ -mtime +30 -exec rm -fr {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment