#!/bin/ksh
#
# $Revision: 1.1+12 $ $Date: 2022-12-21 21:46:03-05 $
# $Source: /root $
# $Host: furbag.my.domain $
# $UUID: 3ee9f7f1-1b92-4f49-9d31-210e1f5de196 $
#
#<bkremovable: make tarballs for removable drive

export PATH=/usr/local/bin:/bin:/usr/bin
export TMPDIR=/var/tmp
tag=${0##*/}
umask 022

logmsg () { echo "$(date '+%F %T') $tag: $@"; }
die ()    { logmsg "FATAL: $@"; exit 1; }

# CONFIGURE THIS.
archdir="/archive/bkup"     # staging area for compressed tarballs.

# Need one argument -- something to back up.
case "$#" in
    0)  die 'need a filesystem' ;;
    *)  fs="$1" ;;
esac

# Sanity checks.
test -d "$archdir" || die "$archdir: archive dest not found"
test -d "$fs"      || die "$fs: filesystem not found"
cd "$fs"           || die "$fs: cannot cd to filesystem"

case "$fs" in
    /)  dest='root' ;;
    *)  dest=$(echo "$fs" | sed -e 's!^/!!' -e 's!/!-!') ;;
esac

toc="/tmp/$dest.toc"
tarball="$archdir/$dest.tgz"

# Run find to get the table of contents.
logmsg "PWD=$(pwd)"
logmsg "FS=$fs"
logmsg "TOC=$toc"
logmsg "ARCH=$tarball"
find . -xdev -print > "$toc"

# Real work starts here.
logmsg "start tar"
tar --no-recursion -b 2560 -T "$toc" -czf "$tarball"

mv $toc $archdir
logmsg done
exit 0
