#!/bin/sh # Written by: Brenda Bell # Version: 1.1 # Date: 05 Oct 2003 # URL: opensource.theotherbell.com # # This script is distributed as is without warrenty. # # It may be freely distributed with or without modification provided # that: # # 1. This header is included in the distribution # 2. The redistributor documents his modifications immediately # below this header by providing his/her name and a description # of the modifications # # Change log: # # 05 Oct 2003: Added touch for excludefile to force its inclusion in # level 1 and level 2 backups # backup2disk_log() { logger -s -p local0.info -t backup2disk "${1}" echo ${1} return 0 } backup2disk_checkargs() { # make sure every option has a value if [ -z "$archivedir" ] || [ -z "$backupdatadir" ] || [ -z "$crossfilesystems" ] || [ -z "$dereferencelinks" ] || [ -z "$excludefile" ] || [ -z "$level" ] || [ -z "$disablecompression" ] || [ -z "$notification" ] || [ -z "$disableverify" ] || [ -z "$sourcedir" ] ; then $me $* --help return 1 fi # check options against allowed values if [ "$crossfilesystems" != "yes" -a "$crossfilesystems" != "no" ] || [ "$dereferencelinks" != "yes" -a "$dereferencelinks" != "no" ] || [ "$level" != "0" -a "$level" != "1" -a "$level" != "2" ] || [ "$disablecompression" != "yes" -a "$disablecompression" != "no" ] || [ "$notification" != "none" -a "$notification" != "summary" -a "$notification" != "list" -a "$notification" != "verboselist" ] || [ "$disableverify" != "yes" -a "$disableverify" != "no" ] ; then $me --help return 1 fi if [ "$notification" = "summary" -o "$notification" = "list" -o "$notification" = "verboselist" ] ; then if [ -z "$sendto" ] ; then $me --help return 1 fi fi # check for existence and appropriate permissions on files and directories if [ ! -d $backupdatadir ] || [ ! -w $backupdatadir ] ; then echo $backupdatadir must exist for write return 1 fi if [ "$excludefile" != "default" -a "$excludefile" != "none" ] ; then if [ ! -f $excludefile -o ! -r $excludefile ] ; then echo $excludefile must exist for read return 1 fi fi if [ ! -d $sourcedir ] || [ ! -r $sourcedir ] ; then echo $sourcedir must exist for read return 1 fi # mount filesystem if [ "$mount" != "" ] ; then $mount if [ $? -ne 0 ] ; then backup2disk_log "$mount failed!" return 1 fi fi if [ ! -d $archivedir ] || [ ! -w $archivedir ] ; then backup2disk_log "$archivedir must exist for write" return 1 fi # make sure the archive directory exists mkdir -p $archivedir/$sourcedir # configure the environment for this backup session echo "Starting backup configuration at `date`" # set up date-based controls now=`date` datestamp=`date --date "$now" +%s` sourceid=`echo $sourcedir | tr "/" "~"` datestamp0=$backupdatadir/level0-$sourceid-datestamp datestamp1=$backupdatadir/level1-$sourceid-datestamp if [ "$level" -eq 0 ] ; then if [ -f $datestamp0 -a ! -w $datestamp0 ] ; then echo $datestamp0 not writable return 1 fi fi if [ "$level" -eq 1 ] ; then if [ ! -f $datestamp0 -o ! -r $datestamp0 ] ; then echo $datestamp0 must exist for read return 1 fi if [ -f $datestamp1 -a ! -w $datestamp1 ] ; then echo $datestamp1 not writable return 1 fi fi if [ "$level" -eq 2 ] ; then if [ ! -f $datestamp1 -o ! -r $datestamp1 ] ; then echo $datestamp1 must exist for read return 1 fi fi # set up tar options if [ "$level" -eq 0 ] ; then lbltext="Level $level backup of $sourcedir as of $now" elif [ "$level" -eq 1 ] ; then afterdate="--after-date '`cat $datestamp0`'" lbltext="Level $level backup of $sourcedir from `cat $datestamp0` to $now" elif [ "$level" -eq 2 ] ; then afterdate="--after-date '`cat $datestamp1`'" lbltext="Level $level backup of $sourcedir from `cat $datestamp1` to $now" fi lbltext=`echo "$lbltext" | cut -c1-99` label="--label '$lbltext'" if [ "$excludefile" = "default" ] ; then excludefile="$backupdatadir/exclude-default" fi if [ "$excludefile" = "none" ] ; then sourcefiles="$sourcedir" else if [ ! -f $excludefile -o ! -w $excludefile ] ; then echo "$excludefile must exist for read" return 1 fi excludefrom="--exclude-from '$excludefile'" touch $excludefile sourcefiles="$excludefile $sourcedir" fi if [ "$disablecompression" != "yes" ] ; then gzip="--gzip" archiveextension="tgz" else archiveextension="tar" fi archivefile="$archivedir/$sourcedir/$datestamp.$level.$archiveextension" file="--file '$archivefile'" if [ -f "$archivefile" ] ; then echo "$archivefile exists" return 1 fi logger -s -p local0.info -t backup2disk "Preparing to archive ${sourcedir} to ${archivefile}" if [ "$crossfilesystems" != "yes" ] ; then onefilesystem="--one-file-system" fi if [ "$disableverify" != "yes" ] ; then verbose="--verbose" fi logger -s -p local1.info -t backup2disk archivedir=$archivedir/$sourcedir logger -s -p local1.info -t backup2disk backupdatadir=$backupdatadir logger -s -p local1.info -t backup2disk crossfilesystems=$crossfilesystems logger -s -p local1.info -t backup2disk dereferencelinks=$dereferencelinks logger -s -p local1.info -t backup2disk disablecompression=$disablecompression logger -s -p local1.info -t backup2disk disableverify=$disableverify logger -s -p local1.info -t backup2disk excludefile=$excludefile logger -s -p local1.info -t backup2disk level=$level logger -s -p local1.info -t backup2disk notification=$notification logger -s -p local1.info -t backup2disk sendto=$sendto logger -s -p local1.info -t backup2disk sourcedir=$sourcedir return 0 } backup2disk_notify() { if [ "$notification" = "list" ] ; then notifycmd="eval tar --list $file $gzip" elif [ "$notification" = "verboselist" ] ; then notifycmd="eval tar --list $file $gzip $verbose" fi if [ "$notification" != "none" ] ; then mail -s "$lbltext" $sendto <