Skip to content

Instantly share code, notes, and snippets.

@rufhausen
Last active September 16, 2015 19:04
Show Gist options
  • Save rufhausen/9398648 to your computer and use it in GitHub Desktop.
Save rufhausen/9398648 to your computer and use it in GitHub Desktop.
Local MySQL backup with "hidden" credentials. Backups over 30 days old are removed.
#!/bin/bash
#MYSQL DATABASE BACKUP
#Author: Gary Taylor <[email protected]>
#Credentials are stored in a separate file that you can keep them out of your repository while including the dbbackup script itself.
#Credentials Location
source $PWD/.backup.vars
#BACKUP FILE NAMING
BACKUP_FILE=$DATABASE-$HOST-$(date +"%Y-%m-%d-%H-%M-%S")
BACKUP_FILE_PATH=$BACKUP_DIR$BACKUP_FILE'.gz'
#CREATE THE BACKUP
mysqldump --opt --single-transaction -u $USERNAME -p$PASSWORD $DATABASE | gzip > $BACKUP_FILE_PATH
echo "$BACKUP_FILE created"
FILE_SIZE=$(ls -lah $BACKUP_FILE_PATH | awk '{ print $5}')
#REMOVE BACKUPS OLDER THAN 30 DAYS
find $BACKUP_DIR -mtime +30 -exec rm {} \;
echo "Removed backups older than 30 days"
#EMAIL BACKUP NOTIFICATION
echo "$BACKUP_FILE operation completed (Size: $FILE_SIZE)" | mutt -s "$HOST Database Backup Notification" $EMAIL_NOTIFY
echo "Backup operation complete"
DATABASE=''
USERNAME=''
PASSWORD=''
HOST=$(hostname)
BACKUP_DIR='[full_path]'
EMAIL_NOTIFY=''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment