#!/usr/local/bin/bash
#/ Usage: ghe-restore-redis <host>
#/ Restore redis files from an rsync snapshot.
#/
#/ Note: This script typically isn't called directly. It's invoked by the
#/ ghe-restore command.
set -e

# Bring in the backup configuration
# shellcheck source=share/github-backup-utils/ghe-backup-config
. "$(dirname "${BASH_SOURCE[0]}")/ghe-backup-config"

trap_errors

# Show usage and bail with no arguments
[[ -z ${*} ]] && print_usage

# Grab host arg
GHE_HOSTNAME="${1}"

# Grab snapshot path arg
GHE_RESTORE_SNAPSHOT_PATH="${2}"

bm_start "$(basename "${0}")"

res=0
ghe-ssh "$GHE_HOSTNAME" -- 'ghe-import-redis' < "$GHE_RESTORE_SNAPSHOT_PATH/redis.rdb" 2>&3 1>&3 || res=$?

if [ $res -eq 3 ]; then
  log_warn_verbose "There were problems importing information to the redis service." 
  res=0
elif [ $res -gt 0 ] && [ $res -ne 3 ]; then
  log_error_verbose "An error occurred while importing the redis data." 
elif [ $res -eq 0 ]; then
  log_info_verbose "Redis import completed successfully"
fi

bm_end "$(basename "${0}")"
exit $res