Last active
September 16, 2015 19:04
-
-
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.
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 | |
#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" |
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
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