Skip to content

Instantly share code, notes, and snippets.

@eburhan
Forked from dalecaru/innobackupex-restore.sh
Created March 1, 2016 14:35
Show Gist options
  • Select an option

  • Save eburhan/0db00af9fbe2cd701a15 to your computer and use it in GitHub Desktop.

Select an option

Save eburhan/0db00af9fbe2cd701a15 to your computer and use it in GitHub Desktop.

Revisions

  1. Damian Caruso revised this gist Mar 2, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions innobackupex-restore.sh
    Original file line number Diff line number Diff line change
    @@ -85,10 +85,15 @@ else
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --apply-log --redo-only --use-memory=$MEMORY $FULLBACKUP > $TMPFILE 2>&1
    check_innobackupex_error

    # Apply incrementals to base backup
    for i in `find $PARENT_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -n`; do
    echo "Applying $i to full ..."
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --apply-log --redo-only --use-memory=$MEMORY $FULLBACKUP --incremental-dir=$PARENT_DIR/$i > $TMPFILE 2>&1
    check_innobackupex_error

    if [ $INCR = $i ]; then
    break # break. we are restoring up to this incremental.
    fi
    done
    else
    error "unknown backup type"
  2. Damian Caruso revised this gist Mar 2, 2012. 1 changed file with 113 additions and 0 deletions.
    113 changes: 113 additions & 0 deletions innobackupex-restore.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    #!/bin/sh
    #
    # Script to prepare and restore full and incremental backups created with innobackupex-runner.
    #
    # This script is provided as-is; no liability can be accepted for use.
    #

    INNOBACKUPEX=innobackupex-1.5.1
    INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
    TMPFILE="/tmp/innobackupex-restore.$$.tmp"
    MYCNF=/etc/mysql/my.cnf
    BACKUPDIR=/backup # Backups base directory
    FULLBACKUPDIR=$BACKUPDIR/full # Full backups directory
    INCRBACKUPDIR=$BACKUPDIR/incr # Incremental backups directory
    MEMORY=1024M # Amount of memory to use when preparing the backup

    #############################################################################
    # Display error message and exit
    #############################################################################
    error()
    {
    echo "$1" 1>&2
    exit 1
    }

    #############################################################################
    # Check for errors in innobackupex output
    #############################################################################
    check_innobackupex_error()
    {
    if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
    echo "$INNOBACKUPEX failed:"; echo
    echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
    cat $TMPFILE
    rm -f $TMPFILE
    exit 1
    fi
    }

    # Check options before proceeding
    if [ ! -x $INNOBACKUPEXFULL ]; then
    error "$INNOBACKUPEXFULL does not exist."
    fi

    if [ ! -d $BACKUPDIR ]; then
    error "Backup destination folder: $BACKUPDIR does not exist."
    fi

    if [ $# != 1 ] ; then
    error "Usage: $0 /absolute/path/to/backup/to/restore"
    fi

    if [ ! -d $1 ]; then
    error "Backup to restore: $1 does not exist."
    fi

    # Some info output
    echo "----------------------------"
    echo
    echo "$0: MySQL backup script"
    echo "started: `date`"
    echo

    PARENT_DIR=`dirname $1`

    if [ $PARENT_DIR = $FULLBACKUPDIR ]; then
    FULLBACKUP=$1

    echo "Restore `basename $FULLBACKUP`"
    echo
    else
    if [ `dirname $PARENT_DIR` = $INCRBACKUPDIR ]; then
    INCR=`basename $1`
    FULL=`basename $PARENT_DIR`
    FULLBACKUP=$FULLBACKUPDIR/$FULL

    if [ ! -d $FULLBACKUP ]; then
    error "Full backup: $FULLBACKUP does not exist."
    fi

    echo "Restore $FULL up to incremental $INCR"
    echo

    echo "Replay committed transactions on full backup"
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --apply-log --redo-only --use-memory=$MEMORY $FULLBACKUP > $TMPFILE 2>&1
    check_innobackupex_error

    for i in `find $PARENT_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -n`; do
    echo "Applying $i to full ..."
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --apply-log --redo-only --use-memory=$MEMORY $FULLBACKUP --incremental-dir=$PARENT_DIR/$i > $TMPFILE 2>&1
    check_innobackupex_error
    done
    else
    error "unknown backup type"
    fi
    fi

    echo "Preparing ..."
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --apply-log --use-memory=$MEMORY $FULLBACKUP > $TMPFILE 2>&1
    check_innobackupex_error

    echo
    echo "Restoring ..."
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --copy-back $FULLBACKUP > $TMPFILE 2>&1
    check_innobackupex_error

    rm -f $TMPFILE
    echo "Backup restored successfully. You are able to start mysql now."
    echo "Verify files ownership in mysql data dir."
    echo "Run 'chown -R mysql:mysql /path/to/data/dir' if necessary."
    echo
    echo "completed: `date`"
    exit 0
  3. Damian Caruso revised this gist Mar 2, 2012. 1 changed file with 22 additions and 21 deletions.
    43 changes: 22 additions & 21 deletions innobackupex-runner.sh
    Original file line number Diff line number Diff line change
    @@ -27,41 +27,42 @@ KEEP=1 # Number of full backups (and its incrementals) to keep
    # Grab start time
    STARTED_AT=`date +%s`

    # Some info output
    echo "----------------------------"
    echo
    echo "innobackupex-runner.sh: MySQL backup script"
    echo "started: `date`"
    echo
    #############################################################################
    # Display error message and exit
    #############################################################################
    error()
    {
    echo "$1" 1>&2
    exit 1
    }

    # Check options before proceeding

    if [ ! -x $INNOBACKUPEXFULL ]; then
    error
    echo "$INNOBACKUPEXFULL does not exist."; echo
    exit 1
    error "$INNOBACKUPEXFULL does not exist."
    fi

    if [ ! -d $BACKUPDIR ]; then
    error
    echo "Backup destination folder: $BACKUPDIR does not exist."; echo
    exit 1
    else
    mkdir -p $FULLBACKUPDIR
    mkdir -p $INCRBACKUPDIR
    error "Backup destination folder: $BACKUPDIR does not exist."
    fi

    if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then
    echo "HALTED: MySQL does not appear to be running."; echo
    exit 1
    error "HALTED: MySQL does not appear to be running."
    fi

    if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then
    echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)."; echo
    exit 1
    error "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)."
    fi

    echo "Check completed OK."
    # Some info output
    echo "----------------------------"
    echo
    echo "$0: MySQL backup script"
    echo "started: `date`"
    echo

    # Create full and incr backup directories if they not exist.
    mkdir -p $FULLBACKUPDIR
    mkdir -p $INCRBACKUPDIR

    # Find latest full backup
    LATEST_FULL=`find $FULLBACKUPDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
  4. Damian Caruso revised this gist Mar 1, 2012. 1 changed file with 55 additions and 37 deletions.
    92 changes: 55 additions & 37 deletions innobackupex-runner.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    #!/bin/sh
    #
    # Script to create full and incremental backups (for all databases on server) using innobackupex from Percona.
    # http://www.percona.com/doc/percona-xtrabackup/innobackupex/innobackupex_script.html
    #
    # Script to run innobackupex script (for all databases on server), check for success, and apply logs to backups.
    # Every time it runs will generate an incremental backup except for the first time (full backup).
    # FULLBACKUPLIFE variable will define your full backups schedule.
    #
    # (C)2010 Owen Carter @ Mirabeau BV
    # This script is provided as-is; no liability can be accepted for use.
    @@ -10,18 +14,20 @@
    INNOBACKUPEX=innobackupex-1.5.1
    INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
    USEROPTIONS="--user=root --password=XXXXXX"
    BACKUPDIR=/backup
    TMPFILE="/tmp/innobackupex-runner.$$.tmp"
    MYCNF=/etc/mysql/my.cnf
    MEMORY=512M # Amount of memory to use when preparing the backup
    MYSQL=/usr/bin/mysql
    MYSQLADMIN=/usr/bin/mysqladmin
    BACKUPDIR=/backup # Backups base directory
    FULLBACKUPDIR=$BACKUPDIR/full # Full backups directory
    INCRBACKUPDIR=$BACKUPDIR/incr # Incremental backups directory
    FULLBACKUPLIFE=86400 # Lifetime of the latest full backup in seconds
    KEEP=1 # Number of full backups (and its incrementals) to keep

    # Age of oldest retained backups in minutes.
    AGE=5000
    # Grab start time
    STARTED_AT=`date +%s`

    # Some info output

    echo "----------------------------"
    echo
    echo "innobackupex-runner.sh: MySQL backup script"
    @@ -31,15 +37,18 @@ echo
    # Check options before proceeding

    if [ ! -x $INNOBACKUPEXFULL ]; then
    error
    echo "$INNOBACKUPEXFULL does not exist."; echo
    exit 1
    error
    echo "$INNOBACKUPEXFULL does not exist."; echo
    exit 1
    fi

    if [ ! -d $BACKUPDIR ]; then
    error
    echo "Backup destination folder: $BACKUPDIR does not exist."; echo
    exit 1
    error
    echo "Backup destination folder: $BACKUPDIR does not exist."; echo
    exit 1
    else
    mkdir -p $FULLBACKUPDIR
    mkdir -p $INCRBACKUPDIR
    fi

    if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then
    @@ -48,15 +57,40 @@ if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then
    fi

    if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then
    echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)"; echo
    echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)."; echo
    exit 1
    fi

    # Now run the command to produce the backup; capture it's output.

    echo "Check completed OK; running $INNOBACKUPEX command."

    $INNOBACKUPEXFULL --defaults-file=$MYCNF $USEROPTIONS $BACKUPDIR > $TMPFILE 2>&1
    echo "Check completed OK."

    # Find latest full backup
    LATEST_FULL=`find $FULLBACKUPDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`

    # Get latest backup last modification time
    LATEST_FULL_CREATED_AT=`stat -c %Y $FULLBACKUPDIR/$LATEST_FULL`

    # Run an incremental backup if latest full is still valid. Otherwise, run a new full one.
    if [ "$LATEST_FULL" -a `expr $LATEST_FULL_CREATED_AT + $FULLBACKUPLIFE + 5` -ge $STARTED_AT ] ; then
    # Create incremental backups dir if not exists.
    TMPINCRDIR=$INCRBACKUPDIR/$LATEST_FULL
    mkdir -p $TMPINCRDIR

    # Find latest incremental backup.
    LATEST_INCR=`find $TMPINCRDIR -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`

    # If this is the first incremental, use the full as base. Otherwise, use the latest incremental as base.
    if [ ! $LATEST_INCR ] ; then
    INCRBASEDIR=$FULLBACKUPDIR/$LATEST_FULL
    else
    INCRBASEDIR=$LATEST_INCR
    fi

    echo "Running new incremental backup using $INCRBASEDIR as base."
    $INNOBACKUPEXFULL --defaults-file=$MYCNF $USEROPTIONS --incremental $TMPINCRDIR --incremental-basedir $INCRBASEDIR > $TMPFILE 2>&1
    else
    echo "Running new full backup."
    $INNOBACKUPEXFULL --defaults-file=$MYCNF $USEROPTIONS $FULLBACKUPDIR > $TMPFILE 2>&1
    fi

    if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
    echo "$INNOBACKUPEX failed:"; echo
    @@ -71,27 +105,11 @@ rm -f $TMPFILE

    echo "Databases backed up successfully to: $THISBACKUP"
    echo
    echo "Now applying logs to the backuped databases"

    # Run the command to apply the logfiles to the backup directory.
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --apply-log --use-memory=$MEMORY $THISBACKUP > $TMPFILE 2>&1

    if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
    echo "$INNOBACKUPEX --apply-log failed:"; echo
    echo "---------- ERROR OUTPUT from $INNOBACKUPEX --apply-log ----------"
    cat $TMPFILE
    rm -f $TMPFILE
    exit 1
    fi

    echo "Logs applied to backuped databases"
    echo

    # Cleanup

    echo "Cleaning up old backups (older than $AGE minutes) and temporary files"
    rm -f $TMPFILE
    cd /tmp ; find $BACKUPDIR -maxdepth 1 -type d -mmin +$AGE -exec echo "removing: "{} \; -exec rm -rf {} \;
    echo "Cleanup. Keeping only $KEEP full backups and its incrementals."
    AGE=$(($FULLBACKUPLIFE * $KEEP / 60))
    find $FULLBACKUPDIR -maxdepth 1 -type d -mmin +$AGE -execdir echo "removing: "$FULLBACKUPDIR/{} \; -execdir rm -rf $FULLBACKUPDIR/{} \; -execdir echo "removing: "$INCRBACKUPDIR/{} \; -execdir rm -rf $INCRBACKUPDIR/{} \;

    echo
    echo "completed: `date`"
  5. Damian Caruso revised this gist Dec 21, 2011. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions innobackupex-runner.sh
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,8 @@ BACKUPDIR=/backup
    TMPFILE="/tmp/innobackupex-runner.$$.tmp"
    MYCNF=/etc/mysql/my.cnf
    MEMORY=512M # Amount of memory to use when preparing the backup
    MYSQL=/usr/bin/mysql
    MYSQLADMIN=/usr/bin/mysqladmin

    # Age of oldest retained backups in minutes.
    AGE=5000
    @@ -40,12 +42,12 @@ if [ ! -d $BACKUPDIR ]; then
    exit 1
    fi

    if [ -z "`/sbin/service mysql status | grep 'MySQL running (.*)'`" ] ; then
    if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then
    echo "HALTED: MySQL does not appear to be running."; echo
    exit 1
    fi

    if ! `echo 'exit' | /usr/bin/mysql -s $USEROPTIONS` ; then
    if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then
    echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)"; echo
    exit 1
    fi
  6. Damian Caruso revised this gist Dec 21, 2011. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions innobackupex-runner.sh
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@ INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
    USEROPTIONS="--user=root --password=XXXXXX"
    BACKUPDIR=/backup
    TMPFILE="/tmp/innobackupex-runner.$$.tmp"
    MYCNF=/etc/mysql/my.cnf
    MEMORY=512M # Amount of memory to use when preparing the backup

    # Age of oldest retained backups in minutes.
    AGE=5000
    @@ -52,7 +54,7 @@ fi

    echo "Check completed OK; running $INNOBACKUPEX command."

    $INNOBACKUPEXFULL $USEROPTIONS --defaults-file=/etc/my.cnf $BACKUPDIR > $TMPFILE 2>&1
    $INNOBACKUPEXFULL --defaults-file=$MYCNF $USEROPTIONS $BACKUPDIR > $TMPFILE 2>&1

    if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
    echo "$INNOBACKUPEX failed:"; echo
    @@ -70,7 +72,7 @@ echo
    echo "Now applying logs to the backuped databases"

    # Run the command to apply the logfiles to the backup directory.
    $INNOBACKUPEXFULL --apply-log --use-memory=1024 --defaults-file=/etc/my.cnf $THISBACKUP > $TMPFILE 2>&1
    $INNOBACKUPEXFULL --defaults-file=$MYCNF --apply-log --use-memory=$MEMORY $THISBACKUP > $TMPFILE 2>&1

    if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
    echo "$INNOBACKUPEX --apply-log failed:"; echo
    @@ -91,4 +93,4 @@ cd /tmp ; find $BACKUPDIR -maxdepth 1 -type d -mmin +$AGE -exec echo "removing:

    echo
    echo "completed: `date`"
    exit 0
    exit 0
  7. Damian Caruso created this gist Apr 20, 2011.
    94 changes: 94 additions & 0 deletions innobackupex-runner.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    #!/bin/sh
    #
    # Script to run innobackupex script (for all databases on server), check for success, and apply logs to backups.
    #
    # (C)2010 Owen Carter @ Mirabeau BV
    # This script is provided as-is; no liability can be accepted for use.
    # You are free to modify and reproduce so long as this attribution is preserved.
    #

    INNOBACKUPEX=innobackupex-1.5.1
    INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
    USEROPTIONS="--user=root --password=XXXXXX"
    BACKUPDIR=/backup
    TMPFILE="/tmp/innobackupex-runner.$$.tmp"

    # Age of oldest retained backups in minutes.
    AGE=5000

    # Some info output

    echo "----------------------------"
    echo
    echo "innobackupex-runner.sh: MySQL backup script"
    echo "started: `date`"
    echo

    # Check options before proceeding

    if [ ! -x $INNOBACKUPEXFULL ]; then
    error
    echo "$INNOBACKUPEXFULL does not exist."; echo
    exit 1
    fi

    if [ ! -d $BACKUPDIR ]; then
    error
    echo "Backup destination folder: $BACKUPDIR does not exist."; echo
    exit 1
    fi

    if [ -z "`/sbin/service mysql status | grep 'MySQL running (.*)'`" ] ; then
    echo "HALTED: MySQL does not appear to be running."; echo
    exit 1
    fi

    if ! `echo 'exit' | /usr/bin/mysql -s $USEROPTIONS` ; then
    echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)"; echo
    exit 1
    fi

    # Now run the command to produce the backup; capture it's output.

    echo "Check completed OK; running $INNOBACKUPEX command."

    $INNOBACKUPEXFULL $USEROPTIONS --defaults-file=/etc/my.cnf $BACKUPDIR > $TMPFILE 2>&1

    if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
    echo "$INNOBACKUPEX failed:"; echo
    echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
    cat $TMPFILE
    rm -f $TMPFILE
    exit 1
    fi

    THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
    rm -f $TMPFILE

    echo "Databases backed up successfully to: $THISBACKUP"
    echo
    echo "Now applying logs to the backuped databases"

    # Run the command to apply the logfiles to the backup directory.
    $INNOBACKUPEXFULL --apply-log --use-memory=1024 --defaults-file=/etc/my.cnf $THISBACKUP > $TMPFILE 2>&1

    if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
    echo "$INNOBACKUPEX --apply-log failed:"; echo
    echo "---------- ERROR OUTPUT from $INNOBACKUPEX --apply-log ----------"
    cat $TMPFILE
    rm -f $TMPFILE
    exit 1
    fi

    echo "Logs applied to backuped databases"
    echo

    # Cleanup

    echo "Cleaning up old backups (older than $AGE minutes) and temporary files"
    rm -f $TMPFILE
    cd /tmp ; find $BACKUPDIR -maxdepth 1 -type d -mmin +$AGE -exec echo "removing: "{} \; -exec rm -rf {} \;

    echo
    echo "completed: `date`"
    exit 0