#!/bin/bash

SERVER="athena.olympus"
BACKUP="/opt/local/bin/rdiff-backup-2.6"
PATH="$PATH:/sbin:/usr/sbin"

function test_connection
{
    if [ "`ifconfig | grep -v "127.0.0.1" | grep "inet" | wc -l`" -ge 1 ]
    then
        return 0
    else
        return 1
    fi
}

function test_host
{
    ping -c1 -t1 $SERVER > /dev/null 2>&1
    status=$?
    if [ $status -ne 0 ]; then
        return 1
    fi

    return 0
}

test_connection
status=$?
if [ $status -ne 0 ] ; then
    exit 1
fi

test_host
status=$?
if [ $status -ne 0 ] ; then
    exit 2
fi

host=${HOSTNAME/-*}

# Proceed with the backup
${BACKUP} --exclude $HOME/Music --exclude $HOME/Movies \
          --exclude $HOME/Library --exclude $HOME/non-backup \
          $HOME athena-backups::/var/media/backups/$host-$USER \
          > $HOME/.crontab/rdiff-backup.log 2>&1
# Remove backups older than 1 month
${BACKUP} --force --remove-older-than 1M \
          athena-backups::/var/media/backups/$host-$USER \
          >> $HOME/.crontab/rdiff-backup.log 2>&1

