Cacti Sandbox

Cacti Sandbox


Having a sandbox is great way to test your disaster recovery methods.  I’ll step through a process that makes a backup of the Cacti data.  Then we can use that backup file to restore from as needed.

There are some prerequirements to the steps I’ll be using.  These are LAMP, phpMyAdmin, Webmin, Cacti, and a ssh client.

Do keep in mind that any plugins or templates that are not standard are not included in this backup.  This simply gives you a way to view your Cacti graphs on a sandbox.

With that said, lets begin.

Backup the Source Cacti Data

First we’ll backup the Cacti data on the source.

I like to get all the tools running before starting.  Open up a ssh session to the source.  Also open up the source system’s Webmin, Cacti, and phpMyAdmin web pages.

Create a folder on the source to back everything up to with this command.  It creates a folder with a time stamp.

# mkdir /home/local/cacti-temp-$(date +%Y%m%d)

Disable CRON Cacti Poller on the source Cacti Server, I do this from Webmin.  The rest of these steps will be done with a terminal session on the source Cacti Server.

Next, I’ll backup the cacti database on the source Cacti Server with this command.

# mysqldump -u root -p cacti > /home/local/cacti-temp-$(date +%Y%m%d)/cactidump.sql

Finally, I’ll convert the RRD files to XML on the source Cacti Server with following commands under root using su:

# cd /var/lib/cacti/rra
# for i in `find -name "*.rrd"`; do rrdtool dump $i > /home/local/cacti-temp-$(date +%Y%m%d)/$i.xml; done

Now the CRON Cacti Poller can be restarted, we’ve got our backup done.  Next, I’ll compress the XML files with these commands:

# cd /home/local/cacti-temp-$(date +%Y%m%d)
# tar cvfz cacti-backup_$(date +%Y%m%d).tar.gz *

One final thing before uploading everything is changing the rights to the backup folder and files so our ordinary user can access them.

# chown local:local /home/local/cacti-temp-$(date +%Y%m%d)
# chown local:local /home/local/cacti-temp-$(date +%Y%m%d)/*

Then I’ll copy the backup files to the target Cacti Server with command using my user account.

# scp cacti-backup_$(date +%Y%m%d).tar.gz user@target_cacti_server:/home/local
- or -
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress cacti-backup_$(date +%Y%m%d).tar.gz user@target_cacti_server:/home/local

It’s also a good idea to do house cleaning when you’re done.  These commands should suffice.

# rm /home/local/cacti-temp-$(date +%Y%m%d)/*
# rmdir /home/local/cacti-temp-$(date +%Y%m%d)

Backup the Target Cacti Data

It’s good practice to backup the Cacti on the target also.  You can revert back to the initial install should the need arise.

Here is the commands all in a row, much easier if you just run them as su, just saying.  Stop the CRON job for Cacti first, once these are done, keep it stopped.

# mkdir /home/local/cacti-temp-$(date +%Y%m%d)
# mysqldump -u root -p cacti > /home/local/cacti-temp-$(date +%Y%m%d)/cactidump.sql

# cd /var/lib/cacti/rra
# for i in `find -name "*.rrd"`; do rrdtool dump $i > /home/local/cacti-temp-$(date +%Y%m%d)/$i.xml; done

# cd /home/local/cacti-temp-$(date +%Y%m%d)
# tar cvfz cacti-backup_$(date +%Y%m%d).tar.gz *

# chown local:local /home/local/cacti-temp-$(date +%Y%m%d)
# chown local:local /home/local/cacti-temp-$(date +%Y%m%d)/*

Restore the Cacti Data on the Target

First we’ll need to decompress the source data on the target.  Issue this command.

# mkdir /home/local/cacti-restore-$(date +%Y%m%d)
# cp  /home/local/cacti-backup_$(date +%Y%m%d).tar.gz /home/local/cacti-restore-$(date +%Y%m%d)
# rm /home/local/cacti-backup_$(date +%Y%m%d).tar.gz
# cd /home/local/cacti-restore-$(date +%Y%m%d)
# tar zxvf cacti-backup_$(date +%Y%m%d).tar.gz

Next, we’ll need to clear out the existing data on our target to make way for the import data from our source.  Run these commands as su to purge the RRD files and tables from the CACTI database.

# cd /var/lib/cacti/rra
# rm /var/lib/cacti/rra/*

Now we want to restore our XML files to RRD files on our target.  Be sure to include setting the permissions.

# cp /home/local/cacti-restore-$(date +%Y%m%d)/*.xml /var/lib/cacti/rra
# for i in `find -name "*.xml"`; do rrdtool restore $i `echo $i |sed s/.xml//g`; done
# rm /var/lib/cacti/rra/*.xml
# chown www-data:www-data /var/lib/cacti/rra/*.rrd

You can exit out of su.  Now we’ll drop our CACTI database and create it again.

# mysql -u root -p
Enter password....
> drop database cacti;
> create database cacti;
> exit

This is when we’re ready to import our CACTI data back into MySQL.

# mysql -u root -p cacti < /home/local/cacti-restore-$(date +%Y%m%d)/cactidump.sql

That my friends should get you operational.  Go ahead to the Cacti web page on your target sandbox machine, you should see all of your data.

Credit to those that helped with the details of this post – May 9th, 2016

PackT publishing book titled “Cacti 0.8 – Beginnner’s Guide” from Thomas Urban.

Comments are closed.