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>
This commit is contained in:
150
db-restore-from-backup--swarm.sh
Executable file
150
db-restore-from-backup--swarm.sh
Executable file
@@ -0,0 +1,150 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# ==============================================================================
|
||||
# MongoDB Volume Restore Script (Docker Swarm)
|
||||
# ==============================================================================
|
||||
#
|
||||
# PURPOSE:
|
||||
# Restores the complete mongo-volume directory from a volume backup tarball.
|
||||
# The MongoDB service is scaled down during the restore to ensure consistency.
|
||||
#
|
||||
# WHEN TO USE:
|
||||
# - For Docker Swarm deployments (not standalone Docker)
|
||||
# - To restore from backups created by db-backup--swarm.sh
|
||||
# - For disaster recovery scenarios
|
||||
# - When you need to restore the complete MongoDB data directory
|
||||
#
|
||||
# RESTORE TYPE:
|
||||
# - Offline (cold) restore - DB service is scaled to 0 during restore
|
||||
# - Volume-level restore - entire mongo-volume directory
|
||||
# - Extracts from compressed tarball (.tar.gz)
|
||||
#
|
||||
# INPUT:
|
||||
# - Requires backup filename as parameter
|
||||
# - Looks for file in: ./backups/
|
||||
# - Optional: --pre-backup flag to create safety backup before restore
|
||||
#
|
||||
# USAGE:
|
||||
# ./db-restore-from-backup--swarm.sh <backup-filename>
|
||||
# ./db-restore-from-backup--swarm.sh mongo-volume-backup-2025-11-26-14-30.tar.gz
|
||||
# ./db-restore-from-backup--swarm.sh --pre-backup mongo-volume-backup-2025-11-26-14-30.tar.gz
|
||||
#
|
||||
# WARNING:
|
||||
# This will COMPLETELY REPLACE the mongo-volume directory contents!
|
||||
# Use --pre-backup flag to create a safety backup before restore.
|
||||
#
|
||||
# NOTE:
|
||||
# The MongoDB service will be scaled down to 0 and then back up to 1.
|
||||
# Expect downtime during the restore process.
|
||||
#
|
||||
# ==============================================================================
|
||||
|
||||
# Configuration
|
||||
MONGO_SERVICE="utility-bills-tracker_mongo"
|
||||
|
||||
# Parse command line options
|
||||
DO_PRE_BACKUP=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--do-pre-backup=*)
|
||||
DO_PRE_BACKUP="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
TIMESTAMP="$1"
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Usage: ./restore.sh [options] <timestamp>
|
||||
# Example: ./restore.sh 2025-08-24-14-30
|
||||
# Options: --do-pre-backup=true/false (skip interactive prompt)
|
||||
|
||||
if [ -z "${TIMESTAMP:-}" ]; then
|
||||
echo "Usage: $0 [options] <timestamp>"
|
||||
echo "Example: $0 2025-08-24-14-30"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " --do-pre-backup=true Skip interactive prompt, create pre-restore backup"
|
||||
echo " --do-pre-backup=false Skip interactive prompt, no pre-restore backup"
|
||||
echo ""
|
||||
echo "Available backups:"
|
||||
ls -1t backups/mongo-volume-backup-*.tar.gz 2>/dev/null | sed 's/.*mongo-volume-backup-\(.*\)\.tar\.gz/ \1/' || echo " No backups found"
|
||||
exit 1
|
||||
fi
|
||||
BACKUP_DIR="${BACKUP_DIR:-backups}"
|
||||
BACKUP_FILE="$BACKUP_DIR/mongo-volume-backup-$TIMESTAMP.tar.gz"
|
||||
|
||||
# Check if backup file exists
|
||||
if [ ! -f "$BACKUP_FILE" ]; then
|
||||
echo "Error: Backup file '$BACKUP_FILE' not found"
|
||||
echo ""
|
||||
echo "Available backups:"
|
||||
ls -1t "$BACKUP_DIR"/mongo-volume-backup-*.tar.gz 2>/dev/null | sed 's/.*mongo-volume-backup-\(.*\)\.tar\.gz/ \1/' || echo " No backups found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Restoring from backup: $BACKUP_FILE"
|
||||
echo "WARNING: This will replace the current mongo-volume data!"
|
||||
read -p "Are you sure you want to continue? (y/N): " -n 1 -r
|
||||
echo
|
||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo "Restore cancelled"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Scale down mongo service
|
||||
echo "Scaling down mongo service..."
|
||||
docker service scale "$MONGO_SERVICE"=0
|
||||
|
||||
# Handle pre-restore backup
|
||||
CREATE_PRE_BACKUP=true
|
||||
if [ "$DO_PRE_BACKUP" = "false" ]; then
|
||||
CREATE_PRE_BACKUP=false
|
||||
elif [ "$DO_PRE_BACKUP" = "true" ]; then
|
||||
CREATE_PRE_BACKUP=true
|
||||
elif [ -z "$DO_PRE_BACKUP" ]; then
|
||||
# Ask user interactively
|
||||
read -p "Create pre-restore safety backup? (Y/n): " -n 1 -r
|
||||
echo
|
||||
if [[ $REPLY =~ ^[Nn]$ ]]; then
|
||||
CREATE_PRE_BACKUP=false
|
||||
fi
|
||||
else
|
||||
echo "Error: --do-pre-backup must be 'true' or 'false'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create pre-restore backup if requested
|
||||
if [ "$CREATE_PRE_BACKUP" = true ]; then
|
||||
SAFETY_BACKUP="$BACKUP_DIR/mongo-volume-pre-restore-$(date +%Y-%m-%d-%H-%M).tar.gz"
|
||||
echo "Creating safety backup: $SAFETY_BACKUP"
|
||||
sudo tar -czf "$SAFETY_BACKUP" mongo-volume 2>/dev/null || true
|
||||
else
|
||||
echo "Skipping pre-restore backup"
|
||||
SAFETY_BACKUP=""
|
||||
fi
|
||||
|
||||
# Remove current volume
|
||||
echo "Removing current mongo-volume..."
|
||||
sudo rm -rf mongo-volume
|
||||
|
||||
# Extract backup
|
||||
echo "Extracting backup..."
|
||||
sudo tar -xzf "$BACKUP_FILE"
|
||||
|
||||
# Set proper permissions
|
||||
echo "Setting permissions..."
|
||||
sudo chown -R 999:999 mongo-volume
|
||||
|
||||
# Scale mongo service back up
|
||||
echo "Scaling mongo service back up..."
|
||||
docker service scale "$MONGO_SERVICE"=1
|
||||
|
||||
echo "Restore completed successfully!"
|
||||
if [ -n "$SAFETY_BACKUP" ]; then
|
||||
echo "Safety backup saved as: $SAFETY_BACKUP"
|
||||
fi
|
||||
Reference in New Issue
Block a user