Skip to content

Instantly share code, notes, and snippets.

@rubenmromero
Last active October 21, 2018 11:03
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 rubenmromero/61fe214d51b3f454aeda6757990089e0 to your computer and use it in GitHub Desktop.
Save rubenmromero/61fe214d51b3f454aeda6757990089e0 to your computer and use it in GitHub Desktop.
Bash :: Create and rotate backups of all databases stored in MySQL
#!/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