Backup Cacti script

This is a simple script to backup cacti.

#!/bin/bash

if [ $# -lt 2 ]; then
cat <<EOF
  Backup cacti binary
  usage:  backup_cacti.sh <BACKUP_DIR> <BACKUP_KEEP_DAYS>
  examples:      
          backup_cacti.sh /data01/cacti/cacti_backup 7       --backup cacti directory, and keep 7 days
EOF
  exit 1
fi

# arguments
BACKUP_DIR="$1"
BACKUP_KEEP_DAYS="$2"

# configuration
#The cacti home should be the real home, not softlink, otherwise tar will only tar the softlink
CACTI_DIR="cacti-0.8.7g"
CACTI_BASE="/home/mysql"
CACTI_HOME="$CACTI_BASE/$CACTI_DIR"
MAIL_TO="alexzeng@wordpress.com"

# prepare backup
DATE=`date +"%Y.%m.%d"`
BACKUP_FILE="$BACKUP_DIR/cacti.$DATE.tar.gz"

# convert rrd file to xml file, which can be restore to rra
cd $CACTI_HOME/rra
mkdir -p $CACTI_HOME/rraxml
rm -f $CACTI_HOME/rraxml/*
# only backup rra files updated in last 7 days 
for f in `find *.rrd -type f -mtime -7 `
do
  /usr/bin/rrdtool dump $f $CACTI_HOME/rraxml/$f.xml
done

# backup cacti all files except rra files because they cannot be used directly for restore
cd $CACTI_BASE
tar --exclude='rra' -zcf $BACKUP_FILE $CACTI_DIR
if [ $? -ne 0 ]; then
  mailx -s "Backup cacti cron job $0 failed" $MAIL_TO <<EOF
Backup files list :
`ls -lt $BACKUP_DIR/cacti.*.tar.gz`
EOF
else
  cat /dev/null > $CACTI_HOME/log/cacti.log
fi

#delete old files
cd $BACKUP_DIR
for backup in `find . -ctime +$BACKUP_KEEP_DAYS -name "cacti.*.tar.gz"`; do rm -f $backup; done;

It will convert the rra files to xml files for backup purpose (copying rra files directly may not work).

Advertisements

About Alex Zeng
I would be very happy if this blog can help you. I appreciate every honest comments. Please forgive me if I'm too busy to reply your comments in time.

3 Responses to Backup Cacti script

  1. manas panda says:

    This blog is really helpful and it helped me in backing up the cacti.

  2. zahid72 says:

    Very nice post. Can I know will it work when restoring to another server without loss of Tree Mode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: