Newer
Older
tbku / tbku
#!/bin/sh
# Automated Backup Script For TundraWare Inc. Servers
# Copyright (c) 2004-2008, TundraWare Inc., Des Plaines, IL
# All Rights Reserved.
# For Updates See:  http://www.tundraware.com/Software/tbku
# $Id: tbku,v 1.116 2013/03/30 01:48:11 tundra Exp $


#####
# Things User May Want To Change
#####

###
# Program Locations
###

# Try to figure out where things are on this particular
# system.  If you can't, default to FreeBSD settings.

DATE=`which  date`
HOSTNAME=`which hostname`
LS=`which    ls`
SED=`which   sed`
TAR=`which   tar`
TOUCH=`which touch`
UNAME=`which uname`

DATE=${DATE:-"/bin/date"}
HOSTNAME=${HOSTNAME:-"/bin/hostname"}
LS=${LS:-"/bin/ls"}
SED=${SED:-"/usr/bin/sed"}
TAR=${TAR:-"/usr/bin/tar"}
TOUCH=${TOUCH:-"/usr/bin/touch"}
UNAME=${UNAME:-"/usr/bin/uname"}

###
# Default Directories
###

# You can override with corresponding environment 
# variable or change the default value here.

BKUDIR=${TBKUDIR:-"/bku"}             # Where to write the backup files
FILESETDIR=${TBKUSETS:-"$HOME/tbku"}  # Where to find fileset files

###
# Tape Device
###

TAPEDEV=${TBKUTAPE:-"/dev/sa0"}            # Default to FreeBSD SCSI tape

###
# File Naming
###

DATETIME=-`${DATE} "+%Y%m%d"`         # Datestamp used to uniquely ID backups
MACHINE=`${HOSTNAME} -f`-             # FQDN of the machine we're on
OSTYPE=`${UNAME} -s`-                 # Operating System Type
OSREV=`${UNAME} -r`-                  # Operating System Revision Level
HWTYPE=`${UNAME} -p`                  # Processor Type

BKUNAME=${MACHINE}${OSTYPE}${OSREV}${HWTYPE}    # Backup base name
BKUNAME=${TBKUNAME:-$BKUNAME}          # But the user can override it

####
# Automatic Deletion Of Previous Backups
####

# Use this with caution!

BKUDEL=${TBKUDEL:-"NO"}               # Must be "YES" for autodelete


############## Nothing Below Should Need To Be Changed #############


ALLSETS="allsets"              # Special name to force every backup set to be done
COMMON="auto"                  # Common name for automated backups

#####
# Setup
#####

umask 077                   # Make sure only owner can read backups
mkdir -p ${BKUDIR}          # Make sure the backup directory exists


#####
# Figure Out Which Backup Sets To Do
#####

cd $FILESETDIR

if [ $# -ne 0 ]
then
  if [ _$1 = _$ALLSETS ]
  then
    LIST=`${LS} * 2>/dev/null`
  else
    for arg in $*
    do
      a=`${LS} *${arg} 2>/dev/null`
      LIST=$LIST" $a"
    done
  fi

else
  LIST=`${LS} $COMMON* 2>/dev/null`
fi


#####
# Do The Actual Backups, And Leave Indication Of Start/Stop Times For Each Backup Set
#####


# If nothing matches / there is nothing to do, tell the user

LST=`echo $LIST | ${SED} "s/\ //"`   # Get rid of spaces from concatenation above
if [ "_${LST}" = "_" ]
then
  echo "tbku: Nothing to do! Exiting ..."
  exit
fi


# Otherwise process each fileset

for x in $LIST
do
  s=`echo $x | ${SED} "s/.*\.//"`   # Get the suffix

  BASENAME=$BKUNAME-$s
  STAMPEDNAME=${BASENAME}${DATETIME}

  #####
  # Distinguish Between Tape And File Backups
  #####

  if [ "_${s}" = "_tape" ]
  then
    FULLNAME=$TAPEDEV
  else
    FULLNAME=$BKUDIR/$STAMPEDNAME.tar.gz
  fi

  # Optionally, get rid of previous backups of this fileset

  if [ _${TBKUDEL} = "_YES" ]
  then
    rm $BKUDIR/.$BASENAME* $BKUDIR/$BASENAME*
  fi

  echo "Now Doing Backup Set: $s"
  ${TOUCH} $BKUDIR/.$STAMPEDNAME-begin
  ${TAR} -cvzT $FILESETDIR/$x -f $FULLNAME  >$BKUDIR/$STAMPEDNAME.log 2>&1
  ${TOUCH} $BKUDIR/.$STAMPEDNAME-end
done