Last active
October 21, 2018 11:03
-
-
Save rubenmromero/61fe214d51b3f454aeda6757990089e0 to your computer and use it in GitHub Desktop.
Bash :: Create and rotate backups of all databases stored in MySQL
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/bash | |
# | |
# Variables Definition | |
# | |
BACKUPS_DIR=/var/backups/database | |
DUMP_REFIX=mysql_dump | |
DUMP_FILE=${BACKUPS_DIR}/${DUMP_REFIX}-$(/bin/date +%Y_%m_%d-%H_%M).sql | |
MYSQL_USER=<superuser> | |
MYSQL_PASSWD='<password>' | |
MYSQL_HOST=localhost | |
NUM_DAYS_ROTATE=7 | |
# Create dump of all databases | |
/usr/bin/mysqldump -u $MYSQL_USER -p$MYSQL_PASSWD -h $MYSQL_HOST --all-databases --events >$DUMP_FILE | |
/bin/gzip $DUMP_FILE | |
# Delete dump files older than value set in $NUM_DAYS_ROTATE variable | |
/bin/find $BACKUPS_DIR -name "${DUMP_REFIX}*" -ctime +$NUM_DAYS_ROTATE -delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment