X-Git-Url: https://git.treefish.org/~alex/backmeupscotty.git/blobdiff_plain/1aa0158b6b4d4aaca7f413d76aec814a206a212b..333d926de93d4427e4c32c52e7dd18649132a00b:/backmeupscotty diff --git a/backmeupscotty b/backmeupscotty index ed41ae2..c1fa9da 100644 --- a/backmeupscotty +++ b/backmeupscotty @@ -18,22 +18,6 @@ function scottyerror { echo $(upperme): $@ >&2 } -if [ $(pidof -x $(basename $0) | wc -w) -gt 2 ]; then - scottyerror Another instance of $(basename $0) is already running! - exit 1 -fi - -while getopts "qn:" opt; do - case $opt in - q) - exec > /dev/null - ;; - n) - BACKUP_RUNEVERYNTHDAY=$OPTARG - ;; - esac -done - function grepbackups { ssh $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+' } @@ -116,6 +100,12 @@ function scottysync { done } +function deleteLock { + if ! rmdir /tmp/$(basename $0).lock; then + scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!" + fi +} + function prepare { scottyinfo "Preparing for sync" } @@ -126,11 +116,14 @@ function cleanup_abrt { function _cleanup_abrt { scottyerror "Caught exit signal! Cleaning up..." + cleanup_abrt + if [ $(jobs -p) ]; then scottyerror TERMinating remaining child processes... kill $(jobs -p) fi + deleteLock exit } @@ -148,6 +141,7 @@ function cleanup_normal { function _cleanup_normal { cleanup_normal + deleteLock } function backmeupscotty { @@ -164,3 +158,19 @@ function backmeupscotty { trap - EXIT } + +if ! mkdir /tmp/$(basename $0).lock; then + scottyerror "Another instance of $(basename $0) is still running!" + exit 1 +fi + +while getopts "qn:" opt; do + case $opt in + q) + exec > /dev/null + ;; + n) + BACKUP_RUNEVERYNTHDAY=$OPTARG + ;; + esac +done