Wednesday, January 6, 2016

Daily backups of all mysql databases (shell script with a fancy date format and retention period)

#!/bin/sh

BACKUP_DIR="/mnt/nfs_backup/backups/mysqldumps"

# Keep daily and ad-hoc backups separately
mkdir -p $BACKUP_DIR/daily
mkdir -p $BACKUP_DIR/longterm

# Perform backup of all databases
# --single-transaction option is used to make "consistent state of the database at the time when START TRANSACTION was issued without blocking any applications."
# --events option is used to avoid warnings (being sent via email from cron). It's for "Event Scheduler events for the dumped databases"
mysqldump --single-transaction --all-databases --events | gzip -9 > $BACKUP_DIR/daily/mysqldump_alldatabases_`date +"%Y%m%d_%H%M%S"`.sql.gz

# Keep daily backups for 20 days
find $BACKUP_DIR/daily -mtime +20 -type f -name "*sql.gz" -exec /bin/rm -rf {} \;

No comments:

Post a Comment