#!/bin/sh
#
# local-sysbackup: Creates an archive $BACKFILE containing a set of files in
# the system.  A number $BACKUPS of old backups are also kept and rotated in
# the same directory as the new backup.  Those files are listed in the file
# $LISTFILE, each one in a line by itself.  Blank lines and lines starting
# with `#' are ignored.
#
# Ivan Vilata i Balaguer <ivan at selidor dot net>, 2000-2002.
#
# Do what you want with this, but acknowledgement is welcome.
# Of course there is NO WARRANTY of any kind.


LISTFILE="/etc/local/backup/files"
BACKFILE="/var/backups/sysbackup.tar.gz"
BACKUPS=7


PATH="/usr/local/bin:/usr/bin:/bin"
LC_CTYPE=C
umask 077


progname=$(basename $0)
backdir=$(dirname "$BACKFILE")

if [ ! -d "$backdir" ] ; then
	echo "$progname: the backup directory does not exist" > /dev/stderr
	exit 1
fi

if [ ! -e "$LISTFILE" ] ; then
	echo "$progname: the file list does not exist" > /dev/stderr
	exit 1
fi


# Rotate old backups.
filerotate "$BACKFILE" $BACKUPS

# Unnecessary lines are filtered out from the file list and the result
# is used to create the archive.  Permissions are safed then.
{
	sed '/^#/d;/^[[:space:]]*$/d;s#^/##' "$LISTFILE" | \
	    nice tar -cz -C / -T - -f "$BACKFILE"
	TSTAT=$?
} \
	&& chown root:root "$BACKFILE" \
	&& chmod 600 "$BACKFILE"

exit $TSTAT
