#!/bin/bash # backup--Creates either a full or incremental backup of a set of # defined directories on the system. By default, the output # file is compressed and saved in /tmp with a timestamped filename. # Otherwise, specify an output device (another disk, a removable # storage device, or whatever else floats your boat). setglobal compress = '"bzip2'" # Change to your favorite compression app. setglobal inclist = ""/tmp/backup.inclist.$[date +%d%m%y]"" setglobal output = ""/tmp/backup.$[date +%d%m%y].bz2"" setglobal tsfile = ""$HOME/.backup.timestamp"" setglobal btype = '"incremental'" # Default to an incremental backup. setglobal noinc = '0' # And here's an update of the timestamp. trap "/bin/rm -f $inclist" EXIT proc usageQuit { cat << ''' >&2 Usage: $0 [-o output] [-i|-f] [-n] -o lets you specify an alternative backup file/device, -i is an incremental, -f is a full backup, and -n prevents updating the timestamp when an incremental backup is done. ''' > !2 Usage: $0 [-o output] [-i|-f] [-n] -o lets you specify an alternative backup file/device, -i is an incremental, -f is a full backup, and -n prevents updating the timestamp when an incremental backup is done. EOF exit 1 } ########## Main code section begins here ########### while getopts "o:ifn" arg { match $opt { with o setglobal output = $OPTARG; # Getopts automatically manages OPTARG. with i setglobal btype = '"incremental'"; with f setglobal btype = '"full'"; with n setglobal noinc = '1'; with ? usageQuit } } shift $shExpr(' $OPTIND - 1 ') echo "Doing $btype backup, saving output to $output" setglobal timestamp = $[date +'%m%d%I%M] # Grab month,day,hour,minute from 'date'. # Curious about date formats? "man strftime" if test $btype = "incremental" { if test ! -f $tsfile { echo "Error: can't do an incremental backup: no timestamp file" > !2 exit 1 } find $HOME -depth -type f -newer $tsfile -user $(USER:-LOGNAME) | \ pax -w -x tar | $compress > $output setglobal failure = "$Status" } else { find $HOME -depth -type f -user $(USER:-LOGNAME) | \ pax -w -x tar | $compress > $output setglobal failure = "$Status" } if test $noinc = "0" -a $failure = "0" { touch -t $timestamp $tsfile } exit 0