#!/bin/sh

#  tecback - 01/2003 Karsten Kruse www.tecneeq.de
#  $Id: tecback,v 1.4 2005/08/07 10:23:08 karsten Exp $
#
#  Installation:
#   1) Leg das Script dort ab wo du viel Platz hast und mach es ausfuerbar
#      mit ,,chmod 700 tecback''.
#
#   2) Weiter unten sind einige Einstellungen die du anpassen solltest:
#        * holdbackup  - Das Alter in Tagen des aeltesten Backups das du
#                        behalten willst
#        * tar         - Voller Pfad zu einem GNU-tar (mindestens Version
#                        1.13.25)
#        * basedir     - Der volle Pfad in dem dieses Script liegt
#        * include     - Verzeichnisse die gesichert werden sollen
#        * exclude     - Verzeichnisse die nicht gesichert werden sollen
#        * compression - Mit welchem Kompressionsprogramm soll das Backup
#                        komprimiert werden? bz2=bzip2 gz=gzip keine=nichts
#
#   3) Von Hand starten um sicherzustellen das alles klappt: ,,/pfad/tecback''
#
#   4) Einen Cronjob anlegen, z.b. so: 10 2 * * * nice /pfad/tecback
#

holdbackup=62
tar=/usr/pkg/bin/gtar
basedir=/home/_backup
include="/etc /root /usr/X11R6/lib/X11/xdm /usr/pkg/etc /usr/share/sendmail/m4 /var/cron/tabs /var/mail"
exclude="/home/karsten/bin /home/karsten/mldonkey /home/alex/backup" 
compression="bz2"


###### DO NOT EDIT BELOW ####################################################
bomb() { echo ERROR: $1 ; exit 1 ; }                 # bail out

workdir=${basedir}/archive
stampfile=${workdir}/.stampfile
incrementlist=/tmp/tecback_list.$$
hostname=`hostname -s`
monthday=`date +%d`
epoch=`date +%yy%mmo%dd_%Hh%Mm%Ss`

[ -x $tar ] || bomb "$tar nicht gefunden, Pfad falsch?"
if [ ! -d $workdir ] ; then
  mkdir -p $workdir || bomb "konnte $workdir nicht anlegen, keine Rechte?"
fi

for i in $include ; do
  if [ -d $i ] ; then
    includeline="$i $includeline"
  else
    echo "$i in \$include existiert nicht und wird ignoriert"
  fi
done
for i in $exclude $workdir ; do 
  if [ -d $i ] ; then
    ignoreline="--exclude=$i/* $ignoreline"
  else
    echo "$i in \$exclude existiert nicht und wird ignoriert"
  fi
done

# kompression
case $compression in
  bz2|bzip2)   endung="tar.bz2"
	       compcom="-j"
	       ;;
  gz|gzip)     endung="tar.gz"
	       compcom="-z"
	       ;;
  keine|none)  endung="tar"
	       compcom=""
	       ;;
  *)           bomb "$compression ist keine gueltige Auswahl fuer compression"
esac

# needed since find, even if it has not found a file, exits with 0
fullbackups=0
for i in `find $workdir -type f -name "${hostname}_full*" -print` ; do
  fullbackups=1
done

# full or incremental backup?
if [ `date +%d` = 01 \
      -a ! -f ${workdir}/${hostname}_full_`date +%yy%mmo%dd_`*.${endung} \
      -o ! -f $stampfile \
      -o $fullbackups = 0 ] ; then
  kind=full
else
  kind=increment
fi

# set title of backup
title=${workdir}/${hostname}_${kind}_${epoch}.${endung}

# here we do some work
if [ $kind = increment ] ; then
  last_backup=`ls -l $stampfile | awk '{print $7"."$6".",$8}'`
  echo "==> performing incremental backup (new files since $last_backup)"
  find $includeline -type f -newer $stampfile -print > $incrementlist
  $tar -c $compcom -p $ignoreline --file $title --files-from=$incrementlist \
    && touch -r $incrementlist $stampfile
  rm $incrementlist
elif [ $kind = full ] ; then
  echo "==> performing full backup ..."
  $tar -c $compcom -p $ignoreline --file $title $includeline \
    && touch $stampfile
fi
chmod 600 $title

# delete old backups
find $workdir -type f -name "${hostname}_*" -ctime +$holdbackup -exec rm {} \;
echo "==> ... done"
