Skip to content

Instantly share code, notes, and snippets.

@stemid
Last active January 3, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stemid/beb1b593b737b5ee92891077ca696b00 to your computer and use it in GitHub Desktop.
Save stemid/beb1b593b737b5ee92891077ca696b00 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# {{ ansible_managed }}
# vim: set filetype=bash
#
# WARNING: Have you run xtrabackup_prepare.sh first?
# WARNING: You must know what you're doing or the backup will be ruined!
shopt -s nullglob
backup_dir=/var/backups/xtrabackup
incr_backup_dir=/var/backups/xtrabackup_incremental
get_to_lsn() {
filename=$1 && shift
to_lsn="$(grep ^to_lsn "$filename"|cut -d= -f2|tr -d ' ')"
echo "$to_lsn"
}
get_from_lsn() {
filename=$1 && shift
from_lsn="$(grep ^from_lsn "$filename"|cut -d= -f2|tr -d ' ')"
echo "$from_lsn"
}
# Count the number of incremental backups we have
backups=("${backup_dir}" "${incr_backup_dir}/incremental-"*)
number_of_backups=${#backups[@]}
# Add one for the base backup
((number_of_backups++))
current_index=0
for inc in "${backup_dir}" "${incr_backup_dir}/incremental-"*; do
((current_index++))
echo "(${current_index}/${number_of_backups}): $inc"
# This is the first backup, base backup
if [ $current_index -eq 1 ]; then
logger -t "$0" -p debug "xtrabackup --prepare --apply-log-only --target-dir=$inc"
xtrabackup --prepare --apply-log-only --target-dir="$inc"
else
# Check if last incremental or not, last incremental does not need --redo-only
if [ $current_index -lt $number_of_backups ]; then
#logger -t "$0" -p debug "innobackupex --apply-log --redo-only $backup_dir --incremental-dir=$inc"
#innobackupex --apply-log --redo-only "$backup_dir" --incremental-dir="$inc"
logger -t "$0" -p debug "xtrabackup --prepare --apply-log-only --target-dir="$backup_dir" --incremental-dir=$inc"
xtrabackup --prepare --apply-log-only --target-dir="$backup_dir" --incremental-dir="$inc"
else
#logger -t "$0" -p debug "innobackupex --apply-log $backup_dir --incremental-dir=$inc"
#innobackupex --apply-log "$backup_dir" --incremental-dir="$inc"
logger -t "$0" -p debug "xtrabackup --prepare --target-dir="$backup_dir" --incremental-dir=$inc"
xtrabackup --prepare --target-dir="$backup_dir" --incremental-dir="$inc"
fi
rc=$?
test $rc -eq 0 || exit 1
logger -t "$0" -p debug "rm -rfIv $inc"
rm -rfv "$inc"
fi
done
#!/usr/bin/env bash
# {{ ansible_managed }}
# vim: set filetype=bash
backup_dir=/var/backups/xtrabackup
incr_backup_dir=/var/backups/xtrabackup_incremental
get_to_lsn() {
filename=$1 && shift
to_lsn="$(grep ^to_lsn "$filename"|cut -d= -f2|tr -d ' ')"
echo "$to_lsn"
}
get_from_lsn() {
filename=$1 && shift
from_lsn="$(grep ^from_lsn "$filename"|cut -d= -f2|tr -d ' ')"
echo "$from_lsn"
}
# Check LSNs of all backups and abort if not in sequence
old_dir=''
number_of_incremental=0
current_index=0
for dir in "${backup_dir}" "${incr_backup_dir}/incremental-"*; do
((current_index++))
chkpoints_file="${dir}/xtrabackup_checkpoints"
if [ ! -r "$chkpoints_file" ]; then
logger -t "$0" -p err "${chkpoints_file}: Could not find checkpoints file, aborting"
exit 1
fi
# First index means first base backup dir so we treat it differently
if [ $current_index -eq 1 ]; then
old_dir="$dir"
fi
old_chkpoints_file="${old_dir}/xtrabackup_checkpoints"
to_lsn=$(get_to_lsn "$old_chkpoints_file")
from_lsn=$(get_from_lsn "$chkpoints_file")
if [ $current_index -eq 1 ]; then
from_lsn=$to_lsn
fi
# Increment number of incremental backups
((number_of_incremental++))
echo "Checking index #${current_index}: $dir (${to_lsn})"
if [ -z "$to_lsn" -o -z "$from_lsn" ]; then
logger -t "$0" -p err "${inc}: LSN missing, aborting"
exit 1
fi
if [ $to_lsn -ne $from_lsn ]; then
logger -t "$0" -p err "${dir}: to_lsn[${to_lsn}] not equal to from_lsn[${from_lsn}], aborting"
exit 1
fi
# Save the incremental backup dir in an old variable.
old_dir="$dir"
done
logger -t "$0" -p notice "Found ${number_of_incremental} OK incremental copies"
echo "Ready to restore!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment