-
-
Save chengdh/ce0021e6d8d233c0e3921ae3291949e2 to your computer and use it in GitHub Desktop.
Call via crontab on whatever schedule suits you; keep n full mysql Percona xtrabackups of your mysql database, with binary logs applied. Also does a full mysqldump that can then have the binary logs applied to restore to a point-in-time backup via the binlogs. Copy all of this (backup, mysqldump, binlogs) to S3.
This file contains hidden or 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 | |
| # | |
| # Put me in cron.daily, cron.hourly or cron.d for your own custom schedule | |
| # Running daily? You'll keep 3 daily backups | |
| # Running hourly? You'll keep 3 hourly backups | |
| NUM_BACKUPS_TO_KEEP=3 | |
| # Who wants to know when the backup failed, or | |
| # when the binary logs didn't get applied | |
| [email protected] | |
| # Where you keep your backups | |
| BACKUPDIR=/ebs/mysql/backups | |
| # path to innobackupex | |
| XTRABACKUP=/usr/bin/innobackupex | |
| # Add any other files you never want to remove | |
| NEVER_DELETE="lost\+found|\.|\.." | |
| # The mysql user able to access all the databases | |
| OPTIONS="--user=root" | |
| # Shouldn't need to change these... | |
| APPLY_LOG_OPTIONS="--apply-log" | |
| BACKUP="$XTRABACKUP $OPTIONS $BACKUPDIR" | |
| APPLY_BINARY_LOG="$XTRABACKUP $OPTIONS $APPLY_LOG_OPTIONS" | |
| PREV=`ls -rt $BACKUPDIR | tail -n $((NUM_BACKUPS_TO_KEEP+1)) | head -n1 | egrep -v $NEVER_DELETE` | |
| # run a backup | |
| $BACKUP | |
| if [ $? == 0 ]; then | |
| # we got a backup, now we need to apply the binary logs | |
| MOST_RECENT=`ls -rt /ebs/mysql/backups | tail -n1` | |
| $APPLY_BINARY_LOG $BACKUPDIR/$MOST_RECENT | |
| if [ $? == 0 ]; then | |
| # remove backups you don't want to keep | |
| rm -rf $BACKUPDIR/$PREV | |
| else | |
| echo "Couldn't apply the binary logs to the backup $BACKUPDIR/$MOST_RECENT" | mail $EMAIL -s "Mysql binary log didn't get applied to backup" | |
| fi | |
| else | |
| # problem with initial backup :( | |
| echo "Couldn't do a mysql backup" | mail $EMAIL -s "Mysql backup failed" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment