Cacti Backups

Post support questions that directly relate to Linux/Unix operating systems.

Moderators: Moderators, Developers

Author
Message
villieb
Cacti User
Posts: 75
Joined: Fri Nov 09, 2012 5:14 am

Cacti Backups

#1 Post by villieb » Fri Mar 01, 2013 4:29 am

Hi,

I've started backing up my cacti installation, once a day...

Here's the script that does the backup

Code: Select all

##################################################
#!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
##################################################
And restore

Code: Select all

##################################################
#!/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.

User avatar
gandalf
Developer
Posts: 22375
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Cacti Backups

#2 Post by gandalf » Wed Mar 06, 2013 3:42 pm

We backup all changes on a daily basis and do a mysqldump daily. But we don't dump to XML.
R.

zabihhotak
Posts: 5
Joined: Tue Mar 05, 2013 4:35 am

Re: Cacti Backups

#3 Post by zabihhotak » Sat Mar 09, 2013 4:05 am

villieb wrote:Hi,

I've started backing up my cacti installation, once a day...

Here's the script that does the backup

Code: Select all

##################################################
#!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
##################################################
And restore

Code: Select all

##################################################
#!/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...
is there any simple way for backup...

mouthpiec
Posts: 15
Joined: Sat Mar 30, 2013 9:24 am

Re: Cacti Backups

#4 Post by mouthpiec » Fri Apr 05, 2013 5:18 am

hi ,thanks for this script, it works perfectly except that in the loop

Code: Select all

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
}
I am getting the error

Code: Select all

./cacti_backup.sh: line 12: -r: command not found
I am running this on an CentOS platform.
Can you please help?

User avatar
phalek
Cacti Guru User
Posts: 2711
Joined: Thu Jan 31, 2008 6:39 am
Location: Kressbronn, Germany
Contact:

Re: Cacti Backups

#5 Post by phalek » Fri Apr 05, 2013 5:21 am

Code: Select all

rrdtool dump $rrd > $xml.xml
EDIT: Changed command as the orignally posted additional parameter does cause errors
Last edited by phalek on Fri Apr 05, 2013 7:03 am, edited 1 time in total.
Greetings,
Phalek
---
Need more help ? Read the Cacti documentation or my new Cacti 1.x Book
Need on-site support ? Look here Cacti Workshop
Need professional Cacti support ? Look here CereusService
---
Plugins : CereusTransporter | CereusReporting | nmidWebService | nmidSmokeping | nmidWeb2 |

Code: Select all

CereusServer Master:  SYSTEM STATS: Time:2.5621 Method:spine Processes:1 Threads:16 Hosts:446 HostsPerProcess:446 DataSources:14683 RRDsProcessed:7573
CereusServer Agent:   SYSTEM STATS: Time:27.4840 Method:spine Processes:1 Threads:8 Hosts:16 HostsPerProcess:16 DataSources:114576 RRDsProcessed:48061
[/size]

mouthpiec
Posts: 15
Joined: Sat Mar 30, 2013 9:24 am

Re: Cacti Backups

#6 Post by mouthpiec » Fri Apr 05, 2013 5:24 am

thanks for the fast reply.

Line 12 is:

Code: Select all

rrdtool dump $rrd > $xml.xml .-range-check|-r

villieb
Cacti User
Posts: 75
Joined: Fri Nov 09, 2012 5:14 am

Re: Cacti Backups

#7 Post by villieb » Fri Apr 05, 2013 5:25 am

Sorry my mistake...

mouthpiec
Posts: 15
Joined: Sat Mar 30, 2013 9:24 am

Re: Cacti Backups

#8 Post by mouthpiec » Fri Apr 05, 2013 5:30 am

villieb wrote:Sorry my mistake...
what is the mistake villieb?

User avatar
phalek
Cacti Guru User
Posts: 2711
Joined: Thu Jan 31, 2008 6:39 am
Location: Kressbronn, Germany
Contact:

Re: Cacti Backups

#9 Post by phalek » Fri Apr 05, 2013 5:32 am

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 :-)

mouthpiec, the line needs to be changed to:

Code: Select all

