X-Git-Url: http://git.treefish.org/~alex/backmeupscotty.git/blobdiff_plain/055e2cc6e3d94e1e7cc1d7158f54c4c95cdafa0f..24d125dbe2a3ad4d43792e1e7f8894484d7fda41:/backmeupscotty diff --git a/backmeupscotty b/backmeupscotty index 965da29..7a76c15 100644 --- a/backmeupscotty +++ b/backmeupscotty @@ -4,12 +4,33 @@ REMOTE_HOST=localhost REMOTE_BASE=/tmp/backmeupscotty ARCHIVE_KEEPNBACKUPS=30 ARCHIVE_KEEPNDAYS=30 +BACKUP_RUNEVERYNTHDAY=1 -while getopts ":q" opt; do +function upperme { + echo $(basename $0) | tr '[:lower:]' '[:upper:]' +} + +function scottyinfo { + echo $(upperme): $@ +} + +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 @@ -17,10 +38,39 @@ function grepbackups { ssh $REMOTE_HOST "ls $REMOTE_BASE/$1" | grep -E '[0-9]+-[0-9]+' } +function isIncomplete { + if ( ssh $REMOTE_HOST '[ -d '$REMOTE_BASE/$1/incomplete' ]' ); then + return 0 + else + return 1 + fi +} + +function isIncompleteOrNthDay { + if isIncomplete $1 || \ + [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ]; + then + return 0 + else + return 1 + fi + +} + function scottysync { timestamp=$(date +%s) - echo BACKMEUPSCOTTY: Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp + scottyinfo Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp + + if [ ! -d "$1" ]; then + scottyerror Source dir $1 does not exist. Not syncing! + return 1 + fi + + if [ $(ls -A "$1" | wc -l) -eq 0 ]; then + scottyerror Source dir $1 is empty. Not syncing! + return 1 + fi dir_current=$REMOTE_BASE/$2/current dir_incomplete=$REMOTE_BASE/$2/incomplete @@ -32,7 +82,16 @@ function scottysync { rsync_exclude=$(eval echo --exclude={$3} | tr -d {}) fi - echo BACKMEUPSCOTTY: Starting rsync + if (ssh $REMOTE_HOST '[ ! -d '$REMOTE_BASE/$2' ]'); then + scottyinfo Creating destination directory $REMOTE_HOST:$REMOTE_BASE/$2 + ssh $REMOTE_HOST "mkdir $REMOTE_BASE/$2" + fi + + if isIncomplete $2; then + scottyerror Continuing old incomplete backup + fi + + scottyinfo Starting rsync rsync -e ssh \ -v -aHAX --numeric-ids --delete --delete-excluded \ --link-dest=$dir_current \ @@ -40,9 +99,9 @@ function scottysync { $1/ $REMOTE_HOST:$dir_incomplete/ if [ $? -eq 0 ]; then - echo BACKMEUPSCOTTY: Timestamping completed backup and linking to current backup + scottyinfo Timestamping completed backup and linking to current backup ssh $REMOTE_HOST \ - "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $dir_timestamped $dir_current" + "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $(basename $dir_timestamped) $dir_current" fi while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do @@ -50,7 +109,7 @@ function scottysync { oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1) if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then - echo BACKMEUPSCOTTY: Removing old backup $oldestbackup + scottyinfo Removing old backup $oldestbackup ssh $REMOTE_HOST rm -r "$REMOTE_BASE/$2/$oldestbackup" else break