Code: Select all
ls -l /usr/local/nagiosxi/scripts/import_xiconfig.phpThe attached script will only work on 2012R2.8+
Code: Select all
ls -l /usr/local/nagiosxi/scripts/import_xiconfig.phpCode: Select all
#!/bin/sh
###############################
# 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
-h | --help)
usage
exit 0
;;
-n | --name)
fullname=$2
;;
-p | --prepend)
prepend=$2
;;
-a | --append)
append=$2
;;
-d | --directory)
rootdir=$2
;;
esac
shift
done
# MySQL root password
mysqlpass="nagiosxi"
# Must be root
#me=`whoami`
#if [ $me != "root" ]; then
# echo "You must be root to run this script."
# exit 1
#fi
# No longer need root because of file permissions -JO
if [ -z $rootdir ]; then
rootdir=/store/backups/nagiosxi
fi
# Make root directory to store backups
#mkdir -p $rootdir
cd $rootdir
# Directory is created upon XI install -JO
# Get current Unix timestamp
ts=`date +%s`
# My working directory
mydir=$rootdir/$ts
# Make directory for this specific backup
mkdir -p $mydir
##############################
# BACKUP DIRS
##############################
echo "Backing up Core Config Manager (NagiosQL)..."
#cp -rp /var/www/html/nagiosql $mydir
#cp -rp /etc/nagiosql $mydir/nagiosql-etc
tar czfps $mydir/nagiosql.tar.gz /var/www/html/nagiosql
tar czfps $mydir/nagiosql-etc.tar.gz /etc/nagiosql
echo "Backing up Nagios Core..."
#cp -rp /usr/local/nagios $mydir
tar czfps $mydir/nagios.tar.gz /usr/local/nagios
echo "Backing up Nagios XI..."
#cp -rp /usr/local/nagiosxi $mydir
tar czfps $mydir/nagiosxi.tar.gz /usr/local/nagiosxi
echo "Backing up MRTG..."
#cp -rp /usr/local/nagiosxi $mydir
tar czfps $mydir/mrtg.tar.gz /var/lib/mrtg
cp /etc/mrtg/mrtg.cfg $mydir/
##############################
# BACKUP DATABASES
##############################
echo "Backing up MySQL databases..."
mkdir -p $mydir/mysql
mysqldump -u root --password=$mysqlpass --add-drop-database -B nagios > $mydir/mysql/nagios.sql
res=$?
if [ $res != 0 ]; then
echo "Error backing up MySQL database 'nagios' - check the password in this script!"
exit $res;
fi
mysqldump -u root --password=$mysqlpass --add-drop-database -B nagiosql > $mydir/mysql/nagiosql.sql
res=$?
if [ $res != 0 ]; then
echo "Error backing up MySQL database 'nagiosql' - check the password in this script!"
exit $res;
fi
echo "Backing up PostgresQL databases..."
mkdir -p $mydir/pgsql
pg_dump -c -U nagiosxi nagiosxi > $mydir/pgsql/nagiosxi.sql
res=$?
if [ $res != 0 ]; then
echo "Error backing up PostgresQL database 'nagiosxi' !"
exit $res;
fi
##############################
# BACKUP CRONJOB ENTRIES
##############################
# Not necessary
##############################
# BACKUP SUDOERS
##############################
# Not necessary
##############################
# BACKUP LOGROTATE
##############################
echo "Backing up logrotate config files..."
mkdir -p $mydir/logrotate
cp -rp /etc/logrotate.d/nagiosxi $mydir/logrotate
##############################
# BACKUP APACHE CONFIG FILES
##############################
echo "Backing up Apache config files..."
mkdir -p $mydir/httpd
cp -rp /etc/httpd/conf.d/nagios.conf $mydir/httpd
cp -rp /etc/httpd/conf.d/nagiosxi.conf $mydir/httpd
cp -rp /etc/httpd/conf.d/nagiosql.conf $mydir/httpd
#############################
# SET THE NAME
#############################
name=$fullname
if [ -z $fullname ]; then
name=$prepend$ts$append
fi
##############################
# ZIP BACKUP
##############################
echo "Compressing backup..."
tar czfps $name.tar.gz $ts
rm -rf $ts
if [ -s $name.tar.gz ];then
echo " "
echo "==============="
echo "BACKUP COMPLETE"
echo "==============="
echo "Backup stored in $rootdir/$name.tar.gz"
exit 0;
else
echo " "
echo "==============="
echo "BACKUP FAILED"
echo "==============="
echo "File was not created at $rootdir/$name.tar.gz"
exit 1;
fi