Skip to content

Instantly share code, notes, and snippets.

@chengdh
Forked from jaygooby/percona-xtrabackup.sh
Created August 28, 2016 00:49
Show Gist options
  • Save chengdh/ce0021e6d8d233c0e3921ae3291949e2 to your computer and use it in GitHub Desktop.
Save chengdh/ce0021e6d8d233c0e3921ae3291949e2 to your computer and use it in GitHub Desktop.

Revisions

  1. @jaygooby jaygooby revised this gist Feb 11, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -81,6 +81,10 @@ if [ $? == 0 ]; then

    # we got a backup, now we need to apply the binary logs
    MOST_RECENT=$(ls $BACKUPDIR | grep -v $DUMPS | sort | tail -n1)

    # ensure the generated cnf file used by xtrabackup has innodb_file_per_table set
    # otherwise using the --export switch will fail
    echo "innodb_file_per_table" >> $BACKUPDIR/$MOST_RECENT/backup-my.cnf
    $APPLY_BINARY_LOG $BACKUPDIR/$MOST_RECENT

    # binary logs were applied OK, so we have a brand new percona backup
  2. @jaygooby jaygooby revised this gist Jan 30, 2015. 1 changed file with 77 additions and 10 deletions.
    87 changes: 77 additions & 10 deletions percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,49 @@
    #!/bin/bash
    #
    # Carries out a full mysqldump, calls percona-xtrabackup and then
    # copies the sql dump, the percona backup and your mysql bin logs
    # to S3 using s3cmd http://s3tools.org/s3cmd
    #
    # TODO: extract out the S3 backup stuff to make it optional, and so
    # other s3 programs can replace the s3cmd call.
    # TODO: the if [ $? == 0 ] alert blocks should be a function
    # TODO: make the if [ $? == 0 ] if [ $? != 0 ] more consistent - test
    # for failure first in all cases

    # 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
    # Who wants to know when the backup failed
    [email protected]

    # Your mysql bucket, where the backups and dumps will get
    # copied to
    S3_BUCKET=logs.example.com/mysql

    # Where you keep your backups
    BACKUPDIR=/ebs/mysql/backups

    # The folder in $BACKUPDIR where you'll keep your mysqldumps
    DUMPS=dumps
    DUMPDIR=$BACKUPDIR/$DUMPS

    # How you'll do a mysqldump
    # the --master-data=2 means we're able to do point-in-time recovery
    # using the binlogs that we'll also backup
    MYSQLDUMP=/usr/bin/mysqldump
    MYSQLDUMP_OPTIONS="-u root -h 127.0.0.1 --all-databases --single-transaction --default-character-set=latin1 --skip-set-charset --master-data=2"

    # Where your mysql binary logs live
    MYSQL_BIN_LOG_DIR=/var/mysql/lib/mysql

    # path to innobackupex
    XTRABACKUP=/usr/bin/innobackupex

    # Add any other files you never want to remove
    NEVER_DELETE="lost\+found|\.|\.."
    NEVER_DELETE="lost\+found|\.|\..|$DUMPS"

    # The mysql user able to access all the databases
    OPTIONS="--user=root"
    @@ -26,28 +52,69 @@ OPTIONS="--user=root"
    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`

    # The dumps and percona backups we'll be removing
    PREV=$(ls $BACKUPDIR | sort | grep -v "$(ls $BACKUPDIR | tail -n $NUM_BACKUPS_TO_KEEP)" | grep -v $DUMPDIR)
    PREVDUMP=$(ls $DUMPDIR | sort | grep -v "$(ls $DUMPDIR | tail -n $NUM_DUMPS_TO_KEEP)")

    # do a mysqldump
    $DUMP | /bin/gzip -9 > $DUMPDIR/$(date +%Y%m%d_%H:%M).sql.gz

    # we got a full dump
    if [ $? == 0 ]; then
    # only remove if $PREVDUMP is set
    if [ -n "$PREVDUMP" ]; then
    # remove dumps you don't want to keep
    cd $DUMPDIR && rm -rf $PREVDUMP
    if [ $? != 0 ]; then
    echo "Looks like there was a problem deleting $PREVDUMP in $DUMPDIR" | mail $EMAIL -s "Couldn't remove $PREVDUMP in $DUMPDIR"
    fi
    fi
    else
    echo "Couldn't do the mysqldump to $DUMPDIR" | mail $EMAIL -s "mysqldump to $DUMPDIR failed"
    fi

    # run a backup
    $BACKUP

    if [ $? == 0 ]; then

    # we got a backup, now we need to apply the binary logs
    MOST_RECENT=`ls -rt $BACKUPDIR | tail -n1`
    MOST_RECENT=$(ls $BACKUPDIR | grep -v $DUMPS | sort | tail -n1)
    $APPLY_BINARY_LOG $BACKUPDIR/$MOST_RECENT

    if [ $? == 0 ]; then
    # binary logs were applied OK, so we have a brand new percona backup
    # so remove older backups we don't need
    if [ $? == 0 ]; then
    # only remove if $PREV is set
    if [ -n "$PREV" ]; then
    # remove backups you don't want to keep
    rm -rf $BACKUPDIR/$PREV
    cd $BACKUPDIR && rm -rf $PREV
    if [ $? != 0 ]; then
    echo "Looks like there was a problem deleting $PREV in $BACKUPDIR"
    fi
    fi

    # Sync the percona backups to S3
    # ionice the s3 sync, so we don't block the mysql server
    /usr/bin/ionice -c 2 -n 6 /usr/bin/s3cmd sync --delete-removed $BACKUPDIR/* s3://$S3_BUCKET/backups/

    # alert if the s3 sync failed
    if [ $? != 0 ]; then
    echo "Looks like there was a problem with the mysql backup syncing to S3" | mail $EMAIL -s "Couldn't copy mysql backup to S3"
    else
    # now back up the latest binlogs too, we'll need these if we want to do a point-in-time restore (which the mysqldump is prepped for)
    /usr/bin/ionice -c 2 -n 6 /usr/bin/s3cmd sync --delete-removed $MYSQL_BIN_LOG_DIR/mysql-bin* s3://$S3_BUCKET/binlogs/
    if [ $? != 0 ]; then
    echo "Couldn't sync the SQL binlogs to S3 - you won't be able do a point-in-time recovery" | mail $EMAIL -s "Mysql binlogs not copied to S3"
    fi
    fi

    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"
    # problem with initial backup :(
    echo "Couldn't do a mysql backup" | mail $ALERT -s "Mysql backup failed"
    fi
  3. @jaygooby jaygooby revised this gist Jun 19, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ if [ $? == 0 ]; then

    if [ $? == 0 ]; then
    # only remove if $PREV is set
    if [[ -n "$PREV" ]]; then
    if [ -n "$PREV" ]; then
    # remove backups you don't want to keep
    rm -rf $BACKUPDIR/$PREV
    fi
  4. @jaygooby jaygooby revised this gist Jun 19, 2014. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -38,8 +38,11 @@ if [ $? == 0 ]; then
    $APPLY_BINARY_LOG $BACKUPDIR/$MOST_RECENT

    if [ $? == 0 ]; then
    # remove backups you don't want to keep
    rm -rf $BACKUPDIR/$PREV
    # only remove if $PREV is set
    if [[ -n "$PREV" ]]; then
    # remove backups you don't want to keep
    rm -rf $BACKUPDIR/$PREV
    fi
    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
  5. @jaygooby jaygooby revised this gist Nov 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ $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`
    MOST_RECENT=`ls -rt $BACKUPDIR | tail -n1`
    $APPLY_BINARY_LOG $BACKUPDIR/$MOST_RECENT

    if [ $? == 0 ]; then
  6. @jaygooby jaygooby revised this gist Mar 20, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -41,10 +41,10 @@ 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 protected] -s "Mysql binary log didn't get applied to backup"
    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 protected] -s "Mysql backup failed"
    echo "Couldn't do a mysql backup" | mail $EMAIL -s "Mysql backup failed"
    fi
  7. @jaygooby jaygooby revised this gist Mar 20, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@ [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
  8. @jaygooby jaygooby created this gist Mar 20, 2013.
    48 changes: 48 additions & 0 deletions percona-xtrabackup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/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
    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 protected] -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 protected] -s "Mysql backup failed"
    fi