X-Git-Url: https://git.treefish.org/~alex/backmeupscotty.git/blobdiff_plain/5bfca64faf83a059b06cd63c259789989f59bc94..f877daff4c14c12d44baca7c0abe84ffc424057f:/backmeupscotty?ds=sidebyside diff --git a/backmeupscotty b/backmeupscotty index e0689a0..1df23cd 100644 --- a/backmeupscotty +++ b/backmeupscotty @@ -6,16 +6,31 @@ ARCHIVE_KEEPNBACKUPS=30 ARCHIVE_KEEPNDAYS=30 BACKUP_RUNEVERYNTHDAY=1 -function upperme { - echo $(basename $0) | tr '[:lower:]' '[:upper:]' +_ERROR_ENCOUNTERED=0 +_UPPERME=$(echo $(basename $0) | tr '[:lower:]' '[:upper:]') + +function timestamp { + date +'[%y-%m-%d|%H:%M:%S]' +} + +function scottyline { + echo $(timestamp) $_UPPERME: $@ } function scottyinfo { - echo $(upperme): $@ + if [ $_ERROR_ENCOUNTERED -eq 0 ]; then + scottyline $@ + else + scottyline $@ >&2 + fi } function scottyerror { - echo $(upperme): $@ >&2 + scottyline $@ >&2 + if [ $_ERROR_ENCOUNTERED -eq 0 ]; then + _ERROR_ENCOUNTERED=1 + scottyline "Going into verbose mode after error encounter." >&2 + fi } function ssh255 { @@ -110,6 +125,8 @@ function scottysync { scottyinfo "Timestamping completed backup and linking to current backup." ssh255 $REMOTE_HOST \ "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $(basename $dir_timestamped) $dir_current" + else + scottyerror "Rsync failed." fi while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do @@ -125,6 +142,12 @@ function scottysync { done } +function deleteLock { + if ! rmdir /var/lock/$(basename $0); then + scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!" + fi +} + function cleanup_abort { scottyerror "Caught exit signal! Cleaning up." @@ -135,6 +158,8 @@ function cleanup_abort { kill $(jobs -p) fi + deleteLock + exit } @@ -146,6 +171,11 @@ function prepare { scottyinfo "No prepare function was defined." } +function cleanup_normal { + cleanup + deleteLock +} + function printhelp { cat </var/run/$(basename $0).pid -if ! flock -n 200; then +if ! mkdir /var/lock/$(basename $0); then scottyerror "Another instance of $(basename $0) is still running!" exit 1 +else + trap deleteLock EXIT fi -echo $$ 1>&200