#!/bin/bash
#
# Restores a Full Backup of Nagios XI
# Copyright (c) 2008-2018 Nagios Enterprises, LLC. All rights reserved.
#
# Heavily modified by EverWatch Global, Inc. for syncing two Nagios installations
#
# Restore just needs to restore the MySQL stuff.  Everything else was shipped over via rsync

PATH=/home/nagios/bin:/usr/bin:/bin

. /home/nagios/bin/colors.sh

# Change this password if your root password is different than "nagiosxi"
# MySQL root password
themysqlpass="nagiosxi"

# Make sure we have the backup file
if [ $# != 1 ]; then
    error "Usage: $0 <backupfile>"
    warning "This script restores your XI system using a previously made Nagios XI backup file."
    exit 1
fi
backupfile=$1

BASEDIR=/usr/local/nagiosxi/scripts
SPECIAL_BACKUP=0

# Import Nagios XI and xi-sys.cfg config vars
. $BASEDIR/../var/xi-sys.cfg
eval $(php $BASEDIR/import_xiconfig.php)

## Must be root
#me=`whoami`
#if [ $me != "root" ]; then
#    error "You must be root to run this script."
#    exit 1
#fi

rootdir=/store/backups/nagiosxi

##############################
# MAKE SURE BACKUP FILE EXIST
##############################
if [ ! -f $backupfile ]; then
    error "Unable to find backup file $backupfile!"
    exit 1
fi

##############################
# MAKE TEMP RESTORE DIRECTORY
##############################
ts=`date +%s`
mydir=${rootdir}/${ts}-restore
mkdir -p $mydir
if [ ! -d $mydir ]; then
    error "Unable to create restore directory $mydir!"
    exit 1
fi


##############################
# UNZIP BACKUP
##############################
cd $mydir
tar xzfps $backupfile
cd `ls`
backupdir=`pwd`


##############################
# SHUTDOWN SERVICES
##############################
verbose "Shutting down services..."
sudo $BASEDIR/manage_services.sh stop nagios
sudo $BASEDIR/manage_services.sh stop ndo2db
sudo $BASEDIR/manage_services.sh stop npcd


rootdir=/
# RE-IMPORT ALL XI CFG VARS
. $BASEDIR/../var/xi-sys.cfg
eval `php $BASEDIR/import_xiconfig.php`

# Overwrite the mysqlpass with the hardcoded one at the top
mysqlpass="$themysqlpass"

##############################
# RESTORE DATABASES
##############################
restore_databases() {
verbose "Restoring MySQL databases..."
if [[ "$cfg__db_info__ndoutils__dbserver" == *":"* ]]; then
    ndoutils_dbport=`echo "$cfg__db_info__ndoutils__dbserver" | cut -f2 -d":"`
    ndoutils_dbserver=`echo "$cfg__db_info__ndoutils__dbserver" | cut -f1 -d":"`
else
    ndoutils_dbport='3306'
    ndoutils_dbserver="$cfg__db_info__ndoutils__dbserver"
fi
mysql -h "$ndoutils_dbserver" --port="$ndoutils_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagios.sql
res=$?
if [ $res != 0 ]; then
    error "Error restoring MySQL database 'nagios' - check the password in this script!"
    exit $res;
fi
if [[ "$cfg__db_info__nagiosql__dbserver" == *":"* ]]; then
    nagiosql_dbport=`echo "$cfg__db_info__nagiosql__dbserver" | cut -f2 -d":"`
    nagiosql_dbserver=`echo "$cfg__db_info__nagiosql__dbserver" | cut -f1 -d":"`
else
    nagiosql_dbport='3306'
    nagiosql_dbserver="$cfg__db_info__nagiosql__dbserver"
fi
mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagiosql.sql
res=$?
if [ $res != 0 ]; then
    error "Error restoring MySQL database 'nagiosql' - check the password in this script!"
    exit $res;
fi
# Only restore PostgresQL if we are still using it 
if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then
    service postgresql initdb &>/dev/null || true
    verbose "Restoring Nagios XI PostgresQL database..."
    if [ -f /var/lib/pgsql/data/pg_hba.conf ]; then
        pghba="/var/lib/pgsql/data/pg_hba.conf"
        cp -pr $pghba $pghba.old
    else
        #Ubuntu/Debian
        pghba=$(find /etc/postgresql -name "*pg_hba.conf")
        cp -pr $pghba $pghba.old
    fi
    echo "local  all         all                   trust
host    all         all         127.0.0.1/32          trust
host    all         all         ::1/128               trust" > $pghba
    sudo $BASEDIR/manage_services.sh start postgresql
    sudo -u postgres psql -c "create user nagiosxi with password 'n@gweb';"
    sudo -u postgres psql -c "create database nagiosxi owner nagiosxi;"
    sudo $BASEDIR/manage_services.sh restart postgresql
    # Sleep a bit (required so Postgres finishes startup before we connect again)
    verbose "Sleeping for a few seconds (please wait)..."
    sleep 7
    psql -U nagiosxi nagiosxi < $backupdir/pgsql/nagiosxi.sql
    res=$?
    if [ $res != 0 ]; then
        error "Error restoring PostgresQL database 'nagiosxi' !"
        exit $res;
    fi
    sudo $BASEDIR/manage_services.sh restart postgresql
    if [ "$dist" == "el7" ] || [ "$dist" == "el8" ]; then
        systemctl enable postgresql.service
    elif [[ "$distro" == "Ubuntu" ]] || [[ "$distro" == "Debian" ]]; then
        update-rc.d postgresql enable
    else
        chkconfig postgresql on
    fi
    # Remove nagiosxi db from mysql if postgres is used instead
    if [[ "$cfg__db_info__nagiosql__dbserver" == *":"* ]]; then
        nagiosql_dbport=`echo "$cfg__db_info__nagiosql__dbserver" | cut -f2 -d":"`
        nagiosql_dbserver=`echo "$cfg__db_info__nagiosql__dbserver" | cut -f1 -d":"`
    else
        nagiosql_dbport='3306'
        nagiosql_dbserver="$cfg__db_info__nagiosql__dbserver"
    fi
    mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysqlpass < "DROP TABLE IF EXISTS nagiosxi;"
else
    verbose "Restoring Nagios XI MySQL database..."
    if [[ "$cfg__db_info__nagiosxi__dbserver" == *":"* ]]; then
        nagiosxi_dbport=`echo "$cfg__db_info__nagiosxi__dbserver" | cut -f2 -d":"`
        nagiosxi_dbserver=`echo "$cfg__db_info__nagiosxi__dbserver" | cut -f1 -d":"`
    else
        nagiosxi_dbport='3306'
        if [ "x$cfg__db_info__nagiosxi__dbserver" == "x" ]; then
            nagiosxi_dbserver="localhost"
        else
            nagiosxi_dbserver="$cfg__db_info__nagiosxi__dbserver"
        fi
    fi
    mysql -h "$nagiosxi_dbserver" --port="$nagiosxi_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagiosxi.sql
    res=$?
    if [ $res != 0 ]; then
        error "Error restoring MySQL database 'nagiosxi' !"
        exit $res;
    fi
fi
}

restore_databases
verbose "Restarting services..."
sudo $BASEDIR/manage_services.sh restart mysqld
sudo $BASEDIR/manage_services.sh restart httpd
sudo $BASEDIR/manage_services.sh start npcd
sudo $BASEDIR/manage_services.sh start ndo2db
sudo $BASEDIR/manage_services.sh start nagios

rm -rf $mydir
rm -f /tmp/nagiosxi.forceinstall

echo " "
echo "==============="
echo "RESTORE COMPLETE"
echo "==============="

verbose "Restarting Nagios XI"
cd /usr/local/nagiosxi/scripts
./reconfigure_nagios.sh

exit 0
