#!/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 --compression lzma,9 ssh://wernigerode:23/./backups/sputnik::'sputnik-2017-12-08T20:29:40.checkpoint Fri, 2017-12-08 20:29:45' $@
#/usr/bin/borg create --compression lzma,9 ssh://wernigerode:23/./backups/sputnik::'sputnik-2017-12-08T20:29:40.checkpoint Fri, 2017-12-08 20:29:45.checkpoint Sat, 2017-12-09 07:23:28' $@
/usr/bin/borg create --verbose --compression lzma,9 ssh://wernigerode:23/./backups/sputnik::'sputnik-2017-12-08T20:29:40.checkpoint Fri, 2017-12-08 20:29:45.checkpoint Sat, 2017-12-09 07:23:28.checkpoint Sun, 2017-12-10 07:32:31' $@

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}
