run_backup.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. # Load environment variables from .env file
  3. source .env
  4. # Export Borg passphrase
  5. export BORG_PASSPHRASE=$PASSPHRASE
  6. export BORG_RSH="ssh -i $SSH_KEY -p $SSH_PORT"
  7. # Build the list of existing directories
  8. EXISTING_DIRECTORIES=$(for DIR in $SOURCE_DIRECTORIES; do
  9. if [ -d "$DIR" ]; then
  10. echo "$DIR"
  11. else
  12. echo "Directory does not exist: $DIR"
  13. fi
  14. done)
  15. # If we have valid directories, run the backup
  16. if [ -n "$EXISTING_DIRECTORIES" ]; then
  17. echo "Backing up directories: $EXISTING_DIRECTORIES"
  18. touch exclude.txt
  19. sudo -E borg create --verbose --filter AME --list --stats --compression zstd,10 --exclude-caches --exclude-from exclude.txt "$SERVER_USER@$SERVER_IP:$REPOSITORY::{hostname}-{now}" $EXISTING_DIRECTORIES
  20. if [ $? -ne 0 ]; then
  21. echo "Borg backup failed."
  22. exit 1
  23. fi
  24. else
  25. echo "No valid directories to back up."
  26. exit 1
  27. fi
  28. # Prune old backups using a custom SSH port
  29. sudo -E borg prune --glob-archives '{hostname}-*' --keep-daily=7 --keep-weekly=4 --keep-monthly=6 --keep-yearly=10 "$SERVER_USER@$SERVER_IP:$REPOSITORY"
  30. if [ $? -ne 0 ]; then
  31. echo "Borg prune failed."
  32. exit 1
  33. fi
  34. sudo -E borg compact "$SERVER_USER@$SERVER_IP:$REPOSITORY"
  35. if [ $? -ne 0 ]; then
  36. echo "Borg compact failed."
  37. exit 1
  38. fi