#! /bin/sh
#
# An working example for a script that does
# either a full or incremental backup and
# can verify the backup. It requires growisofs
# and either star or a tar that supports d/--diff
# and -X (GNU tar is okay).
#
# Author: Patrick Ohly, Patrick.Ohly@gmx.de
# License: do whatever you like with it...
#

set -e

#
# Edit this script to customize it:
#

# directories to be backed up - without leading
# slash, but relative to /
dirs="etc var/spool usr/local/bin usr/local/home"

# the DVD writer
dev=/dev/scd1

# intermediate mount point for backup
mnt=/mnt

# intermediate file with exclude patterns (not deleted,
# to allow inspection)
exclude=/tmp/backup.exclude

# a logfile of backup/verify process
log=/tmp/backup.log

# the image will have one subdirectory with this
# prefix followed by...
prefix=backup-
# ... the current date/time in this format
current="$prefix`date +%Y-%m-%d-%H:%M`"

# file exclusion patterns, see mkisofs -exclude-list
cat >$exclude <<EOF
*/.netscape/cache
*/.galeon/mozilla/galeon/Cache
var/spool/squid
var/spool/cruft
*~
EOF

# other mkisofs options - note that -J is not used
# here because it caused mkisofs to abort due to
# filename collisions. It might be useful to read
# the backup under Windows. -D is used to preserve
# the directory structure, even if that requires
# breaking the ISO 9660 standard.
mkisofsopts="-R -D"

case "$1" in
        incremental)
                mode=-M
                mount $dev $mnt
                root="-root $current -old-root `cd $mnt && ls -d $prefix* | tail -1`"
                umount $mnt
                ;;
        full)
                mode=-Z
                root="-root $current"
                ;;
        verify)
                mount $dev $mnt

                # choose the tar command that better suits you...
                cmd="(cd $mnt/`cd $mnt && ls -d $prefix* | tail -1` && tar cf - *) | (cd / && tar df - -X $exclude $dirs ) 2>&1 | tee $log"
                # cmd="(cd $mnt/`cd $mnt && ls -d $prefix* | tail -1` && star -c -f - *) | (cd / && star -diff diffopts=perm,type,uid,gid,size,data,rdev,symlink,mtime,ctime -f - -V `sed -e 's/\(.*\)/pat=\1/' $exclude` $dirs ) 2>&1 | tee $log"
                echo $cmd
                set +e
                eval $cmd
                res=$?
                umount $mnt
                exit $res
                ;;
        *)
                echo "usage: $0 full|incremental|verify"
                exit 1
                ;;
esac

cd /
cmd="growisofs $mode $dev $root -quiet $mkisofsopts -exclude-list $exclude -graft-points `for i in $dirs; do echo $i=$i; done` 2>&1 | tee $log"
echo $cmd
eval $cmd
exit $?
