From 028953010b1a476a04f4b26e734c1917b5f8f74e Mon Sep 17 00:00:00 2001 From: Eric Loyd Date: Sun, 25 Apr 2021 18:47:04 -0400 Subject: [PATCH] Minor non-code changes, removed restore_xi.sh --- failover/failover.sh | 4 +- failover/restore_xi.sh | 208 ----------------------------------------- failover/rsync_xi.sh | 48 ++-------- 3 files changed, 9 insertions(+), 251 deletions(-) delete mode 100755 failover/restore_xi.sh diff --git a/failover/failover.sh b/failover/failover.sh index 6a2adc7..f508ef9 100755 --- a/failover/failover.sh +++ b/failover/failover.sh @@ -40,7 +40,7 @@ do_nothing() { do_primary() { verbose "We are the primary. Creating package to send to secondary." - ./rsync_xi.sh --prepend ${syncname} --primary "${primary}" --secondary "${secondary}" $extraCmd + /home/nagios/bin/rsync_xi.sh --prepend ${syncname} --primary "${primary}" --secondary "${secondary}" $extraCmd if [ -r "/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.primary" -a -w "/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css" ]; then verbose "Copying local gradient files" cp /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.primary /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css @@ -51,7 +51,7 @@ do_secondary() { verbose "We are the secondary. Expanding package from the primary." syncfile=$(basename `ls -tr1 /store/backups/nagiosxi/${syncname}* | tail -1`) [ -z "$syncfile" ] && error "No sync file ($syncfile) found." && exit 2 - ./rsync_xi.sh --primary "${primary}" --secondary "${secondary}" --file "/store/backups/nagiosxi/$syncfile" + /home/nagios/bin/rsync_xi.sh --primary "${primary}" --secondary "${secondary}" --file "/store/backups/nagiosxi/$syncfile" if [ $? -eq 0 ]; then verbose "Ensuring Nagios services are stopped because we are the secondary" sleep 2 diff --git a/failover/restore_xi.sh b/failover/restore_xi.sh deleted file mode 100755 index f76ea39..0000000 --- a/failover/restore_xi.sh +++ /dev/null @@ -1,208 +0,0 @@ -#!/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 " - 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..." -cp /usr/local/nagios/var/retention.dat $backupdir -sudo $BASEDIR/manage_services.sh stop nagios -sudo $BASEDIR/manage_services.sh stop ndo2db -sudo $BASEDIR/manage_services.sh stop npcd -cp $backupdir/retention.dat /usr/local/nagios/var/retention.dat - - -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 diff --git a/failover/rsync_xi.sh b/failover/rsync_xi.sh index 90b9f4e..faa9b3d 100755 --- a/failover/rsync_xi.sh +++ b/failover/rsync_xi.sh @@ -93,7 +93,6 @@ sudo $BASEDIR/manage_services.sh stop nagios sleep 5 sudo $BASEDIR/manage_services.sh start nagios - if [ -z $rootdir ]; then rootdir="/store/backups/nagiosxi" fi @@ -105,11 +104,9 @@ cd $rootdir # SET THE NAME & TIME ############################# name=$fullname - if [ -z $fullname ]; then name=$prepend$timeStamp$append fi - # Get current Unix timestamp as name if [ -z $name ]; then name=timeStampts @@ -117,7 +114,6 @@ fi # My working directory workDir=$rootdir/$name - # Make directory for this specific backup mkdir -p $workDir @@ -280,23 +276,14 @@ test_mysql_connection() { return $? } -backupfile=$1 rootdir=/store/backups/nagiosxi - -############################## -# MAKE SURE BACKUP FILE EXIST -############################## +backupfile=$1 if [ ! -f $backupfile ]; then error "Unable to find backup file $backupfile!" exit 1 fi -############################## -# MAKE TEMP RESTORE DIRECTORY -############################## -#ts=`echo $backupfile | cut -d . -f 1` ts=`date +%s` -### echo "TS=$ts" mydir=${rootdir}/${ts}-restore mkdir -p $mydir if [ ! -d $mydir ]; then @@ -304,10 +291,7 @@ if [ ! -d $mydir ]; then exit 1 fi - -############################## -# UNZIP BACKUP -############################## +# Extract backup verbose "Extracting backup to $mydir..." cd $mydir tar xzfps $backupfile @@ -317,9 +301,7 @@ backupdir=`pwd` verbose "Backup files look okay. Preparing to restore..." -############################## -# SHUTDOWN SERVICES -############################## +# Shutdown services verbose "Shutting down services..." [ -r "/usr/local/nagios/var/retention.dat" ] && cp /usr/local/nagios/var/retention.dat $backupdir sudo $BASEDIR/manage_services.sh stop nagios @@ -332,10 +314,7 @@ rootdir=/ # 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":"` @@ -409,9 +388,7 @@ 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" @@ -424,18 +401,13 @@ if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then echo "local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust" > $pghba - $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;" - $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 @@ -510,20 +482,14 @@ 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 - -############################## -# DELETE TEMP RESTORE DIRECTORY -############################## +verbose "Restarting Nagios XI" +cd /usr/local/nagiosxi/scripts +./reconfigure_nagios.sh rm -rf $mydir - echo " " echo "===============" echo "RESTORE COMPLETE" echo "===============" - -verbose "Restarting Nagios XI" -cd /usr/local/nagiosxi/scripts -./reconfigure_nagios.sh } do_secondary() {