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>
93 lines
2.7 KiB
Bash
Executable File
93 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# ==============================================================================
|
|
# MongoDB Online Dump Script (Standalone Docker)
|
|
# ==============================================================================
|
|
#
|
|
# PURPOSE:
|
|
# Creates an online backup of only the 'utility-bills' database using mongodump.
|
|
# The database server remains running during the backup process.
|
|
#
|
|
# WHEN TO USE:
|
|
# - For standalone Docker deployments (not Docker Swarm)
|
|
# - When you need a quick backup without downtime
|
|
# - When you only need the database content, not the full volume
|
|
#
|
|
# BACKUP TYPE:
|
|
# - Online (hot) backup - DB server keeps running
|
|
# - Database-level backup - only 'utility-bills' database
|
|
# - Creates compressed archive (.tar.gz) using mongodump
|
|
#
|
|
# OUTPUT:
|
|
# - Backup files stored in: ./mongo-backup/
|
|
# - Filename format: utility-bills-dump-YYYY-MM-DD_HH-MM.tar.gz
|
|
# - Log file: ./mongo-backup/db-dump-db-standalone.log
|
|
# - Automatic rotation: keeps newest 7 backups (configurable via KEEP env var)
|
|
#
|
|
# USAGE:
|
|
# ./db-dump--standalone.sh
|
|
# KEEP=10 ./db-dump--standalone.sh # Keep 10 backups instead of 7
|
|
#
|
|
# ==============================================================================
|
|
|
|
# Configuration
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
# create backup directory if it doesn't exist
|
|
BACKUP_DIR="$SCRIPT_DIR/mongo-backup"
|
|
mkdir -p "$BACKUP_DIR"
|
|
|
|
# initialize log file (overwrite if exists)
|
|
LOG_FILE="$BACKUP_DIR/db-dump-db-standalone.log"
|
|
> "$LOG_FILE"
|
|
|
|
# Function to log messages
|
|
log() {
|
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
|
echo "[$timestamp] $*" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
log "Starting database dump process..."
|
|
|
|
BACKUP_FILE_NAME_BASE="utility-bills-dump-"
|
|
BACKUP_FILE_NAME="${BACKUP_FILE_NAME_BASE}$(date +%F_%H-%M).tar.gz"
|
|
|
|
CONTAINER_NAME="evidencija-rezija__mongo"
|
|
DB_NAME="utility-bills"
|
|
|
|
docker exec "$CONTAINER_NAME" sh -c '
|
|
mongodump \
|
|
--username root \
|
|
--password HjktJCPWMBtM1ACrDaw7 \
|
|
--authenticationDatabase admin \
|
|
--dumpDbUsersAndRoles \
|
|
--db '$DB_NAME' \
|
|
--archive=/backup/'$BACKUP_FILE_NAME' \
|
|
--gzip
|
|
'
|
|
|
|
log "... DB dump created successfully."
|
|
|
|
# rotate old backups: keep only the newest $KEEP files
|
|
KEEP="${KEEP:-7}"
|
|
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"/${BACKUP_FILE_NAME_BASE}*.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 "Database backup process completed successfully."
|