#!/bin/bash
#Copyright 2001-2003 William Stearns <wstearns@pobox.com>
#Released under the GPL

#Usage:
#Can be run as root or a normal user, but only root can correctly preserve
#permissions for a full system backup.  If more than one user on a 
#given system will be using this client, or multiple independent backup
#specifications need to run, you must use multiple ssh keys to keep them
#seperate; make sure you use a backup name on the Command= line, as in:
#Command="/usr/sbin/rsync-backup-server wstearns-host1-project1" dsa_key...
#rsync-backup-client / root@mybackupserver.mydomain.org:/
#	translates into
#rsync -avR -e ssh --numeric-ids -v --delete --delete-after
#$EXCLUDESPEC --exclude /proc/ / root@mybackupserver.mydomain.org:/
#	where $EXCLUDESPEC reads from the automatically populated ~/.rsync-backup/localbackuponly
#	(add or remove filespecs as you prefer).

Version=0.2.5

debug () {
	echo "$*" >/dev/stderr
	if type -path logger >/dev/null 2>/dev/null ; then
		logger -t rsync-backup-client "$*"
	fi
}

require-util () {
#Assures the utility is / utilities are in the path _and_ executable.
#FIXME Hmmm, I suppose we need "["... :-)
	while [ -n "$1" ]; do
		if ! type -path "$1" >/dev/null 2>/dev/null ; then
			debug Missing "$1", exiting.
			exit 1
		fi
		shift
	done
}

#System sanity check
require-util date cat grep wc rsync touch chmod
if [ ! -r /usr/lib/rsync-backup/exclude.default ]; then
	debug Missing /usr/lib/rsync-backup/exclude.default, exiting.
	exit 1
fi
if [ ! -r /usr/lib/rsync-backup/localbackuponly.default ]; then
	debug Missing /usr/lib/rsync-backup/localbackuponly.default, exiting.
	exit 1
fi

debug Starting rsync-backup-client $Version on `date +%Y%m%d`
debug Called with parameters "$*"

if [ `cat /etc/passwd | grep -v '^[^:]*:[\*x]:' | wc -l` -gt 0 ]; then
	debug This script needs a shadow password enabled system, please
	debug run pwconv.  Exiting.
	exit 1
fi

if [ ! -d ~/.rsync-backup ]; then
	if ! mkdir ~/.rsync-backup ; then
		debug Unable to create ~/.rsync-backup. Exiting.
		exit 1
	fi
fi

if [ ! -f ~/.rsync-backup/localbackuponly ]; then
	if touch ~/.rsync-backup/localbackuponly ; then
		chmod 600 ~/.rsync-backup/localbackuponly
		cat /usr/lib/rsync-backup/localbackuponly.default >~/.rsync-backup/localbackuponly
	else
		debug Unable to create ~/.rsync-backup/localbackuponly .  Exiting.
		exit 1
	fi
fi

if [ ! -f ~/.rsync-backup/exclude ]; then
	if touch ~/.rsync-backup/exclude ; then
		chmod 644 ~/.rsync-backup/exclude
		cat /usr/lib/rsync-backup/exclude.default >~/.rsync-backup/exclude
	fi
fi

if [ -f ~/.rsync-backup/localbackuponly ]; then
	EXCLUDESPEC="${EXCLUDESPEC} --exclude-from=$HOME/.rsync-backup/localbackuponly"
fi
if [ -f ~/.rsync-backup/exclude ]; then
	EXCLUDESPEC="${EXCLUDESPEC} --exclude-from=$HOME/.rsync-backup/exclude"
fi

if [ ! -d ~/.dontbackup/ ]; then
	mkdir --mode=700 -p ~/.dontbackup/
fi

if [ -f ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz ]; then
	rm -f ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz
fi

#debug tar -czf ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz \
# --exclude ~/.dontbackup/ --exclude ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz \
# $( for ONEFILE in $( cat ~/.rsync-backup/localbackuponly ) ; do \
# if [ -r "$ONEFILE" -o -d "$ONEFILE" ]; then \
# echo $ONEFILE ; fi ; done )

tar -czf ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz \
 --exclude ~/.dontbackup/  --exclude ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz \
 $( for ONEFILE in $( cat ~/.rsync-backup/localbackuponly ) ; do \
 if [ -r "$ONEFILE" -o -d "$ONEFILE" ]; then \
 echo $ONEFILE ; fi ; done ) 2>&1 \
 | grep -v "Removing leading \`/'"

debug rsync -aR -e ssh --numeric-ids --delete --delete-after \
--rsync-path=/usr/sbin/rsync-backup-server \
$EXCLUDESPEC --exclude /proc/ $@
rsync -aR -e ssh --numeric-ids --delete --delete-after \
--rsync-path=/usr/sbin/rsync-backup-server \
$EXCLUDESPEC --exclude /proc/ $@

debug Finished rsync-backup-client.
if [ -f ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz ]; then
	debug Please move ~/.dontbackup/rsync-excluded-$(date +%Y%m%d).tar.gz
	debug 'to an off-system location; because of their generally'
	debug password-related contents, the files in it have _not_ been backed
	debug up to the backup server.
fi





exit 0

