:
##########################################################################
# Shellscript:	backup - backup (remote) system on local tape
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Requires   :	cpio, dd, ping, {rsh|ssh}
# Category   :	System Administration
# SCCS-Id.   :	@(#) backup	2.1 03/05/29
##########################################################################
# Description
# Notes
#    o	This script runs unmodified with the following operating systems:
#	Solaris 9
#	SuSE Linux 8.0
##########################################################################

PN=`basename "$0"`			# Program name
VER='2.1'

# Uncomment some of the following lines to disable auto-detection
#: ${RCMD:=rsh}				# Remotely run command
#: ${RCMDFLAGS="-l root"}
#: ${RCMD:=ssh}
#: ${RCMDFLAGS="-l root"}
#: ${TAPE:=/dev/rmt0}

timeout=5

usage () {
    echo >&2 "$PN - backup (remote) system on local tape, $VER
usage: $PN rhost [directory ...]

The specified directories (default: all) on the host \"rhost\" are
combined into an \"cpio\" archive, which then is written to a local
tape device (TAPE=$TAPE)."
    exit 1
}

pingonce () {
    if [ X"$ping" = X"" ]
    then
	# Find out how to "ping" exactly once, and determine parts
	# for the command line "$ping $pingflags $host $pingadd"
	case "`uname | tr '[A-Z]' '[a-z]'`" in
	    sunos)			# ping $host $timeout
		ping=ping;
		pingflags=
		pingadd=$timeout;;
	    linux | * )			# timeout -t $timeout ping -c 1 $host
		#ping="${timeout+timeout -t $timeout} ping";
		ping=ping
		pingflags="-c 1 -w $timeout"
		pingadd=;;
	esac
    fi
    $ping $pingflags "$@" $pingadd
}

echon () {
    if [ X"$ECHON" = X ]
    then
	# Determine how to "echo" without newline: "echo -n" or "echo ...\c"
	if [ X`echo -n` = X-n ]
	then ECHON=echo; NNL="\c"
	else ECHON="echo -n"; NNL=""
	fi
    fi
    $ECHON "$*$NNL"
}

##########################################################################

# "getopt" removes whitespace between arguments, therefore we do
# not use it here.

#set -- `getopt :h "$@"` || usage
#[ $# -lt 1 ] && usage			# "getopt" detected an error

while [ $# -gt 0 ]
do
    case "$1" in
					# your flags here
	--)	shift; break;;
	-h)	usage;;
	-*)	usage;;
	*)	break;;			# First host name
    esac
    shift
done

[ $# -gt 0 ] || set -- localhost	# Default remote host
Rhost=$1; shift
[ $# -gt 0 ] || set -- '.[!.]* .'	# Default remote directory pattern

# If no tape device was specified using the TAPE environment
# varible, try to find out a possible device

if [ X"$TAPE" = X"" ]
then
    for TAPE in /dev/rmt/1 /dev/rmt/0 /dev/rmt0 ""
    do [ -c "$TAPE" -o -b "$TAPE" ] && break
    done
fi
: ${TAPE:=/dev/rmt0}

# Program to run a command remotely. Should have the syntax
# $RCMD $RCMDFLAGS host command [...]

if [ X"$RCMD" = X"" ]
then
    if [ X"$SSH_AGENT_PID$SSH_CLIENT_PID" != X ]
    then RCMD=ssh; RCMDFLAGS="-l root"
    else RCMD=rsh; RCMDFLAGS="-l root"
    fi
fi

if [ -c "$TAPE" -o -b "$TAPE" ]		# Is $TAPE a valid device?
then
    echon >&2 "Write directories from $Rhost to local device $TAPE (^d = no)? "
    read dummy || { echo >&2; exit 0; }
else
    echo >&2 "cannot access backup device TAPE=$TAPE"
    exit 1
fi

if pingonce "$Rhost" >/dev/null 2>&1 &&
	[ X`"$RCMD" $RCMDFLAGS "$Rhost" pwd` != X ]
#if [ X`"$RCMD" $RCMDFLAGS "$Rhost" pwd` != X ]	# Try rsh/ssh
then
    # Create a remote "cpio" archive, and send it over the net to a
    # local "dd" process which writes it to the tape device.

    set -x
    "$RCMD" $RCMDFLAGS "$Rhost" 'cd /; find '"$*"' -xdev -print |
	cpio -ov -H odc' |
	dd of=$TAPE obs=10k
    set +x
else
    echo >&2 "$PN: cannot reach host \"$Rhost\" (.rhosts entry for root?)"
    echo >&2 "$PN: Please verify that \"$RCMD $RCMDFLAGS $Rhost pwd\" works"
    exit 1
fi

exit 0
