Introduction: How Easily to Backup Your Linux Box Using Rdiff-backup

Picture of How Easily to Backup Your Linux Box Using Rdiff-backup

This instructable will show you how to run a simple full featured backup and recovery system on linux using rdiff-backup and a usb drive.

Step 1: Assumptions

Picture of Assumptions

I am going to assume a system that uses yum for installation and have mounted you usb drive as /mnt//backup.

I use fedora, but you can use anything and just install rdiff-backup however you'd like.

Also it is available from download here: http://rdiff-backup.nongnu.org/

Step 2: Install Rdiff-backup

Picture of Install Rdiff-backup

Install rdiff-backup
[root@HOST scripts]# yum install rdiff-backup

Step 3: Identify the Directories You Want to Backup

Picture of Identify the Directories You Want to Backup

You can backup the whole system, but that may be overkill, I want to backup my /etc/ directory for any changes I may have made to named, sendmail, network, etc, /data and my home dirs.

Step 4: Automate

Picture of Automate

Clearly you don't want to do this by hand. We'll write a script.

First thing in the script, we will check to see the usb drive is mounted, and quit if not.
#!/bin/bash
#Script to backup to usb drive

BACKUPBASE="/backups"

#check to see if backup target is mounted.
if `df -h | grep $BACKUPBASE >/dev/null `
then
echo "Starting $0 `date`"
else
echo "ERROR: $BACKUPBASE not mounted"
echo "$0 exiting `date` "
exit 1
fi

Step 5: Write a Function

Picture of Write a Function

Next we will write a function to actually do the backup.

function backup {
DEST=$1$2
SOURCE=$2
echo "Src : $SOURCE"
if [-d $DEST]
then
echo "Dest: $DEST"
else
mkdir -p $DEST
echo "Dest: $DEST -created"
OPTS="--force"
fi

#Perform backup
rdiff-backup -v2 --exclude-special-files $OPTS $SOURCE $DEST
#Cleanup version files older than 4weeks
rdiff-backup -v2 --remove-older-than 4W --force $DEST
#Print a report of what we backed up and cleaned up
rdiff-backup --list-changed-since 0D23h00m $DEST
}

Step 6: Do Tell the Script Which Dirs to Backup

Picture of Do Tell the Script Which Dirs to Backup

backup $BACKUPBASE /data
backup $BACKUPBASE /etc
backup $BACKUPBASE /usr/local
backup $BACKUPBASE /home

Step 7: The Whole Thing Together Now

Picture of The Whole Thing Together Now

#!/bin/bash
#Script to backup to usb drive

BACKUPBASE="/backups"

#Check to see if backup drive is mounted.
if `df -h | grep $BACKUPBASE >/dev/null `
then
echo "Starting $0 `date`"
else
echo "ERROR: $BACKUPBASE not mounted"
echo "$0 exiting `date` "
exit 1
fi

function backup {
DEST=$1$2
SOURCE=$2
echo "Src : $SOURCE"
if [-d $DEST]
then
echo "Dest: $DEST"
else
mkdir -p $DEST
echo "Dest: $DEST -created"
OPTS="--force"
fi

#Perform backup
rdiff-backup -v2 --exclude-special-files $OPTS $SOURCE $DEST
#Cleanup version files older than 4weeks
rdiff-backup -v2 --remove-older-than 4W --force $DEST
#Print a report of what we backed up and cleaned up
rdiff-backup --list-changed-since 0D23h00m $DEST
}

backup $BACKUPBASE /data
backup $BACKUPBASE /etc
backup $BACKUPBASE /usr/local
backup $BACKUPBASE /home

Step 8: Add to Cron

Picture of Add to Cron

[root@HOST scripts]# crontab -e
10 1 * * * /usr/local/scripts/backup-rdiff.sh > /var/log/backup.log 2>&1

Step 9: Restore

Picture of Restore

To restore the most recent version you can simply copy the file out of the backup directory. If you want a version from 2 days ago:
rdiff-backup -r 2D /backup/etc/named.conf /etc/named.conf

Simple and very effective.

Comments

admin (author)2009-07-07

Hey, this is a great instructable and is very informative. Just one thing is missing... pictures! It really helps a lot when trying to follow directions so you should consider taking some photographs. Once you do that and leave me a message when you have so that we can publish your work. Thanks! Thanks for the cool instructable and we hope to publish this soon!

itrex111 (author)2012-02-06

Very good.

Dentman (author)2011-11-13

Very thorough!
Works a treat.

About This Instructable

7,281views

12favorites

License:

Bio: I like to tinker with just about anything, sometimes it works out in the end. Have fun looking at the projects, try tearing something open ... More »
More by joe:Intel Edison Garage Monitor and Alert SystemIntel Edison Fat Bike Tire AnalyzerIntel Galileo Garage Monitor
Add instructable to: