#!/bin/env bash

# The latest backup (latest full & latest incremental) will be restored.
# To restore to a specific point in time, please contact us.
# Put this script in the same directory as your base and incr directories
# If you're not aware where it is, check the BACKDIR variable in the script
# that creates the backup. It should normally be /usr/bin/wsrep_sst_backup_mtg

set -o pipefail

function dir_exists_and_writable() {
    # check incr dir exists and is writable
    if test ! -d $1 -o ! -w $1
    then
        error
        echo $1 'does not exist or is not writable'; echo
        exit 1
    fi
}

function restore_streamfile() {
    
    mkdir -p $1/backup
    zcat $1/backup.stream.gz | mbstream -x -C $1/backup/ || exit $?
}

:> folders.txt

for i in $(find . -name backup.stream.gz | xargs dirname) #Get all directory names of directories that contain the .gz file
do 
echo $i >> folders.txt  				#Save all those directory names to a file
done

LATEST_FULL_TIME=$(grep base folders.txt |sort -n -r | head -1 | awk -F \/ {'print $NF'})

LATEST_INCR_TIME=$(grep incr folders.txt | grep "$LATEST_FULL_TIME" |sort -n -r |head -1 | awk -F \/ {'print $NF'})


if [ -z "$LATEST_FULL_TIME" ]
then
  echo "HALTED: It appears there's no full backup taken."; echo
  exit 1
else
  echo "Latest full back taken on $LATEST_FULL_TIME."
  LATEST_FULL_DIR="./base/${LATEST_FULL_TIME}"
  dir_exists_and_writable "$LATEST_FULL_DIR"
  restore_streamfile "$LATEST_FULL_DIR" && BASE_DIR_FULL="${LATEST_FULL_DIR}/backup/"
fi

if [ -z "$LATEST_INCR_TIME" ]
then
  echo "It appears there's no incremental backup taken for our full backup."; echo
else
  echo "Latest incremental backup for our full taken on $LATEST_FULL_TIME."
  LATEST_INCR_DIR="./incr/${LATEST_FULL_TIME}/${LATEST_INCR_TIME}"
  dir_exists_and_writable "$LATEST_INCR_DIR"
  restore_streamfile "$LATEST_INCR_DIR" && BASE_DIR_INCR="${LATEST_INCR_DIR}/backup/"
fi

echo "We'll attempt a restore that will bring galera cluster on ${LATEST_INCR_TIME:-$LATEST_FULL_TIME} status."
echo "Press any key to continue or Ctrl-C to stop."
read




echo "Preparing backups ..."
mariabackup --prepare --target-dir "$BASE_DIR_FULL" && \
[[ -n "$LATEST_INCR_TIME" ]] && \
mariabackup --prepare --target-dir "$BASE_DIR_FULL" --incremental-dir "$BASE_DIR_INCR"	

echo "Done!"
echo "Final steps are to stop mariadb service, restore the backup and start the service again."
echo "If you're restoring a Galera Cluster, pause now and stop mariadb on other nodes."
echo "Again press any key to continue, or Ctrl-C to abort and take over manually."
read

restore_time=$(date +"%Y%m%d_%H%M%S")

systemctl stop mariadb && \
mv /var/lib/mysql /var/lib/mysql_before_$restore_time && \
mariabackup --copy-back --target-dir "$BASE_DIR_FULL" && \
chown -R mysql:mysql /var/lib/mysql && \
echo "The restored files are in place now!" && \
echo "If that's a Galera Cluster you should run 'sudo galera_new_cluster'" && \
echo "If that's a single MariaDB server you may start the mariadb service."
