#!/bin/bash
#set -x
####### Konfigurationsparameter #############
BACKUPCONFDIR=/root/TitaniaBackup
RESTIC=/usr/bin/restic
RESTIC_REPO=rest:https://backer415.botter.cc:8444
MAILFROMADDRESS=root@titania.botter.cc
MAILTOADDRESS=root@botter.cc
REPOPASSWORD=$BACKUPCONFDIR/repopassword
#############################################
#  Aufruf:
#  restic-backup-create-repo backuphostsdatei host
#
#  backuphostsdatei:
#
#  # Hostname
#  Hostname[:port] (PreCommand|-) KeepHourly KeepDaily KeepWeekly KeepMonthly KeepYearly [rdiff-backup_Parameter ...]
#
#############################################
# source RESTIC_PASSWORD from config file
. $REPOPASSWORD

if [ $1"_" != "_" ]; then
  if [ -f $BACKUPCONFDIR/$1 ]; then
    BACKUPHOSTS=$1
  else
    echo "Backuphosts Datei existiert nicht!"
    exit 2
  fi 
fi 
if [ $2"_" != "_" ]; then
  REPOHOST="$(egrep -v "^$|^#" $BACKUPCONFDIR/$BACKUPHOSTS | cut -f1 -d" " | egrep $2)"
  if [ $REPOHOST"_" = "_" ]; then
    echo "Host nicht in Backuphosts Datei!"
    exit 1
  fi
else
    echo "Aufruf: $0 backuphostsdatei host"
    exit 3
fi 

echo $REPOHOST
$RESTIC -r $RESTIC_REPO/$REPOHOST stats -q > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL = 0 ]; then
  echo ResticRepo $RESTIC_REPO/$REPOHOST existiert bereits
  exit 8;
fi

$RESTIC -r $RESTIC_REPO/$REPOHOST init
