| {%- from "xtrabackup/map.jinja" import client with context %} |
| #!/bin/bash |
| set -eo pipefail |
| # Purpuse of this script is to locally prepare appropriate backup to restore from local or remote location |
| # Variables, initiated via salt-formulas/xtrabackup |
| {%- if client.restore_from == 'remote' %} |
| RESTORE_FROM_MODE="remote" |
| CLIENT_TARGET_HOST="{{ client.target.host }}" |
| CLIENT_RESTORE_FULL_LATEST="{{ client.restore_full_latest }}" |
| {%- else %} |
| RESTORE_FROM_MODE="local" |
| BACKUPDIR="{{ client.backup_dir }}" # Backups base directory |
| {%- endif %} |
| |
| function backup_remote(){ |
| local LOGDIR="/var/log/backups" |
| local scpLog="/var/log/backups/innobackupex-restore-scp.log" |
| mkdir -p $LOGDIR |
| echo "Calling /usr/local/bin/innobackupex-restore.sh $REMOTEBACKUPPATH and getting the backup files from remote host" |
| REMOTEBACKUPPATH="$(ssh xtrabackup@${CLIENT_TARGET_HOST} "/bin/bash /usr/local/bin/innobackupex-restore-call.sh ${CLIENT_RESTORE_FULL_LATEST}")" |
| /usr/local/bin/innobackupex-restore.sh $REMOTEBACKUPPATH |
| } |
| |
| function backup_local(){ |
| local FULL="$(find $BACKUPDIR/full -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -${CLIENT_RESTORE_FULL_LATEST}| tail -1)" |
| local FULL_INCR="$(find $BACKUPDIR/incr -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -${CLIENT_RESTORE_FULL_LATEST} | tail -1)" |
| local BEFORE_NEXT_FULL_INCR="$(find $BACKUPDIR/incr -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -$(( ${CLIENT_RESTORE_FULL_LATEST} - 1 )) | tail -1)" |
| |
| if [ -z "$FULL" ]; then |
| echo "Error: No local backup found in $BACKUPDIR/full" >&2 |
| exit 1 |
| fi |
| |
| if [ -z "$BEFORE_NEXT_FULL_INCR" ]; then |
| BEFORE_NEXT_FULL_INCR="Empty" |
| fi |
| |
| if [ "$FULL" = "$FULL_INCR" ]; then |
| LATEST_FULL_INCR=$(find $BACKUPDIR/incr/$FULL_INCR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1 | tail -1) |
| echo "Restoring full backup $FULL starting from its latest incremental $LATEST_FULL_INCR" |
| echo "Calling /usr/local/bin/innobackupex-restore.sh $BACKUPDIR/incr/$FULL/$LATEST_FULL_INCR" |
| echo |
| /usr/local/bin/innobackupex-restore.sh $BACKUPDIR/incr/$FULL_INCR/$LATEST_FULL_INCR |
| elif [ "$FULL" = "$BEFORE_NEXT_FULL_INCR" ]; then |
| LATEST_FULL_INCR="$(find $BACKUPDIR/incr/$BEFORE_NEXT_FULL_INCR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1 | tail -1)" |
| echo "Restoring full backup $FULL starting from its latest incremental $LATEST_FULL_INCR" |
| echo "Calling /usr/local/bin/innobackupex-restore.sh $BACKUPDIR/incr/$FULL/$LATEST_FULL_INCR" |
| echo |
| /usr/local/bin/innobackupex-restore.sh $BACKUPDIR/incr/$FULL/$LATEST_FULL_INCR |
| else |
| echo "Restoring full backup $FULL" |
| echo "Calling /usr/local/bin/innobackupex-restore.sh $BACKUPDIR/full/$FULL" |
| echo |
| /usr/local/bin/innobackupex-restore.sh $BACKUPDIR/full/$FULL |
| fi |
| } |
| |
| # Body ######################################################################## |
| if [[ ${RESTORE_FROM_MODE} == "remote" ]]; then |
| backup_remote |
| elif [[ ${RESTORE_FROM_MODE} == "local" ]]; then |
| backup_local |
| fi |