blob: ca0a2998910523f1160c317d943fb61993381579 [file] [log] [blame]
Oleksii Molchanov2eeeb882020-02-27 11:56:23 +02001{%- from "xtrabackup/map.jinja" import server with context -%}
Pavel Cizinsky7b1da742019-03-07 13:03:02 +01002#!/bin/bash
Jiri Broulik634ea8a2017-05-31 17:10:03 +02003
4# This script returns appropriate backup that client will restore
Pavel Cizinsky7b1da742019-03-07 13:03:02 +01005set -eo pipefail
Jiri Broulik634ea8a2017-05-31 17:10:03 +02006
Pavel Cizinsky7b1da742019-03-07 13:03:02 +01007BACKUPDIR="{{ server.backup_dir }}" # Backups base directory
8FULL="$(find $BACKUPDIR/full -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -$1 | tail -1)"
9FULL_INCR="$(find $BACKUPDIR/incr -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -$1 | tail -1)"
10BEFORE_NEXT_FULL_INCR="$(find $BACKUPDIR/incr -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -$(( $1 - 1 )) | tail -1 || true)"
11
12function prerequisites(){
13 if [ $1 -eq 0 ]; then
Jiri Broulik634ea8a2017-05-31 17:10:03 +020014 echo "No arguments provided"
15 exit 1
Pavel Cizinsky7b1da742019-03-07 13:03:02 +010016 fi
Jiri Broulik634ea8a2017-05-31 17:10:03 +020017
Pavel Cizinsky7b1da742019-03-07 13:03:02 +010018 # if arg is not an integer
19 case $2 in
Jiri Broulik634ea8a2017-05-31 17:10:03 +020020 ''|*[!0-9]*) echo "Argument must be integer"; exit 1 ;;
21 *) ;;
Pavel Cizinsky7b1da742019-03-07 13:03:02 +010022 esac
23}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020024
Pavel Cizinsky7b1da742019-03-07 13:03:02 +010025function return_backup(){
26 if [ -z "$BEFORE_NEXT_FULL_INCR" ]; then
Jiri Broulik634ea8a2017-05-31 17:10:03 +020027 BEFORE_NEXT_FULL_INCR="Empty"
Pavel Cizinsky7b1da742019-03-07 13:03:02 +010028 fi
Martin Polreich710fab52019-08-06 16:35:28 +020029 if [ "$FULL" == "$FULL_INCR" ]; then
Pavel Cizinsky7b1da742019-03-07 13:03:02 +010030 LATEST_FULL_INCR=$(find $BACKUPDIR/incr/$FULL_INCR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1 | tail -1)
31 echo "$BACKUPDIR/incr/$FULL/$LATEST_FULL_INCR"
32 elif [ $FULL = $BEFORE_NEXT_FULL_INCR ]; then
33 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)
34 echo "$BACKUPDIR/incr/$FULL/$LATEST_FULL_INCR"
35 else
36 echo "$BACKUPDIR/full/$FULL"
37 fi
38}
Jiri Broulik634ea8a2017-05-31 17:10:03 +020039
Pavel Cizinsky7b1da742019-03-07 13:03:02 +010040prerequisites "$#" "$1"
41return_backup