#!/bin/bash

##########################################
#  Tokens                                #
##########################################

set -o pipefail
export XDG_RUNTIME_DIR=/run/user/1000
export XDG_CONFIG_HOME=/home/server/.config
source $HOME/.secrets/Backup/env.sh

##########################################
# Create sqlite3 backup for Vaultwarden  #
##########################################

rm /home/server/Containers/pw.thomasave.be/data/backup.sqlite3 /home/server/Containers/robbertave.com/data/backup.sqlite3
sqlite3 /home/server/Containers/pw.thomasave.be/data/db.sqlite3 "VACUUM INTO '/home/server/Containers/pw.thomasave.be/data/backup.sqlite3'"
sqlite3 /home/server/Containers/robbertave.com/data/database.sqlite "VACUUM INTO '/home/server/Containers/robbertave.com/data/backup.sqlite3'"

##########################################
# Unlock the Vault                       #
##########################################

echo "$ZFS_PASSPHRASE" | ssh vault /home/server/Storage/Thomas/Scripts/Mount/borg_unlock.sh

##########################################
# Run the backups                        #
##########################################

echo -e '\n\n\n'
echo "Running secrets Backups"
borg create -s --progress \
    /home/server/Backups/Secrets::'{hostname}-{now}' \
    /home/server/.secrets

echo -e '\n\n\n'
echo "Running dotfiles Backups"
borg create -s --progress \
    10.4.0.1:Storage/Thomas/Borg/Dotfiles::'{hostname}-{now}' \
    /home/server/.dotfiles\

echo -e '\n\n\n'
echo "Running Calendar Backups"
borg create -s --progress \
    10.4.0.1:Storage/Thomas/Borg/Calendar::'{hostname}-{now}' \
    /home/server/Containers/cal.thomasave.be\

echo -e '\n\n\n'
echo "Running Caddy Backups"
borg create -s --progress \
    --exclude /home/server/Containers/Caddy/Data\
    10.4.0.1:Storage/Thomas/Borg/Containers/caddy::'{hostname}-{now}' \
    /home/server/Containers/Caddy\

echo -e '\n\n\n'
echo "Running git.thomasave.be Backups"
borg create -s --progress \
    --exclude /home/server/Containers/git.thomasave.be/gitea/gitea/sessions \
    --exclude /home/server/Containers/git.thomasave.be/runner-data/.cache \
    --exclude /home/server/Containers/git.thomasave.be/gitea/gitea/jwt \
    --exclude /home/server/Containers/git.thomasave.be/gitea/ssh \
    10.4.0.1:Storage/Thomas/Borg/Containers/git.thomasave.be::'{hostname}-{now}' \
    /home/server/Containers/git.thomasave.be

echo -e '\n\n\n'
echo "Running pw.thomasave.be Backups"
borg create -s --progress \
    --exclude /home/server/Containers/pw.thomasave.be/data/icon_cache \
    --exclude /home/server/Containers/pw.thomasave.be/data/sends \
    10.4.0.1:Storage/Thomas/Borg/Containers/pw.thomasave.be::'{hostname}-{now}' \
    /home/server/Containers/pw.thomasave.be

echo -e '\n\n\n'
echo "Running stats.thomasave.be Backups"
borg create -s --progress \
    --exclude /home/server/Containers/stats.thomasave.be/logs \
    10.4.0.1:Storage/Thomas/Borg/Containers/stats.thomasave.be::'{hostname}-{now}' \
    /home/server/Containers/stats.thomasave.be

echo -e '\n\n\n'
echo "Running robbertave.com Backups"
borg create -s --progress \
    10.4.0.1:Storage/Thomas/Borg/Containers/robbertave.com::'{hostname}-{now}' \
    /home/server/Containers/robbertave.com

echo -e '\n\n\n'
echo "Running DNS Backups"
borg create -s --progress \
    --exclude /home/server/Containers/adguard.thomasave.be/work \
    10.4.0.1:Storage/Thomas/Borg/Adguard::'{hostname}-{now}' \
    /home/server/Containers/adguard.thomasave.be

echo -e '\n\n\n'
echo "Running Workspace Backups"
borg create -s --progress \
    --exclude /home/server/Workspace/TrackBox/Resources \
    10.4.0.1:Storage/Thomas/Borg/Workspace::'{hostname}-{now}' \
    /home/server/Workspace/
borg prune --keep-daily 7 --keep-weekly 4 --keep-monthly -1 --save-space 10.4.0.1:Storage/Thomas/Borg/Workspace
borg compact 10.4.0.1:Storage/Thomas/Borg/Workspace

