#!/bin/sh

# backup script
# 2007-09-08
# Ilari Scheinin
# firstname.lastname@helsinki.fi
# MIT License

# config section

SOURCE=/
DRIVE=MyBackupDrive
KEEP_TIME=6M
ALERT=95%
SLEEP_IF_IDLE=1800
WAKE_TIME=10:00:00

# end of config section

export PATH=/opt/local/bin:/usr/sbin:$PATH

DESTINATION=/Volumes/$DRIVE
DEVICE=/dev/$(diskutil list | grep $DRIVE | awk '{print $6}')

if [ ! -d $DESTINATION ] && [ $DEVICE ]; then
	MOUNTED=1
	diskutil mount $DEVICE >/dev/null
fi

if [ ! -d $DESTINATION ]; then
	echo Error: Could not mount backup device. >&2
	exit 1
fi

mdutil -i off $DESTINATION >/dev/null

if [ $# -gt 0 ] && [ $1 = "stats" ]; then
	if [ $# -eq 1 ]; then
		ARGS=""
	elif [ $# -gt 2 ]; then
		ARGS=$*
	elif [ $2 = "last" ]; then
		ARGS="--begin-time 1D"
	else
		ARGS="--begin-time $2"
	fi
	rdiff-backup-statistics $ARGS $DESTINATION
	if [ $MOUNTED ]; then
		diskutil eject $DESTINATION >/dev/null
	fi
	exit 0
fi

diskutil repairPermissions / &>/dev/null

rdiff-backup \
	--remove-older-than $KEEP_TIME \
	--terminal-verbosity 2 \
	--force \
	$DESTINATION
rdiff-backup \
	--exclude-fifos \
	--exclude-other-filesystems \
	--exclude-sockets \
	--exclude /.Spotlight-V100 \
	--exclude /automount \
	--exclude /cores \
	--exclude /Network \
	--exclude /private/tmp \
	--exclude /private/var/vm \
	--exclude /tmp \
	--exclude "/Users/*/.Trash" \
	--exclude "/Users/*/Library/Caches" \
	--exclude "/Users/*/tmp" \
	--exclude /Volumes/ \
	$* $SOURCE $DESTINATION

USAGE=$(df $DESTINATION | grep -v Capacity | awk '{print $5}')
if [ $USAGE = "100%" ] || [ $USAGE = $ALERT ] || [[ $USAGE > $ALERT ]]; then
	echo Warning: Disk usage on $DRIVE is $USAGE
fi

bless -folder $DESTINATION/System/Library/CoreServices

if [ $MOUNTED ]; then
	diskutil eject $DESTINATION >/dev/null
fi

if [ -t 0 ]; then
	exit 0
fi
if [ ! $SLEEP_IF_IDLE -gt 0 ]; then
	exit 0
fi
M=$(date "+%b")
D=$(date "+%d")
SLEPT=$(zgrep -e "$M[ ]{1,2}$D $WAKE_TIME .* System Wake" /var/log/system.log*)
if [ ! "$SLEPT" ]; then
	exit 0
fi
IDLE_TIME=$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}')
if [ $IDLE_TIME -gt $SLEEP_IF_IDLE ]; then
	pmset schedule sleep "$(date "+%D %H:")$(expr $(date "+%M") "+" 2):00"
fi

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