From dc5698d7d626456edc68fba93dc0de97d12ce35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Wed, 26 Nov 2025 09:42:20 +0100 Subject: [PATCH] Improve backup scripts with logging and remove duplicate rotation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit db-backup-standalone.sh: - Add logging with timestamps to backups/db-backup-standalone.log - Add SCRIPT_DIR to make all paths relative to script location - Add detailed logging for each backup step (stop, backup, rotate, start) - Log rotation status and which old backups are removed db-scheduled-backup.sh: - Remove duplicate backup rotation logic (now handled in standalone script) - Comment out cleanup code to avoid redundant operations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- db-backup-standalone.sh | 32 ++++++++++++++++++++++++++++---- db-scheduled-backup.sh | 19 ++++++++++--------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/db-backup-standalone.sh b/db-backup-standalone.sh index c945def..eb08f1b 100755 --- a/db-backup-standalone.sh +++ b/db-backup-standalone.sh @@ -2,36 +2,60 @@ set -euo pipefail # Configuration +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +LOG_FILE="$SCRIPT_DIR/backups/db-backup-standalone.log" MONGO_SERVICE="mongo" -COMPOSE_FILE="docker-compose-standalone.yaml" +COMPOSE_FILE="$SCRIPT_DIR/docker-compose-standalone.yaml" + +# Initialize log file (overwrite if exists) +mkdir -p "$(dirname "$LOG_FILE")" +> "$LOG_FILE" + +# Function to log messages +log() { + local timestamp=$(date '+%Y-%m-%d %H:%M:%S') + echo "[$timestamp] $*" | tee -a "$LOG_FILE" +} # stop mongo container while we copy its volume +log "Stopping MongoDB container..." docker compose -f "$COMPOSE_FILE" stop "$MONGO_SERVICE" # timestamp for filename TIMESTAMP=$(date +"%Y-%m-%d-%H-%M") # backup directory and retention (can be overridden via env) -BACKUP_DIR="${BACKUP_DIR:-backups}" +BACKUP_DIR="${BACKUP_DIR:-$SCRIPT_DIR/backups}" KEEP="${KEEP:-7}" mkdir -p "$BACKUP_DIR" BACKUP_FILE="$BACKUP_DIR/mongo-volume-backup-$TIMESTAMP.tar.gz" +log "Creating backup: $BACKUP_FILE" sudo tar -czvpf "$BACKUP_FILE" mongo-volume +log "Backup created successfully." + # rotate old backups: keep only the newest $KEEP files if [ "$KEEP" -gt 0 ]; then + log "Rotating backups, keeping the newest $KEEP files." + # gather files sorted newest-first mapfile -t files < <(ls -1t "$BACKUP_DIR"/mongo-volume-backup-*.tar.gz 2>/dev/null || true) if [ "${#files[@]}" -gt "$KEEP" ]; then for f in "${files[@]:$KEEP}"; do - echo "Removing old backup: $f" + log "Removing old backup: $f" rm -f -- "$f" done fi + + log "Rotation completed." +else + log "Backup rotation disabled (KEEP=$KEEP)." fi -# bring mongo container back up +log "Starting MongoDB container..." docker compose -f "$COMPOSE_FILE" start "$MONGO_SERVICE" + +log "Database backup process completed successfully." diff --git a/db-scheduled-backup.sh b/db-scheduled-backup.sh index 52d3296..e8f89f3 100755 --- a/db-scheduled-backup.sh +++ b/db-scheduled-backup.sh @@ -33,14 +33,15 @@ fi log "Starting database backup..." "$BACKUP_SCRIPT" -# Delete backups older than 7 days -BACKUP_DIR="${BACKUP_DIR:-backups}" -if [ -d "$BACKUP_DIR" ]; then - log "Cleaning up backups older than 7 days in $BACKUP_DIR..." - find "$BACKUP_DIR" -name "mongo-volume-backup-*.tar.gz" -type f -mtime +7 -delete - log "Cleanup completed." -else - log "Warning: Backup directory $BACKUP_DIR not found, skipping cleanup." -fi +# Rotation is already handled in db-backup-standalone.sh, so no need to repeat it here. +# # Delete backups older than 7 days +# BACKUP_DIR="${BACKUP_DIR:-backups}" +# if [ -d "$BACKUP_DIR" ]; then +# log "Cleaning up backups older than 7 days in $BACKUP_DIR..." +# find "$BACKUP_DIR" -name "mongo-volume-backup-*.tar.gz" -type f -mtime +7 -delete +# log "Cleanup completed." +# else +# log "Warning: Backup directory $BACKUP_DIR not found, skipping cleanup." +# fi log "Scheduled backup completed successfully."