[alt.sources] Recursive ftp from another Unix machine....

jp@hplb.hpl.hp.com (Julian Perry) (07/12/90)

Here's a shell script which connects to a remote machine and pulls in
(in binary mode) all files (recursively downwards) from the named
directories.  It's really handy for example:
	getrftp uunet.uu.net comp.sources.unix/volume14

If the ftp fails part way through it will just carry on without bothering
to transfer files it already has.  It will only talk to other Unix ftp
servers.

Comments/suggestions/defects?

Jules
--
E-MAIL:         jp@hplb.hpl.hp.com || jp@hpl.hp.co.uk
IN-REAL-LIFE:   Julian Perry
ORGANISATION:   Hewlett-Packard Laboratories, Bristol
ADDRESS:        Filton Road, Stoke Gifford, Bristol, England, BS12 6QZ
TELEPHONE:      +44 272 799910 x 24019


#---------------------------------- cut here ----------------------------------
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Julian Perry <jp@hplb.hpl.hp.com> on Thu Jul 12 16:46:32 1990
#
# This archive contains:
#	getrftp	
#
# Existing files will not be overwritten.
# Error checking via wc(1) will be performed.
# Error checking via sum(1) will be performed.

LANG=""; export LANG
PATH=/bin:/usr/bin:$PATH; export PATH

if sum -r </dev/null >/dev/null 2>&1
then
	sumopt='-r'
else
	sumopt=''
fi

if test -f getrftp
then
	echo Ok to overwrite existing file getrftp\?
	read answer
	case "$answer" in
	[yY]*)	echo Proceeding;;
	*)	echo Aborting; exit 1;;
	esac
	rm -f getrftp
	if test -f getrftp
	then
		echo Error: could not remove getrftp, aborting
		exit 1
	fi
fi
echo x - getrftp
cat >getrftp <<'@EOF'
#!/bin/sh
#
# Script to get directories of files (recursively) from a Unix
# ftp server.
#
# Usage:	getrftp <host> <dir1> [<dir2> ...]
#
# File:		getrftp
# Author:	Julian Perry	(jp@hplb.hpl.hp.com)
# Date:		90/07/12
# Revision:	1.0
#

USER=ftp
PASSWORD=`hostname`
VERBOSE=-v

if [ $# -lt 2 ]
then
  echo "usage: $0 <host> <dir1> [<dir2> ...]"
  exit 1
fi

HOST=$1
ROOT=`pwd`
TMPFIL="/tmp/grftp.$$"
trap "rm -f $TMPFIL; trap '' 0; exit" 0 1 2 13 15

shift

for DIR in $*
do

  ftp -v -g -i -n $HOST <<EOF
user $USER $PASSWORD
ls "-pR $DIR" $TMPFIL
quit
EOF

  {
  echo "user $USER $PASSWORD"
  echo "binary"

  CURDIR=$DIR
  mkdir -p $ROOT/$CURDIR
  cd $ROOT/$CURDIR

  echo "lcd $ROOT/$CURDIR"
  echo "cd /"
  echo "cd $CURDIR"

  while read line
  do
    if [ ! -z "$line" ]
    then
      case "$line" in
	*:) # Found a directory
	    CURDIR=`echo "$line" | sed -e 's/:$//'`
	    mkdir -p $ROOT/$CURDIR
	    cd $ROOT/$CURDIR
	    echo "lcd $ROOT/$CURDIR"
	    echo "cd /"
	    echo "cd $CURDIR"
	    ;;
	*/) # Found a directory to be ignored
	    ;;
	*)  # Found a file
	    if [ ! -f "$line" ]
	    then
	      echo "get $line"
	    fi
      esac
    fi
  done } < $TMPFIL | ftp $VERBOSE -g -i -n $HOST

done

@EOF
set `sum $sumopt <getrftp`; if test $1 -ne 26669
then
	echo ERROR: getrftp checksum is $1 should be 26669
fi
set `wc -lwc <getrftp`
if test $1$2$3 != 771991213
then
	echo ERROR: wc results of getrftp are $* should be 77 199 1213
fi

chmod 755 getrftp

exit 0