#!/bin/sh

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

# config section

SOURCE=/                          # what to back up
DESTINATION_VOLUME=MyExternalHD   # name of the backup drive
BACKUP_NAME=Backup                # name of the disk image on the backup drive
IMAGE_SIZE=220g                   # max size how big the disk image can grow
KEEP_TIME=3M                      # how long to keep backups, see man rdiff-backup
ALERT=90%                         # when to alert about disk space running out

RDIFF_BACKUP=/opt/local/bin/rdiff-backup

# 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 disk image?
	echo Backup image not found, creating...
	FORCE="--force"
	hdiutil create -quiet -size $IMAGE_SIZE -type SPARSE -fs HFS+J \
		-volname $BACKUP_NAME -uid 0 -gid 80 -mode 0775 $IMAGE
fi

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

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

# turn Spotlight indexing off for the mounted disk image
mdutil -i off $MOUNT >/dev/null

# do the backup
$RDIFF_BACKUP --exclude-sockets              --exclude-other-filesystems \
	--exclude /.Spotlight-V100/          --exclude /automount/ \
	--exclude /cores/                    --exclude /Network/ \
	--exclude /private/tmp/              --exclude /private/var/launchd/ \
	--exclude /private/var/run/          --exclude /private/var/spool/postfix/ \
	--exclude /private/var/vm/           --exclude /tmp/ \
	--exclude "/Users/*/Library/Caches/" --exclude /Volumes/ \
	$FORCE $SOURCE $MOUNT

# remove files older than KEEP_TIME
# $RDIFF_BACKUP --remove-older-than $KEEP_TIME $MOUNT

# I have left the above command commented out, because I am waiting until
# I receive a warning about disk usage exceeding the ALERT level. Then I
# will see what is a suitable KEEP_TIME and will then uncomment the command.

# get the disk space usage, and give a warning if necessary
USAGE=`df -h $MOUNT | grep -v Capacity | awk '{print $5}'`
if [ $USAGE = "100%" ] || [ $USAGE = $ALERT ] || [[ $USAGE > $ALERT ]]; then
	echo Warning: Disk usage is $USAGE
fi

# 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