#!/bin/sh
#
# $Id$
#
# Sync the snapshot files to the local archive
#
# Rick van der Zwet <info@rickvanderzwet.nl>
#


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

# Get the SSH Port and strip FQDN
PORT=`echo $FQDN | awk -F: '{print $2}'`
PORT=${PORT:-22}
FQDN=`echo $FQDN | awk -F: '{print $1}'`

DIRS=${2:-"configbackup mysqlbackup"}
BACKUP_USER=${BACKUP_USER:-${3:-backup}}
SSH_KEY=${4:-/root/.ssh/backup.key}

EXCLUDES=`dirname $0`/excludes

SNAPSHOTROOT="/usr/backup/$HOSTID"

# Deleting will be done manually to avoid whipe-outs.
OPTS="--force --ignore-errors --archive --compress"


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


for DIR in $DIRS; do
  TARGET=$SNAPSHOTROOT/$DIR
  SOURCE=/usr/backup/$HOSTID/$DIR
  mkdir -p $TARGET || exit 1

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