Backup your linux

There are many way to backup your linux machine.

I’ll describe one, with a nice check in Nagios, to see if the backup was successful.
For the backup we use rdiff-backup (http://rdiff-backup.nongnu.org/).

rdiff-backup can be downloaded and installed using yum and the EPEL repositiry

yum -y install rdiff-backup

With a little script in the /etc/cron.daily/ directory we can create backups:

#!/bin/bash
rdiff-backup --terminal-verbosity 2 /etc /backup/etc
rdiff-backup --terminal-verbosity 2 /usr/lib/ /backup/usr/lib/

Of course you could add as many directories as you would like.

To check if the backup was successful you could use the following script to check in Nagios if the backup of that day was successful. This script checks per directory.

instead of dir=$1 you could create an array with all directories like this:

dir[0]='/backup/usr/lib/'
dir[1]='/backup/etc/'
#!/bin/bash
### Set the directories where session files are stored ###
dir=$1

### Set some default variables we need ###
date=`date '+%Y-%m-%d'`
file="rdiff-backup-data/session_statistics.$date*.data";

# Check if the file exists:
if [ -f $dir$file ]
then
 echo the file exists
else
 echo CRITICAL - the file does not exist
 exit $STATE_CRITICAL
fi
# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

# reset problem counter
problems=0

for i in "${dir[@]}"
do
 size=`cat $i$file | grep TotalDestinationSizeChange | awk -F"(" '{print $2}' | awk -F")" '{print $1}'`
 errors=`cat $i$file | grep Errors | awk '{print$2}'`;
 if [ $errors -eq 0 ]
 then
 echo "The backup of $i on $date was without errors"
 elif [ $errors -gt 0 ]
 then
 echo "The backup of $i on $date had $errors errors"
 let problems=problems+1 
 else
 echo "Something went wrong!"
 exit $STATE_CRITICAL
 fi
done

if [ $problems -eq 0 ]
 then
 echo "OK - The backup of $date was without errors | backupsize=$size"
 exit $STATE_OK
elif [ $errors -gt 0 ]
 then
 echo "CRITICAL - The backup of $date had errors"
 exit $STATE_CRITICAL
else
 echo "CRITICAL - Something went wrong!"
 exit $STATE_CRITICAL
fi