borg_create.sh 910 B

12345678910111213141516171819202122232425262728293031
  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