X-Git-Url: https://git.treefish.org/~alex/backmeupscotty.git/blobdiff_plain/aad0734411ecf750bf2233dba6cffb333da50d68..037b181608965bcd921c2ae115af03c18ed27b35:/backmeupscotty?ds=sidebyside diff --git a/backmeupscotty b/backmeupscotty index 44e9c54..d6f358f 100644 --- a/backmeupscotty +++ b/backmeupscotty @@ -5,17 +5,32 @@ REMOTE_DIR=/tmp/backmeupscotty/test ARCHIVE_KEEPNBACKUPS=30 ARCHIVE_KEEPNDAYS=30 BACKUP_RUNEVERYNTHDAY=1 +BWLIMIT=500KiB -function upperme { - echo $(basename $0) | tr '[:lower:]' '[:upper:]' +_ERROR_ENCOUNTERED=0 + +function timestamp { + date +'[%y-%m-%d|%H:%M:%S]' +} + +function scottyline { + echo $(timestamp) $@ } 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 { @@ -43,20 +58,19 @@ function isIncomplete { } function isNthDay { - if [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ]; + if [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ]; then return 0 else return 1 - fi + fi } function latestTooOld { for oldbackup in $(grepbackups); do tstamp=$(echo $oldbackup | cut -d'-' -f1) - - if [ $(( $(date +%s) - $tstamp )) -lt \ - $(( ($BACKUP_RUNEVERYNTHDAY*24+12)*60*60 )) ] + + if [ $(( $(date +%s) - $tstamp )) -lt $(( ($BACKUP_RUNEVERYNTHDAY*24+12)*60*60 )) ] then return 1 fi @@ -100,16 +114,19 @@ function scottysync { fi scottyinfo "Starting rsync." - rsync -e ssh \ - -v -aHAX --numeric-ids --delete --delete-excluded \ - --link-dest=$dir_current \ - $rsync_exclude \ - $SYNC_SRC/ $REMOTE_HOST:$dir_incomplete/ - + rsync -e ssh --bwlimit=$BWLIMIT \ + -v -aHAX --numeric-ids --delete --delete-excluded \ + --link-dest=$dir_current \ + $rsync_exclude \ + $SYNC_SRC/ $REMOTE_HOST:$dir_incomplete/ + if [ $? -eq 0 ]; then 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" + "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,8 +142,10 @@ function scottysync { done } -function prepare { - scottyinfo "Preparing for sync." +function deleteLock { + if ! rmdir /var/lock/$(basename $0); then + scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!" + fi } function cleanup_abort { @@ -139,6 +158,8 @@ function cleanup_abort { kill $(jobs -p) fi + deleteLock + exit } @@ -150,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