# Backup function adapted from ENCS bu() { VERBOSE="" [ -z "$1" ] && set -- "-h" while [ -n "$1" ] do case "$1" in -h) cat <<-EOF Backup function v0.2 by Daniel Morrison (adapted from ENCS concept) bu [-v] [ ...] -h Show this help -v Verbosely show copies made e.g.: "original -> .old/backup" bu copies one or more files into a directory named '.old' which is created, if needed, in the same directory as the file. Files may be specified with full or relative paths. If multiple files are specified, each is handled in turn, so multiple backup directories may be created, as needed. The verbose option takes effect only for files listed AFTER the '-v' option is given on the command line. EOF return 1 ;; -v) VERBOSE="TRUE" shift; continue; ;; esac FILEOK="" [ -e "$1" ] && [ -r "$1" ] && [ -f "$1" ] \ && FILEOK="TRUE" [ -n "$FILEOK" ] || { printf "%s: File not found, could not read, or not a regular file\n" "$1" printf "Maybe try -h?\n" shift continue } case "$1" in */*) DIR=`echo $1 | sed 's%^\(.*/\)[^/]*$%\1%'` FILE=`echo $1 | sed 's%^.*/\([^/]*\)$%\1%'` ;; *) DIR="" FILE="$1" esac DIROK="" [ ! -e "${DIR}.old" ] && { mkdir "${DIR}.old" [ 0 = $? ] || { shift; continue; } } [ -d "${DIR}.old" ] && [ -w "${DIR}.old" ] \ && DIROK="TRUE" [ -n "$DIROK" ] || { printf "%s: No permission, or is not a directory\n" "${DIR}.old" shift continue } DATE=`date +'%Y%m%d.%H%M%S'` cp -p "$1" "${DIR}.old/$FILE.$DATE" [ 0 = $? ] && [ -n "$VERBOSE" ] \ && printf "%s -> %s\n" "$1" "${DIR}.old/$FILE.$DATE" shift done }