#!/bin/sh

# backup script
# 2007-03-11
# Ilari Scheinin
# firstname.lastname@helsinki.fi
# MIT License

# config section

DESTINATION_VOLUME=MyExternalHD   # name of the backup drive
BACKUP_NAME=Backup                # name of the disk image on the backup drive

RDIFF_BACKUP_STATISTICS=/opt/local/bin/rdiff-backup-statistics

# end of config section

VOL=/Volumes/$DESTINATION_VOLUME
IMAGE=/Volumes/$DESTINATION_VOLUME/$BACKUP_NAME.sparseimage

if [ ! -d $VOL ]; then            # can we find the backup volume?
	echo 1>&2 Error: Backup volume not found.
	exit 1
fi

if [ ! -f $IMAGE ]; then          # can we find the backup image?
	echo 1>&2 Error: Backup image not found.
	exit 1
fi

# mount the image and use grep and awk to grab the path to the mounted volume
MOUNT=`hdiutil attach -readonly $IMAGE | grep Apple_HFS | awk '{print $3}'`

if test ! -d $MOUNT; then         # did the image mount successfully?
	echo 1>&2 Error: Could not mount backup image.
	exit 1
fi

if test $# -eq 0; then            # no arguments given
	ARGS=""
elif test $# -gt 1; then          # more than one argument
	ARGS=$*
elif test $1 = "last"; then       # get only the last backup
	ARGS="--begin-time 1D"
else                              # one argument
	ARGS="--begin-time $1"
fi

# get the backup statistics
$RDIFF_BACKUP_STATISTICS $ARGS $MOUNT

# unmount the disk image
hdiutil detach -quiet -force $MOUNT

exit 0

# license

The MIT License

Copyright (c) 2007 Ilari Scheinin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

# EOF
