#!/bin/sh usage() { printf >&2 "Usage: %s: snapshot_dir program\n" $(basename "$0") printf >&2 "Mark directory with a timestamp and run program" printf >&2 "remove mark when program exits" exit 2 } die() { printf "%s\n" "$*" exit 1 } test $# -gt 1 || usage SNAP_SRC="$1" : ${SNAP_WRITESRC:=$SNAP_SRC} export SNAP_SRC SNAP_WRITESRC shift test -d "$SNAP_SRC" || die "not a directory: $SNAP_SRC" test -d "$SNAP_WRITESRC" || die "not a directory: $SNAP_WRITESRC" LOCKFILE="$SNAP_WRITESRC/.snaprep" touch "$LOCKFILE" || exit $? flock -n 0 <"$LOCKFILE" || exit $? echo $$ >"$LOCKFILE" || exit $? rm -f "$SNAP_WRITESRC"/.snapshot.[0-9]* 2>/dev/null mark="$SNAP_WRITESRC/.snapshot.$(date +%s)" touch "$mark" || exit $? "$@" RETCODE=$? rm -f "$mark" "$LOCKFILE" exit $RETCODE