#!/bin/bash

REMOTE_USER=root
REMOTE_HOST=localhost
REMOTE_DIR=/tmp/backmeupscotty/test
ARCHIVE_KEEPNBACKUPS=30
ARCHIVE_KEEPNDAYS=30
BACKUP_RUNEVERYNTHDAY=1
BWLIMIT=500KiB
BACKUP_FORCE=0
FAKE_SUPER=0

_ERROR_ENCOUNTERED=0

function timestamp {
    date +'[%y-%m-%d|%H:%M:%S]'
}

function scottyline {
    echo $(timestamp) $@
}

function scottyinfo {
    if [ $_ERROR_ENCOUNTERED -eq 0 ]; then
        scottyline $@
    else
        scottyline $@ >&2
    fi
}

function scottyerror {
    scottyline $@ >&2
    if [ $_ERROR_ENCOUNTERED -eq 0 ]; then
        _ERROR_ENCOUNTERED=1
        scottyline "Going into verbose mode after error encounter." >&2
    fi
}

function ssh255 {
    ssh -l $REMOTE_USER $@
    sshret=$?

    if [ $sshret -eq 255 ]; then
        scottyerror "SSH connection failed!"
        exit 1
    else
        return $sshret
    fi
}

function grepbackups {
    ssh255 $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+'
}

function isIncomplete {
    if ( ssh255 $REMOTE_HOST '[ -d '$REMOTE_DIR/incomplete' ]' ); then
        return 0
    else
        return 1
    fi
}

function isNthDay {
    if [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ];
    then
        return 0
    else
        return 1
    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 )) ]
        then
            return 1
        fi
    done

    return 0
}

function scottysync {
    timestamp=$(date +%s)

    scottyinfo "Syncing $SYNC_SRC to $REMOTE_HOST:$REMOTE_DIR @$timestamp."

    if [ ! -d "$SYNC_SRC" ]; then
        scottyerror "Source dir $SYNC_SRC does not exist. Not syncing!"
        return 1
    fi

    if [ $(ls -A "$SYNC_SRC" | wc -l) -eq 0 ]; then
        scottyerror "Source dir $SYNC_SRC is empty. Not syncing!"
        return 1
    fi

    dir_current=$REMOTE_DIR/current
    dir_incomplete=$REMOTE_DIR/incomplete
    dir_timestamped=$REMOTE_DIR/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)

    if [ -z $SYNC_EXC ]; then
        rsync_exclude=""
    else
        rsync_exclude=$(eval echo --exclude={$SYNC_EXC} | tr -d {})
    fi

    if [ $FAKE_SUPER -eq 1 ]; then
        rsync_fake_super="--rsync-path=\"rsync --fake-super\""
    else
        rsync_fake_super=""
    fi

    if (ssh255 $REMOTE_HOST '[ ! -d '$REMOTE_DIR' ]'); then
        scottyinfo "Creating destination directory $REMOTE_HOST:$REMOTE_DIR."
        ssh255 $REMOTE_HOST "mkdir $REMOTE_DIR"
    fi

    if isIncomplete; then
        scottyerror "Continuing old incomplete backup."
    fi

    scottyinfo "Starting rsync."
    rsync -e "ssh -l $REMOTE_USER" --bwlimit=$BWLIMIT \
          -v -aHAX --numeric-ids --delete --delete-excluded \
          --link-dest=$dir_current \
          $rsync_exclude $rsync_fake_super \
          $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"
    else
        scottyerror "Rsync failed."
    fi

    while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
        oldestbackup=$(grepbackups | head -1)
        oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)

        if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
            scottyinfo "Removing old backup $oldestbackup."
            ssh255 $REMOTE_HOST rm -r "$REMOTE_DIR/$oldestbackup"
        else
            break
        fi
    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."

    cleanup ABORT

    if [ $(jobs -p) ]; then
        scottyerror "TERMinating remaining child processes."
        kill $(jobs -p)
    fi

    deleteLock

    exit
}

function cleanup {
    scottyinfo "No cleanup function was defined."
}

function prepare {
    scottyinfo "No prepare function was defined."
}

function cleanup_normal {
    cleanup
    deleteLock
}

function printhelp {
    cat <<EOF
Usage: $(basename $0) [OPTION]...

Recognized options:
  -q   Only output errors
  -n   Run only on nth day
  -f   Force backup
  -l   List existing backups
  -h   Print out this help
EOF
}

function exclusiveLock {
    if ! mkdir /var/lock/$(basename $0); then
        scottyerror "Another instance of $(basename $0) is still running!"
        exit 1
    else
        trap deleteLock EXIT
    fi
}

function backmeupscotty {
    while getopts "qn:flh" opt; do
        case $opt in
            q)
                exec > /dev/null
                ;;
            n)
                BACKUP_RUNEVERYNTHDAY=$OPTARG
                ;;
            f)
                BACKUP_FORCE=1
                ;;
            l)
                LIST_BACKUPS=1
                ;;
            h)
                printhelp
                exit 0
                ;;
        esac
    done

    ssh255 $REMOTE_HOST exit

    if [ $LIST_BACKUPS ]; then
        for backup in $(grepbackups); do
            echo $backup
        done
        exit 0
    fi

    exclusiveLock

    if [ $BACKUP_FORCE -eq 1 ]; then
        scottyinfo "Backup was enforced."
    elif latestTooOld; then
        scottyerror "The latest backup is too old."
    elif isNthDay; then
        scottyinfo "This is the nth day."
    else
        scottyinfo "No backup has to be done. Exiting."
        exit 0
    fi

    scottyinfo "Performing backup."

    trap cleanup_abort EXIT

    prepare
    scottysync

    trap cleanup_normal EXIT

    exit 0
}