Skip to content

Instantly share code, notes, and snippets.

@benrolfe
Created February 10, 2020 10:45
Show Gist options
  • Save benrolfe/08a5db246689192916a5b2ee1060bbd4 to your computer and use it in GitHub Desktop.
Save benrolfe/08a5db246689192916a5b2ee1060bbd4 to your computer and use it in GitHub Desktop.
Backup script for MySQL on Ubuntu
#!/bin/bash
#----------------------------------------
# OPTIONS
#----------------------------------------
USER='XXXXXX'
PASSWORD='XXXXXX'
DAYS_TO_KEEP=7
GZIP=0
BACKUP_PATH='backups/mysql'
#----------------------------------------
# Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
mkdir -p $BACKUP_PATH
fi
# Get list of database names
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "|" | grep -v Database`
for db in $databases; do
if [ $db == 'information_schema' ] || [ $db == 'performance_schema' ] || [ $db == 'mysql' ] || [ $db == 'sys' ]; then
echo "Skipping database: $db"
continue
fi
date=$(date -I)
echo "Backing up database: $db"
mysqldump -u $USER -p$PASSWORD --databases $db > $BACKUP_PATH/$date-$db.sql
mysqldump -u $USER -p$PASSWORD --databases $db | gzip -c > $BACKUP_PATH/$date-$db.gz
done
# Delete old backups
if [ "$DAYS_TO_KEEP" -gt 0 ] ; then
echo "Deleting backups older than $DAYS_TO_KEEP days"
find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment