netcup-setup/ansible/roles/backup/templates/backup.bash.j2
2025-12-26 19:59:39 +01:00

37 lines
1.2 KiB
Django/Jinja

#!/bin/bash
# Configuration
BACKUP_USER="{{ backup.user }}" # Remote SSH username
BACKUP_HOST="{{ backup.host }}" # Remote SSH server
BACKUP_PATH="{{ backup.path }}" # Remote backup folder
BORG_PASSPHRASE="{{ backup.passphrase }}" # Encryption password (in plain text)
BACKUP_NAME="backup-$(date +'%Y-%m-%d')" # Name of the backup archive
BACKUP_REPO="ssh://$BACKUP_USER@$BACKUP_HOST/$BACKUP_PATH" # Borg repository location
# Environment variable for Borg encryption
export BORG_PASSPHRASE
# Run Borg backup
echo "Starting Borg backup..."
borg create --verbose --filter AME --list --stats --compression lz4 \
$BACKUP_REPO::$BACKUP_NAME \
/etc \
/var/vol
# Capture Borg exit status
BORG_EXIT=$?
# Check if the backup succeeded or was partially successful (exit code 0 or 1)
if [ $BORG_EXIT -eq 0 ] || [ $BORG_EXIT -eq 1 ]; then
echo "Backup succeeded (with return code $BORG_EXIT)!"
else
echo "Backup failed (with return code $BORG_EXIT)!"
exit 1
fi
# Prune old backups (keep last 7 daily, 4 weekly, and 6 monthly backups)
borg prune --list $BACKUP_REPO --keep-daily=7 --keep-weekly=4 --keep-monthly=6
# Unset the encryption password for security
unset BORG_PASSPHRASE