echo -e '\n\n\n'
echo "Running photos.thomasave.be Backups"
borg create -s --progress \
    10.4.0.1:Storage/Thomas/Borg/Containers/photos.thomasave.be::'{hostname}-{now}' \
    /home/server/Containers/photos.thomasave.be
borg prune --keep-daily 7 --keep-weekly 4 --keep-monthly -1 --save-space 10.4.0.1:Storage/Thomas/Borg/Containers/photos.thomasave.be
borg compact 10.4.0.1:Storage/Thomas/Borg/Containers/photos.thomasave.be


echo -e '\n\n\n'
echo "Running Authentik Backups"
borg create -s --progress \
    10.4.0.1:Storage/Thomas/Borg/Containers/auth.thomasave.be::'{now:%Y-%m-%d}' \
    /home/server/Containers/auth.thomasave.be


echo -e '\n\n\n'
echo "Running Yolande Backups"
borg create -s --progress \
    10.4.0.1:Storage/Thomas/Borg/Yolande::'{hostname}-{now}' \
    /home/server/Storage/Yolande
borg prune --keep-daily 7 --keep-weekly 4 --keep-monthly -1 --save-space 10.4.0.1:Storage/Thomas/Borg/Yolande
borg compact 10.4.0.1:Storage/Thomas/Borg/Yolande


echo -e '\n\n\n'
echo "Running Thomas Backups"
borg create -s --progress \
    10.4.0.1:Storage/Thomas/Borg/Thomas::'{hostname}-{now}' \
    /home/server/Storage/Thomas/Documents \
    /home/server/Storage/Thomas/Pictures \
    /home/server/Storage/Thomas/Videos \
    /home/server/Storage/Thomas/Crypt
borg prune --keep-daily 7 --keep-weekly 4 --keep-monthly -1 --save-space 10.4.0.1:Storage/Thomas/Borg/Thomas
borg compact 10.4.0.1:Storage/Thomas/Borg/Thomas

##########################################
#  Perpare Cloud Backup                  #
##########################################

echo -e '\n\n\n'
echo "Peparing cloud backup"

borg create -s --progress \
    --exclude /home/server/Containers/pw.thomasave.be/data/icon_cache \
    --exclude /home/server/Containers/pw.thomasave.be/data/sends \
    --exclude /home/server/Containers/git.thomasave.be/gitea/gitea/sessions \
    --exclude /home/server/Containers/git.thomasave.be/gitea/ssh \
    --exclude /home/server/Containers/git.thomasave.be/gitea/gitea/jwt \
    --exclude /home/server/Containers/Caddy/Data\
    10.4.0.1:Storage/Thomas/Borg/Cloud::'{hostname}-{now}' \
    /home/server/Containers/pw.thomasave.be\
    /home/server/Containers/robbertave.com\
    /home/server/Containers/cal.thomasave.be\
    /home/server/Containers/auth.thomasave.be\
    /home/server/Containers/git.thomasave.be\
    /home/server/Containers/Boaty \
    /home/server/Containers/Caddy \

borg prune -d 365 --save-space 10.4.0.1:Storage/Thomas/Borg/Cloud
borg compact 10.4.0.1:Storage/Thomas/Borg/Cloud
# ssh vault /home/server/Storage/Thomas/Borg/Sync.sh

##########################################
# Lock the Vault                         #
##########################################

ssh vault /home/server/Storage/Thomas/Scripts/Mount/borg_lock.sh

##########################################
#  Backups to Oracle Cloud               #
##########################################

echo "Running Oracle Cloud Backup, containing Vaultwarden, robbertave.com, git.thomasave.be, Boaty, Stats, and Caddy."

borg create -s --progress \
    --exclude /home/server/Containers/pw.thomasave.be/data/icon_cache \
    --exclude /home/server/Containers/pw.thomasave.be/data/sends \
    --exclude /home/server/Containers/git.thomasave.be/gitea/gitea/sessions \
    --exclude /home/server/Containers/git.thomasave.be/gitea/ssh \
    --exclude /home/server/Containers/git.thomasave.be/gitea/gitea/jwt \
    --exclude /home/server/Containers/stats.thomasave.be/logs \
    --exclude /home/server/Containers/Caddy/Data\
    ubuntu@10.0.0.6:~/Backups/Borg::'{hostname}-{now}' \
    /home/server/Containers/pw.thomasave.be\
    /home/server/Containers/robbertave.com\
    /home/server/Containers/git.thomasave.be\
    /home/server/Containers/auth.thomasave.be\
    /home/server/Containers/Boaty \
    /home/server/Containers/Caddy \
    /home/server/Containers/cal.thomasave.be\
    /home/server/Containers/stats.thomasave.be \
