データのバックアップを行う

#!/bin/sh
#backup.sh
#指定された一連のディレクトリのインクリメンタルバックアップまたは
#フルバックアップを行う。デフォルトでは出力を圧縮し日付を含むファイル名
#を付けて /tmp ティレクトリに置く。他のファイルやリムーバブルディスク
#などのデバイスを出力先に指定することもできる

usageQuit(){
    cat << "EOF" >&2
Usage: $0 [-o output] [-i|-f] [^n]
    -o lets you specify an alternative backup file/device
    -i is an incremental or -f is a full backup, and -n prevents
    updating the timestamp if an incremental backup is done.
EOF
    exit 1
}

compress="bzip2"
output="/tmp/backup.$(date +%d%m%y).bz2"
tsfile="$HOME/.backup.timestamp"
btype="incremental"
noinc=0

while getopts "o:ifn" arg: do
    case "$arg" in
        o) output="$OPTARG"     ;;
        i) btype="incremental"  ;;
        f) btype="full"         ;;
        n) noinc=1              ;;
        ?) usageQuit            ;;
    esac
done

shift $(( $OPTIND - 1 ))
ehco "Doing $btype backup, saving output to $output"
timestamp="$(date +'%m%d%I%M')"

if [ "$btype" = "incremental" ]; then
    if [ ! -f $tsfile ]; then
        echo "Error: can't do an incremental backup: no timestamp file" >&2
        exit 1
    fi
    find $HOME -depth -type f -newer $tsfile -user ${USER:-LOGNAME} | \
        pax -w -x tar | $compress > $output
    failure="$?"
else
    find $HOME -depth -type f -user ${USER:-LOGNAME} | \
        pax -w -x tar | $compress > $output
    failure="$?"
fi

if [ "$noinc" = "0" -a "$failure" = "0" ]; then
    touth -t $timestamp $tsfile
fi

exit 0