#!/bin/sh # IMPORTANT: You must use a version of mkisofs recent enough that it # understands "/foo/=/foo" syntax. PATH=/usr/local/bin:/bin:/usr/bin export PATH display_usage() { /bin/cat << 'usage_eof' Usage: $0 [options] [paths] --help -h get help --dryrun -n don't do anything; just print actions to take --yes -y pre-answer all questions "yes" --quiet -q quiet mode --verbose -v verbose mode (not yet implemented) --isofs -i put ISO-9660 image in , not /backup/backup.iso --logfile -l list CD contents in after creation --speed -s speed for cdrecord (default is 1, single speed) --device -d SCSI device as controller,id,lun (default "0,6,0") --exclude -m file or directory name to ignore; can appear multiple times. "cache", ".glimpse", and ".Xauthority" are ignored by default. --blank -b blank a CD-RW before writing; is "all" or "fast" --file -f file with lines in the form DIR/=DIR or FILE=FILE which represent files or directories to be written to CD, eg "/etc/=/etc". Non-option arguments are of the form "CDDIR/=DIR" or "CDFILE=FILE", and will name files or directories to back up (as for lines in --file's file). If only "FILE" or "DIR" is specified, it or its contents will be put in the top level of the CD. A path or --file must be given. No shell metacharacters (spaces, backticks, asterisks, etc.) can appear in a pathname to be backed up. ("/my directory/=/my directory" fails.) usage_eof } error() { echo $* 1>&2 } die() { error $* exit 1 } usage_die() { error "\"$0 --help\" for usage." exit 1 } qecho() { if [ "$quiet" = "0" ] then echo "$@" fi } process_args() { original_args="$@" # to pass to children (UNUSED) really='' # 'echo' if just testing help=0 # 1 -> just display (long) help yes=0 # 1 -> noninteractive (go ahead) quiet=0 # 1 -> be quiet verbose=0 # 1 -> be verbose (UNUSED) cdrecord_v='' # '-v' if $verbose mkisofs_v='' # '-v' if $verbose isofs=/backup/backup.iso # where to create ISO-9660 image dev='0,6,0' # SCSI device to write to (ctlr,id,lun) logs=/backup/cdlogs speed=1 excludes='-m cache -m .glimpse -m .Xauthority' # ignore these dirs cdrecord_blank='' date=`date '+%Y-%m-%d-%H%M'` host=`hostname` logfile='' # name of log file to write # (default set below) dirs='' # files/dirs to copy dirfile='' # name of file with directories/files # to copy # read ./cdbackup.defaults if it exists - can override above definitions [ -r ./cdbackup.defaults ] && . ./cdbackup.defaults arg=$1 if [ $# != "0" ] ; then while ( [ -n "$arg" ] ) do case "$arg" in -h|--help) help=1; ;; -n|--dryrun) really=echo; ;; -y|--yes) yes=1; ;; -q|--quiet) quiet=1; ;; -v|--verbose) verbose=1; ;; -b|--blank) cdrecord_blank="blank=$2"; shift; ;; -d|--device) dev=$2; shift; ;; -f|--file) dirfile=$2; shift; ;; -i|--isofs) isofs=$2; shift; ;; -l|--logfile) logfile=$2; shift; ;; -m|--exclude) excludes="$excludes -m '$2'"; shift; ;; -s|--speed) speed=$2; shift; ;; -*) error "$0: unrecognized argument $arg"; usage_die ;; *) dirs="$dirs $arg"; ;; esac shift arg=$1 done fi if [ $help = 1 ] then display_usage exit 0 fi if [ $verbose = 1 ] then cdrecord_v="-v" mkisofs_v="-v" if [ $quiet = 1 ] then die "$0: --quiet (-q) and --verbose (-v) are incompatible" fi fi if [ -z "$dirs" -a -z "$dirfile" ] then error "$0: no paths specified." usage_die fi if [ -z "$logfile" ] then logfile=$logs/cdbackup$date.log # name of log file to write fi if [ ! -z "$dirfile" ] then if [ -r "$dirfile" ] then dirs="$dirs `cat $dirfile`" else error "$0: ${dirfile}: no such file or file unreadable" usage_die fi fi } ###################################################################### process_args "$@" # remove any old backup file $really rm -f $isofs qecho "Making ISO-9660 filesystem (with Rock Ridge extensions) on $isofs." # -R = Rock Ridge extensions # -T = translation table # -A = set application ID if [ "$quiet" = "1" ] then $really mkisofs -R -T \ -A "$host $date backup" \ $excludes \ -o $isofs \ $dirs > /dev/null 2>&1 else $really mkisofs -R -T \ $mkisofs_v \ -A "$host $date backup" \ $excludes \ -o $isofs \ $dirs fi if [ "$yes" != "1" ] then echo "Insert CD-R into CD recorder, quiet system, and press Return." qecho " (Warning: this may take a *long* time!)" read junk fi $really cdrecord \ $cdrecord_blank \ $cdrecord_v \ speed=$speed \ dev=$dev \ -data $isofs qecho "CD written; generating log file in $logfile" # CD-R might need a moment to be mountable? $really sleep 5 $really mount -t iso9660 -o ro /dev/scd0 /mnt $really cd /mnt if [ -z "$really" ] then find . -ls > $logfile else echo "find . -ls > $logfile" fi $really cd / $really umount /mnt qecho "Backup log is in $logfile"