#!/bin/bash
# ssh -i ~/.ssh/borg-backup e7e9h45y@e7e9h45y.repo.borgbase.com
set -e

export BORG_REPO="ssh://e7e9h45y@e7e9h45y.repo.borgbase.com/./repo"
export BORG_RSH="ssh -i ~/.ssh/borg-backup"
export BORG_PASSPHRASE="Airbus12"

HOSTNAME=$(hostname)
DATE=$(date +%Y-%m-%d_%H-%M-%S)
ARCHIVE_NAME="${DATE}-${HOSTNAME}"

# Resolve symlinks externally and feed real paths to borg via --paths-from-stdin.
# - First find: locates all current* symlinks under /BACKUP and resolves them with realpath.
# - Second find: locates all regular current* files under /BACKUP (non-symlinks).
# - sort -u: deduplicates in case a resolved symlink target overlaps with a regular file.
{
    find /BACKUP -name 'current*' -type l -print0 | xargs -0 -I {} realpath {}
    find /BACKUP -name 'current*' ! -type l
} | sort -u | borg create \
    --verbose \
    --stats \
    --compression lz4 \
    --paths-from-stdin \
    ::$ARCHIVE_NAME

echo "Backup completed: $ARCHIVE_NAME"
