#!/bin/sh
#
# $Id$
#
# Backup WebDAV and Subversion and Wiki
#
# Rick van der Zwet <info@rickvanderzwet.nl>


FQDN=${1:-tyan.wirelessleiden.nl}
HOSTID=`echo $FQDN | awk -F "." '{print $1}'`

BACKUP_USER=${BACKUP_USER:-${2:-backup}}
SSH_KEY=${3:-/root/.ssh/backup.key}
DIRS=${4:-"drupal wiki webfolder svn"}

EXCLUDES=`dirname $0`/excludes

DELETEROOT="/usr/backup/$HOSTID/wwwbackup/old"
MIRRORROOT="/usr/backup/$HOSTID/wwwbackup/current"

BACKUPDIR=$DELETEROOT/`date '+%Y/%M/%d'`
OPTS="--force --ignore-errors --delete-excluded 
      --delete --backup --backup-dir=$BACKUPDIR --archive"


if [ -r $EXCLUDES ]; then
  OPTS="$OPTS --exclude-from=$EXCLUDES"
fi


for DIR in $DIRS; do
  TARGET=$MIRRORROOT/$DIR
  SOURCE=/usr/local/www/$DIR
  mkdir -p $TARGET $BACKUPDIR

  # now the actual transfer
  rsync --rsh="ssh -i $SSH_KEY -l $BACKUP_USER" $OPTS $FQDN:$SOURCE $TARGET
done
