Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Last active July 24, 2018 14:40
Show Gist options
  • Save ilyaevseev/b60b2e7d7d4738a80910 to your computer and use it in GitHub Desktop.
Save ilyaevseev/b60b2e7d7d4738a80910 to your computer and use it in GitHub Desktop.
Innobackup usage sample.
#!/bin/sh
BASEDIR="/var/lib/mysqlbackup"
TSTAMP="$(date +%Y.%m.%d_%H%M%S)"
DESTDIR="$BASEDIR/new"
PREVDIR="$BASEDIR/full"
LOGSDIR="$BASEDIR/logs"
LOGFILE="$LOGSDIR/$TSTAMP.log"
Fail() {
logger -p user.err "$0: $@"
echo "$@" | mail -s "InnoBackup failed" admins
exit 1
}
mkdir -p "$LOGSDIR" || Fail "Cannot create $LOGDIR"
cd "$BASEDIR" || Fail "Cannot chdir $BASEDIR"
which rsync >/dev/null && rsync_opt="--rsync" || rsync_opt=""
RunStage() {
stage="$1"; shift;
innobackupex --defaults-file="/etc/mysql/debian.cnf" "$@" "$DESTDIR/" > "$LOGFILE-$stage" 2>&1
grep -q "innobackupex: completed OK" "$LOGFILE-$stage" && return
Fail "Stage $stage failed, see $LOGFILE-$stage"
}
RunStage 1 --no-timestamp $rsync_opt
RunStage 2 --apply-log # ?? http://toster.ru/q/153493
rm -rf "$PREVDIR"
mv "$DESTDIR" "$PREVDIR"
# todo!!! incremental mode??? http://www.percona.com/doc/percona-xtrabackup/2.2/howtos/recipes_ibkx_inc.html
# --incremental
# --incremental-basedir=DIRECTORY
# --incremental-dir=DIRECTORY
find "$LOGSDIR/" -type f -empty -delete
find "$LOGSDIR/" -type f -name '*.log*' -mtime '+99' -delete
## END ##
#!/bin/sh
#
# Based on http://anothermysqldba.blogspot.ru/2013/06/percona-xtrabackupinnobackupex-backup.html
#
# Requires:
# innobackupex, rsync, ls4sweep
#
# Restore full:
# innobackupex --copy-back /usr/local/src/fullbackup/
# Restore full+incr:
# innobackupex --apply-log --redo-only /path/to/fullbackup/
# innobackupex --apply-log --redo-only /path/to/fullbackup/ --incremental-dir=/path/to/incremental_1/
# innobackupex --apply-log /path/to/fullbackup/ --incremental-dir=/path/to/incremental_2/
# innobackupex --copy-back /path/to/fullbackup/
#
export PATH="$PATH:/usr/local/bin" # ..run ls4sweep under cron
FULLPERIOD="6" #..means one week
DBLIST="$@"
BASEDIR="/home/InnoBackup/$(echo $DBLIST | tr ' /' ',!')"
TSTAMP="$(date +%Y-%m-%d-%H%M)"
LOGSDIR="$BASEDIR/Logs"
LSN_DIR="$BASEDIR/checkpoint"
Fail() {
logger -p user.err -t "${0##*/}" -- "$@"
echo "$@" | mail -s "${0##*/} failed on $(hostname -f)" admins
exit 1
}
Run() {
local stage="$1"; shift
local logfile="$LOGSDIR/$TSTAMP-$stage.log"
# --compress --compress-threads=4 \ #..stage-2 requires uncompressed files!
innobackupex \
--defaults-file=/etc/mysql/debian.cnf \
--no-timestamp \
--no-lock \
--rsync \
--compact \
--parallel=2 \
--slave-info \
--safe-slave-backup \
--databases="$DBLIST" \
--extra-lsndir="$LSN_DIR" \
"$@" > "$logfile" 2>&1
grep -q " completed OK" "$logfile" || Fail "Stage $stage failed, see $logfile"
}
mkdir -p "$LOGSDIR" || Fail "Cannot create $LOGDIR"
cd "$BASEDIR" || Fail "Cannot chdir $BASEDIR"
# Find last full backup not older than FULLPERIOD days..
FULLBASE="$(find "$BASEDIR/" -mindepth 1 -maxdepth 1 -type d -ctime "-$FULLPERIOD" -name '????-??-??-????-full' | sort -nr | head -1)"
if test "$FULLBASE" = ""; then # Create full backup..
DESTDIR="$BASEDIR/$TSTAMP-full"
Run "full" "$DESTDIR/"
else # Create incremental backup..
DESTDIR="$BASEDIR/$TSTAMP-incr"
Run "incr" --incremental --incremental-basedir="$LSN_DIR/" "$DESTDIR/"
fi
ls4sweep --ctime "30:1,3:30,3:90,99:360" $BASEDIR/*-full \
2>"$LOGSDIR/$TSTAMP-sweeperr.log" \
| tee "$LOGSDIR/$TSTAMP-sweep.log" \
| xargs -r /bin/rm >"$LOGSDIR/$TSTAMP-rm.log" 2>&1
find "$LOGSDIR/" -type "f" -mtime "+90" -iname "*.log" -delete
find "$BASEDIR/" -mindepth 1 -maxdepth 1 -type d -name '????-??-??-????-incr' -ctime +"$(expr $FULLPERIOD + $FULLPERIOD + 2)" -delete
## END ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment