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

Version=0.2.5

mkdir -p /backups	#OK, if you don't have mkdir, you have bigger problems than this script... :-)

debug () {
	echo "$*" >>/backups/rsync-backup-debug
}

require-util () {
#Assures the utility is / utilities are in the path _and_ executable.
	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 /usr/sbin/chroot /usr/bin/rsync-static cp date rm

if [ ! -x /usr/sbin/chroot ]; then			debug No chroot ;	exit 1 ; fi
if [ ! -x /usr/bin/rsync-static ]; then			debug No rsync-static; 	exit 1 ; fi
if ! type -path cp >/dev/null 2>/dev/null ; then	debug No cp ;		exit 1 ; fi
if ! type -path date >/dev/null 2>/dev/null ; then	debug No date ;		exit 1 ; fi
if ! type -path rm >/dev/null 2>/dev/null ; then	debug No rm ;		exit 1 ; fi

if [ -n "$1" ] && [ "$1" != "--server" ]; then
	debug Using passed clientname.
	CLIENTNAME="$1"
elif [ -n "$SSH_CLIENT" ]; then
	debug Using IP address from SSH1/OpenSSH shell environment.
	CLIENTNAME="${SSH_CLIENT%% *}"
elif [ -n "$SSH2_CLIENT" ]; then
	debug Using IP address from SSH2 shell environment.
	CLIENTNAME="${SSH2_CLIENT%% *}"
else
	debug 'No passed clientname and null SSH_CLIENT and SSH2_CLIENT,'
	debug 'where do I store the backup?  Exiting.'
	exit 1
fi

if [ -n "$SSH_ORIGINAL_COMMAND" ]; then
	debug "Passed SSH command $SSH_ORIGINAL_COMMAND"
elif [ -n "$SSH2_ORIGINAL_COMMAND" ]; then
	debug "Passed SSH2 command $SSH2_ORIGINAL_COMMAND"
else
	debug Not passed a command.
fi
if type -path logger >/dev/null 2>/dev/null ; then
	logger -t rsync-backup-server Starting rsync-backup-server $Version `date +%Y%m%d` backup from ${CLIENTNAME}.
fi
debug Starting rsync-backup-server $Version `date +%Y%m%d` backup from ${CLIENTNAME}.

#If you want to require admin to create directory first, uncomment this:
#if [ ! -d "/backups/$CLIENTNAME" ]; then
#	debug No "/backups/$CLIENTNAME", please create this directory if you
#	debug want to accept backups from this client.  Exiting.
#	exit 1
#fi

mkdir -p "/backups/$CLIENTNAME/current/" 2>/dev/null

rm -f "/backups/$CLIENTNAME/current/rsync-static" >/dev/null 2>/dev/null
cp -p /usr/bin/rsync-static "/backups/$CLIENTNAME/current/"
/usr/sbin/chroot "/backups/$CLIENTNAME/current/" /rsync-static --server \
 -logDtprR --delete --delete-after --numeric-ids . /

if type -path logger >/dev/null 2>/dev/null ; then
	logger -t rsync-backup-server Finished backup from ${CLIENTNAME}.
fi
debug Finished backup from ${CLIENTNAME}.

#DISPLAY=sparrow.websense.net:10.0
#EUID=0
#HOME=/root
#HOSTTYPE=i386
#LOGNAME=root
#OSTYPE=Linux
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#PPID=15670
#SHOSTNAME=sparrow
#SSH_AUTH_SOCK=/tmp/ssh-XXoTS3Ug/agent.15670
#SSH_CLIENT=127.0.0.1 1023 22
#SSH_ORIGINAL_COMMAND=rsync --server -vvlogDtprR --delete --delete-after --numeric-ids . /
#TERM=dumb
#UID=0
#USER=root
#XAUTHORITY=/tmp/ssh-XXCdWqDd/cookies
