#!/bin/bash
#
# Creates a Full Backup of Nagios XI
# Copyright (c) 2011-2018 Nagios Enterprises, LLC. All rights reserved.
#

BASEDIR=/usr/local/nagiosxi/scripts
# FIXME
SBLOG="/usr/local/nagiosxi/var/components/scheduledbackups.log"
ts=`date +%s`
secondary=""

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

###############################
# USAGE / HELP
###############################
usage () {
    echo ""
    echo "Use this script to backup Nagios XI."
    echo ""
        echo " -n | --name              Set the name of the backup minus the .tar.gz"
        echo " -p | --prepend           Prepend a string to the .tar.gz name"
        echo " -a | --append            Append a string to the .tar.gz name"
        echo " -d | --directory         Change the directory to store the compressed backup"
    echo ""
}

###############################
# ADDING LOGIC FOR NEW BACKUPS
###############################
while [ -n "$1" ]; do
    case "$1" in
	-s | --secondary)
	    secondary="$2"
	    ;;
        -h | --help)
            usage
            exit 0
            ;;
        -n | --name)
            fullname=$2
            ;;
        -p | --prepend)
            prepend=$2"."
            ;;
        -a | --append)
            append="."$2
            ;;
        -d | --directory)
            rootdir=$2
            ;;
    esac
    shift
done

[ -z "$secondary" ] && echo "Secondary not specified." && exit 2

# Restart nagios to forcibly update retention.dat
echo "Restart nagios to forcibly update retention.dat"
$BASEDIR/manage_services.sh restart nagios
sleep 10

if [ -z $rootdir ]; then
    rootdir="/store/backups/nagiosxi"
fi

# Move to root dir to store backups
cd $rootdir

#############################
# SET THE NAME & TIME
#############################
name=$fullname

if [ -z $fullname ]; then
    name=$prepend$ts$append
fi

# Get current Unix timestamp as name
if [ -z $name ]; then
    name=$ts
fi

# My working directory
mydir=$rootdir/$name

# Make directory for this specific backup
mkdir -p $mydir

##############################
# BACKUP DIRS
##############################


do_rsync() {
  echo rsync -avun --delete --delete-after $1 ${secondary}:${1}
}

do_rsync2() {
  rsync -avu --delete --delete-after $1 ${secondary}:${1}
}

# Only backup NagiosQL if it exists
if [ -d "/var/www/html/nagiosql" ]; then
    echo "Backing up NagiosQL..."
    do_rsync /var/www/html/nagiosql
    do_rsync /etc/nagiosql
fi

echo "Backing up Nagios Core..."
do_rsync /usr/local/nagios

# Backup ramdisk if it exists
if [ -f "/etc/sysconfig/nagios" ]; then
    echo "Copying ramdisk configuration..."
    do_rsync /etc/sysconfig/nagios
fi

echo "Backing up Nagios XI..."
do_rsync /usr/local/nagiosxi

echo "Backing up MRTG..."
do_rsync /var/lib/mrtg
do_rsync /etc/mrtg/mrtg.cfg
do_rsync /etc/mrtg/conf.d

# SNMP configs and MIBS
echo "Backing up the SNMP directories"
do_rsync /etc/snmp
do_rsync /usr/share/snmp

echo "Backing up NRDP..."
do_rsync /usr/local/nrdp

echo "Backing up Nagvis..." 
do_rsync /usr/local/nagvis

echo "Backing up nagios user home dir..." 
do_rsync /home/nagios

##############################
# BACKUP DATABASES
##############################
echo "Backing up MySQL databases..."
mkdir -p $mydir/mysql
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
mysqldump -h "$ndoutils_dbserver" --port="$ndoutils_dbport" -u $cfg__db_info__ndoutils__user --password="$cfg__db_info__ndoutils__pwd" --add-drop-database -B $cfg__db_info__ndoutils__db > $mydir/mysql/nagios.sql
res=$?
if [ $res != 0 ]; then
    echo "Error backing up MySQL database 'nagios' - check the password in this script!" | tee -a $SBLOG
    rm -r $mydir
    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
