#!/bin/bash
# Restore data from backup file given as argument.
#
# © 2015, Multapplied Networks, Inc.

if [ $# -lt 1 ]; then
    # No date specified, using today's date
    DATE=$(date +%Y-%m-%d)
else
    # Using date from command line.
    DATE="$1"
fi

BACKUP_DIR=/var/lib/bondingadmin/backups

read -p "Are you sure you want to restore from the ${DATE} backups in ${BACKUP_DIR}? [y/N] " prompt
if [[ ! $prompt =~ [yY](es)* ]]; then
  echo "Quitting."
  exit
fi

test -f /etc/default/bondingadmin/restore && . /etc/default/bondingadmin/restore
ETC_DEFAULT_FILENAME=$BACKUP_DIR/default.$DATE.tar
POSTGRESQL_FILENAME=$BACKUP_DIR/postgresql.$DATE.sql
POSTGRESQL_FILENAME_TMP=$(mktemp -t tmp.XXXXXXXXXX.sql)
DJANGO_FILENAME=$BACKUP_DIR/django.$DATE.json
DJANGO_FILENAME_TMP=$(mktemp -t tmp.XXXXXXXXXX.json)
REDIS_FILENAME=$BACKUP_DIR/redis.$DATE.rdb
CONFIG_FILENAME=$BACKUP_DIR/configuration.$DATE.tar
CA_FILENAME=$BACKUP_DIR/ca.$DATE.tar
MGMTVPN_FILENAME=$BACKUP_DIR/mgmtvpn.$DATE.tar
MEDIA_FILENAME=$BACKUP_DIR/media.$DATE.tar
SALT_PKI_FILENAME=$BACKUP_DIR/salt.pki.master.$DATE.tar
INFLUXDB_BUSINESS_FILENAME=$BACKUP_DIR/influxdb-business.$DATE.tar.gz
MOSQUITTO_FILENAME=$BACKUP_DIR/mosquitto.$DATE.tar
LAYWIRE_FILENAME=$BACKUP_DIR/laywire.$DATE.tar
systemctl stop bondingadmin

# Delete the temporary file we created upon exit.
function cleanup_tmp {
  rm -rf "$DJANGO_FILENAME_TMP"
}
trap cleanup_tmp EXIT

set -e

# Update postgresql conf
echo "Updating postgresql conf"
/usr/share/bondingadmin/update-postgresql-conf.sh

if [ -f "$POSTGRESQL_FILENAME" ] ; then
  cp "$POSTGRESQL_FILENAME" "$POSTGRESQL_FILENAME_TMP"
  chown postgres:postgres "$POSTGRESQL_FILENAME_TMP"
  sudo -u postgres psql --file "$POSTGRESQL_FILENAME_TMP" postgres
  rm -f "$POSTGRESQL_FILENAME_TMP"
else
  # Reset and restore Django using loaddata
  # Check that the backup file exists before we flush the existing db.
  if [ ! -f "${DJANGO_FILENAME}" ]; then
    echo "error: Django data file ${DJANGO_FILENAME} does not exist."
    exit 1
  fi

  # Copy the django backup file to a temporary location and change the ownership
  # to bondingadmin. This temp file will be cleaned up by the exit trap.
  cp "$DJANGO_FILENAME" "$DJANGO_FILENAME_TMP"
  chown bondingadmin:bondingadmin "$DJANGO_FILENAME_TMP"

  /usr/share/bondingadmin/bootstrap.sh flush --noinput
  /usr/share/bondingadmin/bootstrap.sh migrate
  /usr/share/bondingadmin/bootstrap.sh loaddata "$DJANGO_FILENAME_TMP"
  /usr/share/bondingadmin/bootstrap.sh clear_user_sessions
  rm -f "$DJANGO_FILENAME_TMP"
fi

# Redis
# Check that the backup file exists before we flush the existing db.
if [ ! -f "${REDIS_FILENAME}" ]; then
  echo "error: Redis data file ${REDIS_FILENAME} does not exist."
  exit 1
fi
systemctl stop redis-server
cp "${REDIS_FILENAME}" /var/lib/redis/dump.rdb
chmod 660 /var/lib/redis/dump.rdb
chown redis:redis /var/lib/redis/dump.rdb
systemctl start redis-server

# All other tar files can go straight into /; the contents of the archives
# includes full path names.
tar -xvf "${CONFIG_FILENAME}" -C /
tar -xvf "${CA_FILENAME}" -C /
tar -xvf "${MGMTVPN_FILENAME}" -C /
tar -xvf "${MEDIA_FILENAME}" -C /
tar -xvf "${SALT_PKI_FILENAME}" -C /
tar -xvf "${ETC_DEFAULT_FILENAME}" -C /
tar -xvf "${MOSQUITTO_FILENAME}" -C /
tar -xvf "${LAYWIRE_FILENAME}" -C /

# Run bondingadmin-setup to reinitialize the database
bondingadmin-setup

# Reconfigure influxdb
eval "$(ba print_settings INFLUX_SERVER | sed 's/ //g')"  # parse INFLUX_SERVER from the settings
if [ "${INFLUX_SERVER}" != "localhost" ] ; then
  echo "Restoring influxdb business data on remote influxdb server ${INFLUX_SERVER}"
else
  echo "Restoring influxdb business data on local influxdb server"
  # Influxdb is probably running already but this ensures it is
  systemctl start influxdb
fi

INFLUX_BACKUP_TMP=$(mktemp -d)
trap 'rm -rf $INFLUX_BACKUP_TMP' EXIT
tar -xvf "$INFLUXDB_BUSINESS_FILENAME" -C "$INFLUX_BACKUP_TMP"
influx -username root -password "$(cat /etc/bondingadmin/influx-root-pw)" -host "$(ba get_influx_server)" -execute "DROP DATABASE business"
influxd restore -portable -host "$(ba get_influx_server):8088" -database business "$INFLUX_BACKUP_TMP/business"

echo "You can now start bondingadmin services with:"
echo "systemctl start bondingadmin"
