#!/bin/sh
# Alexandre's backup script firstly intended to be used on my lab computer.
#
# Documentation:
# In order to have a password free method for connecting to the backup server,
# we will use a key registred to the server to allow the connection of this
# backup script.
# Generate such a key
# $ ssh-keygen -t dsa -b 1024 -f $HOME/.backup/backup-key
# Send the public key to the backup server
# $ scp $HOME/.backup/backup-key.pub $BUSER@$BHOST:/home/$BUSER/.ssh/
# Register the key on the backup server
# $ ssh $BUSER@$BHOST "cat .ssh/backup-key.pub >> .ssh/authorized_keys"
#
# Your crontab will typically look something like this:
# 59 11 * * * /home/user/bin/backup.sh
#
# Version 0.1, Alexandre Dupas, December 27, 2007
# This script is released under the terms of the GNU General Public License,
# version 2.0.

# directory to backup
BPATH=/dir/to/backup
# exclude contain the list (one item per line) of file to exclude from backup
EXCLUDE=/home/alexandre/.backup/exclude

# file containing the private key
KEY=/home/alexandre/.backup/key-backup

# backup server informations
BHOST=backup.server.com
BPORT=22
BUSER=user.on.the.backup.server
BACKUPPATH=/path/where/data/are/backed/up

# misc
LOGFILE=/home/alexandre/.backup/log
BACKUP0=backup.0
BACKUP1=backup.1
BACKUP2=backup.2
BACKUP3=backup.3

# utilities
RSYNC=rsync
SSH=ssh

# rotate the backups
$SSH -p$BPORT -i $KEY $BUSER@$BHOST "cd $BACKUPPATH; rm -rf $BACKUP3;  mv $BACKUP2 $BACKUP3; mv $BACKUP1 $BACKUP2; mv $BACKUP0 $BACKUP1"

# actual backup script
$RSYNC -avz --delete --delete-excluded \
    -e "$SSH -p$BPORT -i $KEY" \
    --link-dest=$BACKUPPATH/$BACKUP1 \
    --exclude-from=$EXCLUDE \
    $BPATH $BUSER@$BHOST:$BACKUPPATH/$BACKUP0 >> $LOGFILE

