#!/bin/bash # Load environment variables from .env file source .env # Export Borg passphrase export BORG_PASSPHRASE=$PASSPHRASE export BORG_RSH="ssh -i $SSH_KEY -p $SSH_PORT" # Build the list of existing directories EXISTING_DIRECTORIES=$(for DIR in $SOURCE_DIRECTORIES; do if [ -d "$DIR" ]; then echo "$DIR" else echo "Directory does not exist: $DIR" fi done) # If we have valid directories, run the backup if [ -n "$EXISTING_DIRECTORIES" ]; then echo "Backing up directories: $EXISTING_DIRECTORIES" sudo -E borg create --stats "$SERVER_USER@$SERVER_IP:$REPOSITORY::{hostname}-{now:%Y-%m-%d}" $EXISTING_DIRECTORIES if [ $? -ne 0 ]; then echo "Borg backup failed." exit 1 fi else echo "No valid directories to back up." exit 1 fi # Prune old backups using a custom SSH port borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=6 "$SERVER_USER@$SERVER_IP:$REPOSITORY" if [ $? -ne 0 ]; then echo "Borg prune failed." exit 1 fi