#!/usr/bin/env bash

##################################################################
# does backups (duh.) with rsync, either local or remote via ssh  
# sources the file given as first parameter and acts accordingly
# example file:
#
# precommand="echo Yippee ki-yay"
# postcommand="echo done"
# remote=true
# sshUserHost="foo@bar.org"
# 
# files="
# /etc
# /some/other/folder
# /some/file.sh
# "
#
##################################################################


if [ -z $1 ]; then
    echo "usage: dobackup FILE"
    echo "need file with backup instructions"
    exit 1
fi

name=`basename $1`
source $1

timestamp=`date +"%Y%m%d-%H%M"`
dest="$HOME/backups/$name-$timestamp"

if [ "$remote" = true ]; then
    if [ -z "$sshUserHost" ]; then
        echo "ssh data not complete, needs sshUserHost"
        exit 1
    else
        command="rsync -avzR -e ssh $sshUserHost:"
    fi
else
    command="rsync -avR "
fi

echo "$precommand"
eval ${precommand}

for file in $files; do
    fullcommand="$command$file $dest"
    echo "$fullcommand"
    if [ "$remote" = true ]; then
        $fullcommand
    else
	$fullcommand || exit 1
    fi
done

echo "$postcommand"
eval ${postcommand}