Files
evidencija-rezija/db-backup--standalone.sh
Nikola Derežić 4454ea3b7e Enhance backup system with comprehensive scripts and documentation
Database Backup Scripts:
- Add db-dump--standalone.sh for online database dumps (no downtime)
- Add db-restore-from-dump--standalone.sh for restoring from dumps
- Rename backup scripts with double-dash convention for clarity
- Add comprehensive header comments to all backup/restore scripts
- Document online vs offline, standalone vs swarm deployment types
- Add KEEP variable default (7) to dump script for rotation

Docker Configuration:
- Add mongo-backup volume mount to docker-compose-standalone.yaml
- Add mongo-backup volume mount to docker-compose-debug.yml
- Add container names to debug compose for consistency with standalone

Documentation:
- Add comprehensive Database Backup & Restore section to README.md
- Document all backup scripts with usage examples
- Document automated cron schedule (04:00 daily)
- Sunday: Full volume backup (KEEP=2, offline)
- Monday-Saturday: Database dumps (KEEP=7, online)
- Document backup directories and log file locations

Git Configuration:
- Add mongo-backup/ to .gitignore
- Fix missing newline at end of .gitignore

File Cleanup:
- Remove db-scheduled-backup.sh (superseded by cron jobs)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 11:11:17 +01:00

97 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# ==============================================================================
# MongoDB Offline Volume Backup Script (Standalone Docker)
# ==============================================================================
#
# PURPOSE:
# Creates an offline backup of the complete mongo-volume directory.
# The database server is stopped during the backup to ensure consistency.
#
# WHEN TO USE:
# - For standalone Docker deployments (not Docker Swarm)
# - When you need a complete volume backup (entire MongoDB data directory)
# - For disaster recovery scenarios
# - When consistency is critical and downtime is acceptable
#
# BACKUP TYPE:
# - Offline (cold) backup - DB server is stopped during backup
# - Volume-level backup - entire mongo-volume directory
# - Creates compressed tarball (.tar.gz) of the volume
#
# OUTPUT:
# - Backup files stored in: ./backups/
# - Filename format: mongo-volume-backup-YYYY-MM-DD-HH-MM.tar.gz
# - Log file: ./backups/db-backup-standalone.log
# - Automatic rotation: keeps newest 7 backups (configurable via KEEP env var)
#
# USAGE:
# ./db-backup--standalone.sh
# KEEP=10 ./db-backup--standalone.sh # Keep 10 backups instead of 7
#
# NOTE:
# The MongoDB container will be stopped and restarted automatically.
# Expect brief downtime during the backup process.
#
# ==============================================================================
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_FILE="$SCRIPT_DIR/backups/db-backup-standalone.log"
MONGO_SERVICE="mongo"
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:-$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
log "Removing old backup: $f"
rm -f -- "$f"
done
fi
log "Rotation completed."
else
log "Backup rotation disabled (KEEP=$KEEP)."
fi
log "Starting MongoDB container..."
docker compose -f "$COMPOSE_FILE" start "$MONGO_SERVICE"
log "Database backup process completed successfully."