The best way to keep your data safe is to set up automated backups. And your best tool to have automated backups might be your router running OpenWrt: a router is usually always up & connected to the Internet so you'll never miss a backup run Wink.

Here is a simple Howto using rdiff-backup & mini-sendmail. You may want to check how to mount an external usb drive as root partition on your OpenWrt before you get started, as the packages required to run rdiff-backup may exceed your router memory.

Packages installation

Let's start with package installation. We need a ntp client to make sure the router's time is right, a backup tool (rdiff-backup) and some mail client to receive a mail whenever a backup operation completes.

~$ opkg update
~$ opkg install ntpclient mini-sendmail python rdiff-backup

If you plan to access data located on a Windows Share folder, you'll need the additional packages:

~$ opkg install kmod-fs-cifs cifsmount

If you want to access your data over ssh, you will use the shfsmount tool provided by the packages:

~$ opkg install kmod-shfs shfs-utils

Backup script with rdiff-backup

Rdiff-backup is a nice tool but one of its limitations is that you must have the same version of rdiff-backup on source and destination machines. To avoid breaking the backup script whenever the rdiff-backup package is updated, I choosed to mount the source and destination folders on the same machine.

Here is the backup script. It first mounts the remote filesystem using shfsmount, runs rdiff-backup with a list of folder to include or exclude and then unmounts the remote filesystem and send a report mail.

#!/bin/sh
# Backup script for mydomain.net.

SRC_DIR=/mnt/mydomain.net
DST_DIR=/mnt/my_backup_folder
MAIL_RECIPIENT=This email address is being protected from spambots. You need JavaScript enabled to view it.
TMP_FILE=/tmp/backupResult.log

# Create result mail
rm -f $TMP_FILE
cat > $TMP_FILE << EOF
To: $MAIL_RECIPIENT
Subject: backup mydomain.net

EOF

# Mount server fs
mkdir -p $SRC_DIR
shfsmount --cmd="ssh -i /root/.ssh/id_rsa %u@%h /bin/bash" This email address is being protected from spambots. You need JavaScript enabled to view it.:/ $SRC_DIR

# Run rdiff backup
/usr/bin/rdiff-backup --force --remove-older-than 60D $DST_DIR >> $TMP_FILE 2>> $TMP_FILE
/usr/bin/rdiff-backup --print-statistics \
--include "${SRC_DIR}/home/user1/docs" \
--exclude '/*' \
--exclude "${SRC_DIR}/home/user1/docs/bigFolder" \
/ $DST_DIR >> $TMP_FILE 2>> $TMP_FILE

# Umount remote fs
shfsumount $SRC_DIR
rm -Rf $SRC_DIR

# Send result mail
# usage:  sendmail [-f<name>] [-t] [-s<server>] [-p<port>] [-T<timeout>] [-v] [address ...]
sendmail This email address is being protected from spambots. You need JavaScript enabled to view it. -ssmtp.mydomain.net -p25 $MAIL_RECIPIENT < $TMP_FILE
rm $TMP_FILE

The last thing to do is setup a cron task so your backup script run automatically every night.

~$ chmod u+x /root/bin/backup.sh
~$ crontab -e
0 3 * * * /root/bin/backup.sh

Here is a sample report mail: it contains the statistics generated by rdiff-backup.

No increments older than Thu Feb  3 03:00:10 2011 found, exiting.
--------------[ Session statistics ]--------------
StartTime 1301886017.00 (Mon Apr  4 03:00:17 2011)
EndTime 1301886527.10 (Mon Apr  4 03:08:47 2011)
ElapsedTime 510.10 (8 minutes 30.10 seconds)
SourceFiles 21716
SourceFileSize 1168656464 (1.09 GB)
MirrorFiles 21716
MirrorFileSize 1168657541 (1.09 GB)
NewFiles 0
NewFileSize 0 (0 bytes)
DeletedFiles 0
DeletedFileSize 0 (0 bytes)
ChangedFiles 21
ChangedSourceSize 476032 (465 KB)
ChangedMirrorSize 477109 (466 KB)
IncrementFiles 35
IncrementFileSize 1583 (1.55 KB)
TotalDestinationSizeChange 506 (506 bytes)
Errors 14
--------------------------------------------------

You're all set! Remember to test your saved data to make sure you can actually restore a system from your backup Wink