feat: add Docker backup script with container lifecycle, compression, and notifications.
This commit is contained in:
parent
f26d76a1be
commit
c82e74b72b
117
docker_backup.sh
Normal file
117
docker_backup.sh
Normal file
@ -0,0 +1,117 @@
|
||||
#!/bin/bash
|
||||
|
||||
# === Load configuration 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"
|
||||
|
||||
# === Timestamp & File Setup ===
|
||||
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
|
||||
OUTPUT_FILE="$BACKUP_DIR/docker_backup_$TIMESTAMP.tar.gz"
|
||||
LOG_FILE="$BACKUP_DIR/docker_backup_$TIMESTAMP.log"
|
||||
|
||||
# === Logging helper ===
|
||||
log() {
|
||||
echo "[$(date)] $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# === Uptime Kuma Check-in ===
|
||||
uptime_kuma_checkin() {
|
||||
if [[ -n "$UPTIME_KUMA_URL" && -n "$UPTIME_KUMA_API_KEY" ]]; then
|
||||
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="Docker backup errors on $(hostname):\n$1"
|
||||
if [[ -n "$GOTIFY_PUSH_URL" && -n "$GOTIFY_PUSH_KEY" ]]; then
|
||||
curl -s -X POST "${GOTIFY_PUSH_URL}/message?token=${GOTIFY_PUSH_KEY}" \
|
||||
-F "title=[${BACKUP_TAG}] Docker Backup Error on $(hostname)" \
|
||||
-F "message=$(echo -e "$summary" | head -c 1000)" \
|
||||
-F "priority=5" > /dev/null
|
||||
log "⚠️ Gotify error notification sent."
|
||||
fi
|
||||
}
|
||||
|
||||
# === Stop Containers ===
|
||||
stop_containers() {
|
||||
log "🛑 Stopping Docker containers"
|
||||
for container in ${DOCKER_CONTAINERS[@]}; do
|
||||
docker stop "$container" >> "$LOG_FILE" 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
log "✅ Stopped container: $container"
|
||||
else
|
||||
log "❌ Failed to stop container: $container"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# === Start Containers ===
|
||||
start_containers() {
|
||||
log "🚀 Starting Docker containers"
|
||||
for container in ${DOCKER_CONTAINERS[@]}; do
|
||||
docker start "$container" >> "$LOG_FILE" 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
log "✅ Started container: $container"
|
||||
else
|
||||
log "❌ Failed to start container: $container"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# === Backup Directories ===
|
||||
backup_directories() {
|
||||
log "📦 Backing up directories"
|
||||
tar -czf "$OUTPUT_FILE" ${DIRECTORIES_TO_BACKUP[@]} >> "$LOG_FILE" 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
log "✅ Backup created at $OUTPUT_FILE"
|
||||
else
|
||||
log "❌ Failed to create backup archive"
|
||||
gotify_error_notify "Backup failed while archiving directories."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# === Cleanup Old Backups and Logs ===
|
||||
cleanup_old_files() {
|
||||
log "🧹 Cleaning up files older than $RETENTION_DAYS days"
|
||||
find "$BACKUP_DIR" \( -name "docker_backup_*.tar.gz" -o -name "docker_backup_*.log" \) -type f -mtime +$RETENTION_DAYS -exec rm -f {} \;
|
||||
}
|
||||
|
||||
# === Error Scanning ===
|
||||
scan_logs_for_errors() {
|
||||
log "🔍 Scanning logs for errors"
|
||||
ERRORS=""
|
||||
while IFS= read -r logfile; do
|
||||
LOG_ERRORS=$(grep -Ei "error|failed|❌" "$logfile")
|
||||
if [[ -n "$LOG_ERRORS" ]]; then
|
||||
ERRORS+="\n$(basename "$logfile"):\n$LOG_ERRORS\n"
|
||||
fi
|
||||
done < <(find "$BACKUP_DIR" -name "docker_backup_*.log" -type f -mtime -$RETENTION_DAYS)
|
||||
|
||||
if [[ -n "$ERRORS" ]]; then
|
||||
gotify_error_notify "$ERRORS"
|
||||
fi
|
||||
}
|
||||
|
||||
# === Run the Backup Process ===
|
||||
log "🔄 Starting Docker backup job"
|
||||
uptime_kuma_checkin
|
||||
|
||||
stop_containers
|
||||
backup_directories
|
||||
start_containers
|
||||
|
||||
cleanup_old_files
|
||||
scan_logs_for_errors
|
||||
|
||||
log "✅ Docker backup job completed"
|
||||
Loading…
Reference in New Issue
Block a user