Create a compressed tar file (across a network)

Graham Bardsley graham at tcom.stc.co.uk
Sat Oct 28 02:53:52 AEST 1989


The following shell script is a program that I find very useful for manipulating
compressed tar files. It's an interface to tar which provides the ability to
write compressed files. With it you could write a magtape with for example three
times as much data as you would without this but with no added bother. The script
adds two extra flags to tar and also the ability to tar across a net. The
extra flags are :

	d - specify the blocking factor for the dd command within ztar. This is
	    used to speed up the pipes and for shoving things across the net.
	    Defaults to 100b (512k).

	Z - Create/extract a compressed tar archive. For example, to create a 
	    compressed archive of a subdirectory tree X use :

		ztar cfZ X.tar.Z X

ztar has the ability to read/write tars across a net - to use this specify the
filename like <hostname>:/dev/<tape>

I've found ztar very useful (e.g. backing up an entire X11 tree onto one magtape
cartridge or taring to a remote drive) 


Have fun 

Graham


#! /bin/sh
# ztar : Compress tar shell script - does compression as it writes/reads a tar
# file. Also works across a network!
# Tar flags : ctx befFhilmopvw
#
# additional flags d blocks    - blocking factor for dd (default 100 (b))
#                  Z create/undo a compress file
#
# To access a remote file/device use 'ztar cf hostname:/dev/mt . ' or
# whatever.
#
# G. Bardsley 1989
#

if test "$1" = "" ; then
	echo "Usage: ztar {cxt}[flags] file ..." >&2
	exit 1
fi

flags=`echo $1 | sed -e 's/./ &/g'`
shift

for flag in $flags ; do
	case $flag in
	[ctx] )
		if test "$op" = "" ; then
			op=$flag;
			tarflags=${tarflags}${flag}
		else
			echo "Cant mix $op and ${flag} flags" >&2
			exit 1
		fi;;

	[eFhilmopvw])
		tarflags=${tarflags}${flag};;

	f )
		if test "$device" = "" ; then
			if test "$1" = "" ; then
				echo "No argument to take -f from" >&2
				exit 1
			else
				device=`echo $1 | sed -e 's/[^:]*:\(.*\)/\1/'`
				host=`echo $1 | sed -e 's/\([^:]*\):.*/\1/'`
				if test "$device" = "$host" ; then
					device=$1
					host=
				fi
				if test "$device" = "" ; then
					echo "Null device given in spec $1" >&2
					exit 1
				fi
				if test "$host" != "" ; then
					rsh_in_cmd=" rsh $host -n "
					rsh_out_cmd=" rsh $host "
				fi
				shift
			fi
		else
			echo "Already specified device $device" >&2
			exit 1
		fi;;


	b )
		if test "$tarblocks" = "" ; then
			if test "$1" != "" ; then
				tarblocks=$1
				shift
				tarflags=${tarflags}b
			else
				echo "No argument to take -b from" >&2
				exit 1
			fi
		else
			echo "Already specified -b $tarblocks" >&2
			exit 1
		fi;;

	d )
		if test "$ddblocks" = "" ; then
			if test "$1" != "" ; then
				ddblocks=${1}b
				shift
			else
				echo "No argument to take -d from" >&2
				exit 1
			fi
		else
			echo "Already specified -d $ddblocks" >&2
			exit 1
		fi;;

	Z )
		compress_cmd="| compress "
		uncompress_cmd="| compress -d ";;

	* )
		echo "Unknown flag $flag" >&2
		exit 1;;
	esac

done

if test "$op" = "c" ; then
	if test "$1" = "" ; then
		echo "Usage: ztar {cxt}[flags] file ..." >&2
		exit 1
	fi
fi

if test "$op" = "" ; then
	echo "Usage: ztar {cxt}[flags] file ..." >&2
	exit 1
fi

tardirs=$*

if test "$device" = ""; then
	device=`printenv | grep "^TAPE" | sed -e 's/TAPE=//'`
fi

if test "$device" = ""; then
	echo "No device specified" >&2
	exit 1
fi

if test "$host" != "" ; then
	PATH=/etc:/usr/etc:$PATH
	export PATH
	messg=`ping $host`
	if test "$?" != "0" ; then
		echo $messg | sed -e 's/.*: /ztar: /' >&2
		exit 1
	fi
fi

if test "$ddblocks" = ""; then
	ddblocks=100b
fi

if test "$device" != "-"; then
	in_dev="if=${device}"
	out_dev="of=${device}"
fi

trap "/bin/rm -f /tmp/zt$$; exit 0" 0 1 2 3 15

case $op in
	c )
	    eval tar ${tarflags}f - $tardirs $compress_cmd | ( $rsh_out_cmd dd ${out_dev} obs=${ddblocks} conv=sync,block 2> /tmp/zt$$) ;;

	[xt] )
	    eval '(' $rsh_in_cmd dd ${in_dev} ibs=${ddblocks} 2> /tmp/zt$$ ')' $uncompress_cmd | tar ${tarflags}Bf - ${tardirs};;
esac

sed -e '/ records /d' /tmp/zt$$ >&2

exit 0


----
Graham Bardsley,STC Telecoms,Oakleigh Road South,New Southgate, London, N11 1HB
...{uunet,mcvax}!ukc!stc!graham   <graham at tcom.stc.co.uk>   Tel: +44 1 945 2739
--
----
Graham Bardsley,STC Telecoms,Oakleigh Road South,New Southgate, London, N11 1HB
..{uunet,mcvax}!ukc!stc!graham   <graham at tcom.stc.co.uk>   Tel: +44 1 945 2739



More information about the Alt.sources mailing list