Ad blocker detected: Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker on our website.
##################################################
#!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<PASSWORD> 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
##################################################
##################################################
#!/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<PASSWORD> 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
##################################################
Please excuse the crudeness of the script - it's my first script and there's a little tweaking to do - making it more streamline.
My question - how often do you backup your cacti installations? Once a day as what I'm doing or more?
The reason for me asking is right now, I backup at 22:00 every day. If something happens to the server at say 19:00, my last backup is 22:00 the previous night... I'm thinking about backing up every hour...
Last edited by villieb on Fri Apr 05, 2013 5:44 am, edited 1 time in total.
##################################################
#!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 –-range-check|-r
rm $rrd
done
}
#
# Timestamp in YYYY-MM-DD
TIME_STAMP="$(date +%Y-%m-%d)"
#
# Backup the MySQL database
mysqldump -u root -p<PASSWORD> 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
##################################################
##################################################
#!/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<PASSWORD> 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
##################################################
Please excuse the crudeness of the script - it's my first script and there's a little tweaking to do - making it more streamline.
My question - how often do you backup your cacti installations? Once a day as what I'm doing or more?
The reason for me asking is right now, I backup at 22:00 every day. If something happens to the server at say 19:00, my last backup is 22:00 the previous night... I'm thinking about backing up every hour...
Hi villieb, can you edit your original post and change that ?
Btw. Thanks for these. I think the XML dump does actually make sense as the backup then is operating and system architecture independant so they can be restored on 32bit, 64bit Linux, Centos, FreeBSD or Solaris
Not updating the original post... I found this wasn't working so I was simply using "rrdtool dump $rrd > $xml.xml"
phalek wrote:Hi villieb, can you edit your original post and change that ?
Btw. Thanks for these. I think the XML dump does actually make sense as the backup then is operating and system architecture independant so they can be restored on 32bit, 64bit Linux, Centos, FreeBSD or Solaris