#!/usr/bin/env bash

HOST=$(hostname)
DIR=/mnt/sdb/backups/$HOST
SERVER=root@neon
ROOT=/home
REPOSITORY=$SERVER:$DIR

if ! ssh $SERVER exit
then
    echo "Can't connect to server"
    exit
fi

if [[ $1 == "list" ]]
then
    ssh $SERVER BORG_PASSPHRASE=$BORG_PASSPHRASE borg list $DIR
    exit
fi

if ! ssh $SERVER BORG_PASSPHRASE=$BORG_PASSPHRASE borg list $DIR
then
    ssh $SERVER mkdir -p $DIR 
    borg init --encryption=repokey-blake2 $REPOSITORY
fi

borg create -v --stats --progress                   \
    --compression lz4                               \
    $REPOSITORY::'{now:%Y-%m-%dT%H:%M:%S}' $ROOT    \
    --exclude '*.iso'                               \
    --exclude '*.mkv'                               \
    --exclude '/home/user/downloads/torrents/*'

borg prune -v $REPOSITORY                           \
    --keep-daily=7 --keep-weekly=4 --keep-monthly=6
