The -n flag was causing curl to rely on .netrc, which conflicted with explicit -u username:password usage and led to empty responses from the backup API. Removing it ensures proper authentication.
96 lines
2.8 KiB
Bash
Executable File
96 lines
2.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Load config from .env
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
if [[ -f "$SCRIPT_DIR/.env" ]]; then
|
|
source "$SCRIPT_DIR/.env"
|
|
else
|
|
echo "[$(date)] Missing .env file in $SCRIPT_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
# Ensure backup directory exists
|
|
mkdir -p "$BACKUP_DIR"
|
|
|
|
# Generate timestamped filenames
|
|
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
|
|
OUTPUT_FILE="$BACKUP_DIR/backup_$TIMESTAMP.zip"
|
|
LOG_FILE="$BACKUP_DIR/backup_$TIMESTAMP.log"
|
|
|
|
# Logging helper
|
|
log() {
|
|
echo "[$(date)] $1" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Uptime Kuma check-in (GET Request with only "status=up" and "msg=OK")
|
|
uptime_kuma_checkin() {
|
|
if [[ -n "$UPTIME_KUMA_URL" && -n "$UPTIME_KUMA_API_KEY" ]]; then
|
|
# Send GET request directly to Uptime Kuma with status=up and msg=OK
|
|
curl -s "${UPTIME_KUMA_URL}/api/push/${UPTIME_KUMA_API_KEY}?status=up&msg=OK" > /dev/null
|
|
log "✅ Uptime Kuma check-in sent."
|
|
fi
|
|
}
|
|
|
|
# Gotify Error Notification
|
|
gotify_error_notify() {
|
|
local summary="Errors detected in recent Lubelogger backup logs on $(hostname):\n$1"
|
|
|
|
if [[ -n "$GOTIFY_PUSH_URL" && -n "$GOTIFY_PUSH_KEY" ]]; then
|
|
# Send the Gotify notification with form data
|
|
curl -s -X POST "${GOTIFY_PUSH_URL}/message?token=${GOTIFY_PUSH_KEY}" \
|
|
-F "title=[${BACKUP_TAG}] Backup Errors on $(hostname)" \
|
|
-F "message=$(echo -e "$summary" | head -c 1000)" \
|
|
-F "priority=5" > /dev/null
|
|
log "⚠️ Gotify error notification sent."
|
|
fi
|
|
}
|
|
|
|
|
|
# Start backup
|
|
log "🔄 Starting Lubelogger backup job"
|
|
|
|
# Uptime Kuma Check-in: Inform that the backup process has started
|
|
uptime_kuma_checkin
|
|
|
|
response=$(curl -s -u "$USERNAME:$PASSWORD" --location "$BASE_URL/api/makebackup")
|
|
|
|
if [[ -z "$response" ]]; then
|
|
log "❌ Error: Empty response from backup API"
|
|
gotify_error_notify "Empty response from backup API."
|
|
exit 1
|
|
fi
|
|
|
|
backup_url="${BASE_URL}${response//\"/}"
|
|
|
|
log "⬇️ Downloading backup from $backup_url"
|
|
curl -s -u "$USERNAME:$PASSWORD" --location "$backup_url" -o "$OUTPUT_FILE"
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
log "✅ Backup saved to $OUTPUT_FILE"
|
|
else
|
|
log "❌ Error: Failed to download backup"
|
|
gotify_error_notify "Failed to download backup."
|
|
exit 1
|
|
fi
|
|
|
|
# Cleanup old backups and logs
|
|
log "🧹 Cleaning up old backups/logs older than $RETENTION_DAYS days"
|
|
find "$BACKUP_DIR" \( -name "backup_*.zip" -o -name "backup_*.log" \) -type f -mtime +$RETENTION_DAYS -exec rm {} \;
|
|
|
|
# Error summary scanning
|
|
log "🔍 Scanning recent logs for errors"
|
|
ERRORS=""
|
|
while IFS= read -r logfile; do
|
|
LOG_ERRORS=$(grep "Error" "$logfile")
|
|
if [[ -n "$LOG_ERRORS" ]]; then
|
|
ERRORS+="\n$(basename "$logfile"):\n$LOG_ERRORS\n"
|
|
fi
|
|
done < <(find "$BACKUP_DIR" -name "backup_*.log" -type f -mtime -$RETENTION_DAYS)
|
|
|
|
if [[ -n "$ERRORS" ]]; then
|
|
gotify_error_notify "$ERRORS"
|
|
fi
|
|
|
|
log "✅ Backup job finished"
|
|
|