4/03/2015

Cacti Backup and Restore Script

Cacti Backup:
    Files will be saved in /home/cacti-backup/
-rw-r--r-- 1 root root  6851095 Mar 31 08:23 /home/cacti-backup/CactiFolder-2015-03-31.tar.gz
-rw-r--r-- 1 root root    25800 Mar 31 08:20 /home/cacti-backup/CLI_files-2015-03-31.tar.gz
-rw-r--r-- 1 root root   845639 Mar 31 08:20 /home/cacti-backup/LOG_files-2015-03-31.tar.gz
-rw-r--r-- 1 root root 65301949 Mar 31 08:20 /home/cacti-backup/RRAXML_files-2015-03-31.tar.gz
-rw-r--r-- 1 root root     5806 Mar 31 08:20 /home/cacti-backup/SCRIPT_files-2015-03-31.tar.gz
-rw-r--r-- 1 root root 2462302 Mar 31 08:23 /home/cacti-backup/Cacti-Database-2015-03-31.sql


Script:
 After run the backup script, enter your mysql password when ask.

[root@localhost backup]# cat cactibackup.sh

##################################################
#!bin/bash
#
# Script for backing up Cacti and all related folders
#
# This function finds all rrd files runs the rrdtool dump feature and deletes the xml file
rrdump ()
{
        for rrd in `find /home/cacti-backup/rra/ -type f -name "*.rrd"`
                do
                        xml=`echo $rrd | sed 's/.rrd//g'`
                        rrdtool dump $rrd > $xml.xml
                        rm $rrd
                done
}
#
# Timestamp in YYYY-MM-DD
TIME_STAMP="$(date +%Y-%m-%d)"
#
# Backup the MySQL database
mysqldump -u root -p  cacti > /home/cacti-backup/Cacti-Database-${TIME_STAMP}.sql
#
# Backup and archive the Cacti folder
tar -cvpzf /home/cacti-backup/CactiFolder-${TIME_STAMP}.tar.gz /usr/share/cacti
#
# Copy the RRA directory to the backup directory
cd /var/lib/cacti
cp -R rra /home/cacti-backup/
#
# Find all files with the extension rrd and run the RRDTOOL DUMP feature
rrdump
#
# Backup and archive the RRA folder
tar -cvpzf /home/cacti-backup/RRAXML_files-${TIME_STAMP}.tar.gz /home/cacti-backup/rra
#
# Remove the RRA folder
cd /home/cacti-backup
rm -rf rra
#
# Backup and archive all other required folders
tar -cvpzf /home/cacti-backup/CLI_files-${TIME_STAMP}.tar.gz /var/lib/cacti/cli
tar -cvpzf /home/cacti-backup/LOG_files-${TIME_STAMP}.tar.gz /var/log/cacti/
tar -cvpzf /home/cacti-backup/SCRIPT_files-${TIME_STAMP}.tar.gz /var/lib/cacti/scripts
##################################################


=============================================

Cacti restore:

cacti@cacti01:~/cacti-backup$ cat restore.sh

##################################################
#!/bin/bash
#
# Script for restoring Cacti and all related folders
#
# This function finds all rrd files runs the rrdtool dump feature and deletes the xml file
rrdrestore ()
{
   for xml in `find . -type f -name "*.xml"`
        do
                rrd=`echo $xml | sed 's/.xml//g'`
                rrdtool restore $xml $rrd.rrd
                rm $xml
        done
}

restore ()
{
   #Restoring Database
   mysql -u root -p cacti < $MySQLDatabase

   #Unpacking RRA files from archive
   tar -xvzf $RRAFiles -C /

   #Restore RRD files using RRDTOOL restore
        rrdrestore

   #Copy RRA folder to /var/lib/cacti
   cd /home/cacti-backup/
   cp -R rra /var/lib/cacti/

   #Delete RRA folder
   cd /home/cacti-backup
   rm -rf rra

   #Change ownership of RRA directory
   chown -R cacti:cacti /var/lib/cacti/rra
   chown cacti:root /var/lib/cacti/rra

   #Restore all other folders
   tar -xvzf $LOGFiles -C /
   tar -xvzf $CLIFiles -C /
   tar -xvzf $SCRIPTFiles -C /
   tar -xvzf $CactiFolder -C /

   #Change ownershipt of log/cacti directory
   chown -R cacti:apache /var/log/cacti

   echo
   echo Restoration Complete. Please restart server.
echo Please note: You may need to rebuild the poller cache once logged into Cacti
}
#
# Requesting information from user - date of backup in format YYYY-MM-DD
echo
echo
echo -n "Please enter the date from which you would like to restore from (YYYY-MM-DD):"
read date
echo
echo
echo Restoring from date $date
echo
echo
#
# Checking files exist
cd /home/cacti-backup/
MySQLDatabase=Cacti-Database-$date.sql
CactiFolder=CactiFolder-$date.tar.gz
RRAFiles=RRAXML_files-$date.tar.gz
LOGFiles=LOG_files-$date.tar.gz
CLIFiles=CLI_files-$date.tar.gz
SCRIPTFiles=SCRIPT_files-$date.tar.gz
#
#
echo Checking if files exist:
echo
echo
if [ -f $MySQLDatabase ]; then
  echo "File $MySQLDatabase exists. SUCCESS!"
else
echo "File $MySQLDatabase does not exist. FAIL!"
fi
#
#
if [ -f $CactiFolder ]; then
  echo "File $CactiFolder exists. SUCCESS!"
else
  echo "File $CactiFolder does not exist. FAIL!"
fi
#
#
if [ -f $RRAFiles ]; then
  echo "File $RRAFiles exists. SUCCESS!"
else
  echo "File $RRAFiles does not exist. FAIL!"
fi
#
#
if [ -f $CLIFiles ]; then
  echo "File $CLIFiles exists. SUCCESS!"
else
  echo "File $CLIFiles does not exist. FAIL!"
fi
#
#
if [ -f $LOGFiles ]; then
  echo "File $LOGFiles exists. SUCCESS!"
else
  echo "File $LOGFiles does not exist. FAIL!"
fi
#
#
if [ -f $SCRIPTFiles ]; then
  echo "File $SCRIPTFiles exists. SUCCESS!"
else
  echo "File $SCRIPTFiles does not exist. FAIL!"
fi
#
echo
echo
#
#Asking user if they want to continue
echo WARNING: IF ANY OF THE FILES ABOVE FAIL, RESTORE MAY.
while true; do
  read -p "DO YOU WANT TO CONTINUE?" yn
  case $yn in
    [Yy]* ) restore; break;;
    [Nn]* ) exit;;
    * ) echo "Please answer yes or no.";;
  esac
done
##################################################

Or if you ONLY want the database restored that contains all your devices and ports monitored just do:
After a fresh reinstall of Cacti:

cacti@cacti01:~/script$  mysql -u root -p cacti < Cacti-Database-2015-03-31.sql



No comments:

Post a Comment