123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- #!/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}" $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
- sudo -E borg prune --glob-archives '{hostname}-*' --keep-daily=7 --keep-weekly=4 --keep-monthly=6 --keep-yearly=10 "$SERVER_USER@$SERVER_IP:$REPOSITORY"
- if [ $? -ne 0 ]; then
- echo "Borg prune failed."
- exit 1
- fi
- sudo -E borg compact "$SERVER_USER@$SERVER_IP:$REPOSITORY"
- if [ $? -ne 0 ]; then
- echo "Borg compact failed."
- exit 1
- fi
|