#!/bin/sh
if [ $# -lt 1 ] ; then
  echo "No dir for backup given"
  exit
fi
DATE=`date +%Y%m%d`

# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM

info "Starting backup"
set -x

export BORG_PASSPHRASE="YYlaptopYYstadt"
/usr/bin/borg create --verbose --compression lzma,9 ssh://wernigerode:23/./backups/sputnik::'sputnik-{now}' $@

backup_exit=$?

prune_exit=0

# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))

if [ ${global_exit} -eq 1 ];
then
    info "Backup and/or Prune finished with a warning"
fi

if [ ${global_exit} -gt 1 ];
then
    info "Backup and/or Prune finished with an error"
fi

exit ${global_exit}