rrdtool dump $rrd > $xml.xml
EDIT: Changed command as the orignally posted additional parameter does cause errors
Last edited by phalek on Fri Apr 05, 2013 7:03 am, edited 1 time in total.
Greetings,
Phalek
---
Need more help ? Read the Cacti documentation or my new Cacti 1.x Book
Need on-site support ? Look here Cacti Workshop
Need professional Cacti support ? Look here CereusService
---
Plugins : CereusTransporter | CereusReporting | nmidWebService | nmidSmokeping | nmidWeb2 |

Code: Select all

CereusServer Master:  SYSTEM STATS: Time:2.5621 Method:spine Processes:1 Threads:16 Hosts:446 HostsPerProcess:446 DataSources:14683 RRDsProcessed:7573
CereusServer Agent:   SYSTEM STATS: Time:27.4840 Method:spine Processes:1 Threads:8 Hosts:16 HostsPerProcess:16 DataSources:114576 RRDsProcessed:48061
[/size]

mouthpiec
Posts: 15
Joined: Sat Mar 30, 2013 9:24 am

Re: Cacti Backups

#10 Post by mouthpiec » Fri Apr 05, 2013 5:38 am

worked perfectly now!
I have the weathermap plugin in cacti .... will this script backup also the weathermap config?

thanks for the script :)

villieb
Cacti User
Posts: 75
Joined: Fri Nov 09, 2012 5:14 am

Re: Cacti Backups

#11 Post by villieb » Fri Apr 05, 2013 5:43 am

mouthpiec wrote:
villieb wrote:Sorry my mistake...
what is the mistake villieb?
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 :-)

mouthpiec, the line needs to be changed to:

Code: Select all

rrdtool dump $rrd > $xml.xml –-range-check
When I tried this I was getting "ERROR: usage rrdtool dump [--no-header | -n] file.rrd [file.xml]"

I'll modify the original post ommiting "–-range-check|-r"

villieb
Cacti User
Posts: 75
Joined: Fri Nov 09, 2012 5:14 am

Re: Cacti Backups

#12 Post by villieb » Fri Apr 05, 2013 5:47 am

mouthpiec wrote:worked perfectly now!
I have the weathermap plugin in cacti .... will this script backup also the weathermap config?

thanks for the script :)
How big is your RRA archive after running the backup?

The script will backup everything in the cacti folder (including the plugins directory).

I'm running Centos 6.3 32bit and restored to Centos 6.3 64bit with only a little gap in the graph...

mouthpiec
Posts: 15
Joined: Sat Mar 30, 2013 9:24 am

Re: Cacti Backups

#13 Post by mouthpiec » Fri Apr 05, 2013 5:54 am

will this answer your question:

Code: Select all

-rw-r--r-- 1 root root 8.1M Apr  5 12:34 CactiFolder-2013-04-05.tar.gz
-rw-r--r-- 1 root root  26K Apr  5 12:35 CLI_files-2013-04-05.tar.gz
-rw-r--r-- 1 root root 498K Apr  5 12:35 LOG_files-2013-04-05.tar.gz
-rw-r--r-- 1 root root  15K Apr  5 12:35 RRAXML_files-2013-04-05.tar.gz
-rw-r--r-- 1 root root 5.7K Apr  5 12:35 SCRIPT_files-2013-04-05.tar.gz

villieb
Cacti User
Posts: 75
Joined: Fri Nov 09, 2012 5:14 am

Re: Cacti Backups

#14 Post by villieb » Fri Apr 05, 2013 6:08 am

Are you seeing any errors when running the script?

My file sizes are

Code: Select all

-rwxr-xr-x 1 root root 4666961 Apr 5 11:59 Cacti-Database-2013-04-05.sql
-rwxr-xr-x 1 root root 4669822 Apr 5 11:59 CactiFolder-2013-04-05.tar.gz
-rwxr-xr-x 1 root root 25522 Apr 5 12:01 CLI_files-2013-04-05.tar.gz
-rwxr-xr-x 1 root root 24078 Apr 5 12:01 LOG_files-2013-04-05.tar.gz
-rwxr-xr-x 1 root root 127079366 Apr 5 12:01 RRAXML_files-2013-04-05.tar.gz
-rwxr-xr-x 1 root root 6317 Apr 5 12:01 SCRIPT_files-2013-04-05.tar.gz

mouthpiec
Posts: 15
Joined: Sat Mar 30, 2013 9:24 am

Re: Cacti Backups

#15 Post by mouthpiec » Fri Apr 05, 2013 6:33 am

no errors.
File size is similar. Can you list with the -lh option?

Post Reply