mysqldump -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u $cfg__db_info__nagiosql__user --password="$cfg__db_info__nagiosql__pwd" --add-drop-database -B $cfg__db_info__nagiosql__db > $mydir/mysql/nagiosql.sql
res=$?
if [ $res != 0 ]; then
    echo "Error backing up MySQL database 'nagiosql' - check the password in this script!" | tee -a $SBLOG
    rm -r $mydir
    exit $res;
fi

# Only backup PostgresQL if we are still using it 
if [ $cfg__db_info__nagiosxi__dbtype == "pgsql" ]; then
    echo "Backing up PostgresQL databases..."
    mkdir -p $mydir/pgsql
    if [ -z $cfg__db_info__nagiosxi__dbserver ]; then
        cfg__db_info__nagiosxi__dbserver="localhost"
    fi
    pg_dump -h $cfg__db_info__nagiosxi__dbserver -c -U $cfg__db_info__nagiosxi__user $cfg__db_info__nagiosxi__db > $mydir/pgsql/nagiosxi.sql
    res=$?
    if [ $res != 0 ]; then
        echo "Error backing up PostgresQL database 'nagiosxi' !" | tee -a $SBLOG
        rm -r $mydir
        exit $res;
    fi
else
    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'
        nagiosxi_dbserver="$cfg__db_info__nagiosxi__dbserver"
    fi
    mysqldump -h "$nagiosxi_dbserver" --port="$nagiosxi_dbport" -u $cfg__db_info__nagiosxi__user --password="$cfg__db_info__nagiosxi__pwd" --add-drop-database -B $cfg__db_info__nagiosxi__db > $mydir/mysql/nagiosxi.sql
    res=$?
    if [ $res != 0 ]; then
        echo "Error backing up MySQL database 'nagiosxi' - check the password in this script!" | tee -a $SBLOG
        rm -r $mydir
        exit $res;
    fi
fi

##############################
# BACKUP CRONJOB ENTRIES
##############################
echo "Backing up cronjobs for Apache..."
if [[ "$distro" == "Ubuntu" ]] || [[ "$distro" == "Debian" ]]; then
    do_rsync /var/spool/cron/crontabs/$apacheuser
else
    do_rsync /var/spool/cron/apache
fi

##############################
# BACKUP SUDOERS
##############################
# Not necessary

##############################
# BACKUP LOGROTATE
##############################
echo "Backing up logrotate config files..."
do_rsync /etc/logrotate.d/nagiosxi

##############################
# BACKUP APACHE CONFIG FILES
##############################
echo "Backing up Apache config files..."
do_rsync $httpdconfdir/nagios.conf
do_rsync $httpdconfdir/nagiosxi.conf
do_rsync $httpdconfdir/nagiosmobile.conf
do_rsync $httpdconfdir/nagvis.conf
do_rsync $httpdconfdir/nrdp.conf

if [ -d "/etc/apache2/sites-available" ]; then
    do_rsync /etc/apache2/sites-available/default-ssl.conf
else
    do_rsync $httpdconfdir/ssl.conf
fi

##############################
# COMPRESS BACKUP
##############################
echo "Compressing backup..."
tar czfp $name.tar.gz $name
rm -rf $name

# Change ownership
chown $nagiosuser:$nagiosgroup $name.tar.gz

if [ -s $name.tar.gz ];then
    echo " "
    echo "==============="
    echo "BACKUP COMPLETE"
    echo "==============="
    echo "Backup stored in $rootdir/$name.tar.gz"
else
    echo " "
    echo "==============="
    echo "BACKUP FAILED"
    echo "==============="
    echo "File was not created at $rootdir/$name.tar.gz"
    rm -r $mydir
    exit 1;
fi

do_rsync2 $rootdir/$name.tar.gz
