#!/bin/sh
#
# $Id$
#
# The $BACKUPROOT needs to be rsynced to an other system. 
#
# Poor mans configuration backup. Only save the new backup if configuration
# content changes
#
# Rick van der Zwet <info@rickvanderzwet.nl>

TAG=`date "+%Y%m%d.%H:%M"`

BACKUPROOT=/usr/backup/`hostname -s`/configbackup
TARGET=$BACKUPROOT/config-$TAG.tar

# Check for last backup file
mkdir -p $BACKUPROOT || exit 1
LAST=`ls -t1 $BACKUPROOT/*.tar | head -1`

if [ "$LAST" = "$TARGET" ]; then
  echo "# Error file '$TARGET' does already exists" 1>&2
  exit 1
fi

# Create new backup file
tar -C / -cf $TARGET etc usr/local/etc var/db/pkg 2>&1

# If old file this exists, only keep new files if changes exists
if [ -n "$LAST" ]; then
  PREV_MD5=`md5 -q $LAST`
  NEW_MD5=`md5 -q $TARGET`
  if [ "$PREV_MD5" = "$NEW_MD5" ]; then
    rm $TARGET
  fi
fi
