#!/bin/sh
#############################################
#   backup script                           #
#   24/26 Aug. 2002                         #
#   (c) 2002 Chris Schuster <cs_1@gmx.de    #
#   cs-backup -My Backup Script, assumes    #
#   you have write permission to $tmpdir    #
#   and $backdir,                           #
#   Put in Cron for daily backups           #
#############################################



########### Configuration Vars ##############
#       change them to fit your needs       #
#############################################
#dir to be backed up
bakdir="/home/usrname/projects"

#where do the backups go ?
baktodir="/var/backups/usrname"

#who's the owner of the files ?
owner="usrname:usrname"

#where's the tmp dir ?
tmpdir="/tmp"


#############################################
#   don't change anything below this line   #
#############################################

#YearMonthDayHourMinutes
dat=`date +%Y%m%d%H%M`
tarfile="$tmpdir/bak-tar-$dat"
bakfile="$baktodir/$dat-cs-bak.tar.gz"

#############################################

echo "Taking dir, making tar in $tmpdir dir"
#ignore kde thumbnails, cache and Trash
tar -cf $tarfile $bakdir --exclude=kio_http --exclude=share/thumbnails --exclude=icons/favicons --exclude=Desktop/Trash

echo "Identifying MD5Sum of $tarfile..."
#Get Md5Sum of the new tarfile
/usr/bin/md5sum $tarfile | /usr/bin/awk '{print  $1}' > "$tmpdir/$dat-md5"

#check if created archive is the same as the last one
if ("/bin/grep"  "-f" "$baktodir/last-md5sum" "$tmpdir/$dat-md5") >/dev/null 2>&1 ; then

  echo "checksums match!"
  echo "Removing newly created tarfile..."
  #checks. are the same, delete archive
  rm $tarfile
  echo "cleanup..."
  rm "$tmpdir/$dat-md5"
  exit
fi

#else back old md5 up
echo "Created Archive is different! Baking this archive up!"
echo "Backing last-md5 up..."
cp $baktodir/last-md5sum{,.old}

#save the new md5
echo "Creating new md5 sum"
md5sum $tarfile | /usr/bin/awk '{print $1}' > $baktodir/last-md5sum

#gzip the tar file
echo "Gzipping ($bakfile) the newly created TAR archive..."
gzip -9 -c < $tarfile > $bakfile

#delete temporary files
echo "cleanup..."
rm "$tmpdir/$dat-md5"
rm $tarfile

#chown
echo "doing chown cs:cs"
chown $owner $bakfile


#EOF
