Cover V12, I04
apr2003.tar

Listing 1 backup_algo

#!/sbin/sh
# backup_algo: full backup a directory, with progresive file names
#              using tar and compress.
# Author: Juan Vera

# set a sane default mask 
umask 022

# compressor: compress(1), gzip(1), bzip2(1)
COMPRESS=compress

# using GNU/BSD tar (support -z), if not will pipe to compress(1)
#NEWER_TAR="z"

# prevent tar to cross filesystems when creating backups
#DONOTCROSSFILESYSTEMSOPTION="l"

# tar will show files while packing
#VERBOSEOPTION="v"

# prevent GNU ls to print colour control characters - mostly on linux
#GNULS="--color=tty"

# end of config
program_name=`basename $0`

# print help if no parameters 
if [ $# = 0 ]
then
   echo "syntax: $program_name <object to backup> [directory location for backup]"
   exit 1
fi

# basic check
for directory in "$@"
do
   test \! -e "$directory" &&
      echo "error: create $directory first" && exit 1
done

# setup vars used to construct the backup file name
DIRECTORY=`dirname "$1"`
WORKDIR=`ls -1Fd ${GNULS} "$2" 2> /dev/null`

PREFIX=`basename "$1"`
REL=0                        
TODAY=`date +%Y-%m-%d`                
BACKUP_FILE_PREFIX="${PREFIX}.${TODAY}-rel"

EXTENSION=tar.Z
test \! -z "${NEWER_TAR}" && EXTENSION=tgz

# create base backup file name
BACKUP_FILE="${WORKDIR}${BACKUP_FILE_PREFIX}${REL}.${EXTENSION}"

# here the real work starts
cd ${DIRECTORY}

# increment release name if there are backup files already
while [ -s "${BACKUP_FILE}" ]
do
   REL=`expr ${REL} + 1`
   BACKUP_FILE="${WORKDIR}${BACKUP_FILE_PREFIX}${REL}.${EXTENSION}"
done
# echo ${BACKUP_FILE}

# contruct tar options
TAR_OPTIONS=${NEWER_TAR}${DONOTCROSSFILESYSTEMSOPTION}${VERBOSEOPTION}

# finally, create the backup file
if [ ! -z "${NEWER_TAR}" ]
then
   tar ${TAR_OPTIONS}cf ${BACKUP_FILE} ${PREFIX} 
else
   tar ${TAR_OPTIONS}cf - ${PREFIX} | $COMPRESS -c > ${BACKUP_FILE}
fi

# set tar/compress exit value 
exit $?
# EOF