diff --git a/borg_backup/borg_docker_backup.sh b/borg_backup/borg_docker_backup.sh new file mode 100644 index 0000000..4fcfaef --- /dev/null +++ b/borg_backup/borg_docker_backup.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +set -e + +# Configurable variables +REPO="borgbackup@192.168.1.184:/mnt/fileserver/borg" +NOTIFY_URL="https://ntfy.treehousefullofstars.com/backups" +SERVICES_FILE="/home/jadowyne/services.txt" + +# Helper function to notify +ntfy_notify() { + curl -H "Title: Borg Backup" -d "$2: $1" "$NOTIFY_URL" +} + +while IFS=';' read -r SERVICE SRVPATH; do + # Skip blank or comment lines + [[ "$SERVICE" =~ ^#.*$ || -z "$SERVICE" ]] && continue + + echo "=== Backing up $SERVICE ===" + PARENT_DIR="$(dirname "$SRVPATH")" + FOLDER_NAME="$(basename "$SRVPATH")" + SECONDS=0 + + cd "$SRVPATH" || { + ntfy_notify "$SERVICE: Cannot cd to $SRVPATH" "Error" + continue + } + + # Shutdown service + docker compose down + sleep 5 + + if docker compose ps | grep -q 'Up'; then + ntfy_notify "$SERVICE: Failed to shut down containers" "Error" + continue + fi + + cd "$PARENT_DIR" || { echo "Could not cd to $PARENT_DIR"; continue; } + + # Run Borg backup + ARCHIVE="$SERVICE-$(date +%Y-%m-%d_%H-%M-%S)" + BORG_PASSPHRASE='Jumbocarrot&001' borg create --compression zstd,6 "$REPO::$ARCHIVE" "$FOLDER_NAME" + + BORG_PASSPHRASE='Jumbocarrot&001' borg prune --prefix "${SERVICE}-" --keep-last=4 "$REPO" + + if [ $? -eq 0 ]; then + cd "$SRVPATH" || { + ntfy_notify "$SERVICE: Cannot cd to $SRVPATH" "Error" + continue + } + # Start service back up + docker compose up -d + + ELAPSED="$SECONDS" + MIN=$((ELAPSED/60)) + SEC=$((ELAPSED%60)) + + if docker compose ps | grep -q 'Up'; then + ntfy_notify "$SERVICE backed up and restarted successfully in ${MIN}m ${SEC}s" "Success" + else + ntfy_notify "$SERVICE backup successful, but failed to restart service in ${MIN}m ${SEC}s" "Error" + fi + else + ntfy_notify "$SERVICE backup FAILED (see logs)" "Error" + # Consider starting the service anyway + docker compose up -d + fi + +done < "$SERVICES_FILE" \ No newline at end of file