← back to Handbag Auth Nextjs
scripts/devops/restore-backup.sh
180 lines
#!/bin/bash
#
# LUXVAULT Database Restore Script
# Restores database from backup
#
# Usage: ./restore-backup.sh <backup_file.tar.gz>
#
set -euo pipefail
if [ $# -eq 0 ]; then
echo "Usage: $0 <backup_file.tar.gz>"
echo ""
echo "Available backups:"
ls -lh /root/Projects/handbag-auth-nextjs/backups/*.tar.gz 2>/dev/null || echo "No backups found"
exit 1
fi
BACKUP_FILE="$1"
PROJECT_DIR="/root/Projects/handbag-auth-nextjs"
DATA_DIR="$PROJECT_DIR/data/auction-history"
RESTORE_DIR="/tmp/luxvault_restore_$$"
LOG_FILE="$PROJECT_DIR/logs/restore.log"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $*" | tee -a "$LOG_FILE"
}
error() {
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR:${NC} $*" | tee -a "$LOG_FILE" >&2
}
warn() {
echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARNING:${NC} $*" | tee -a "$LOG_FILE"
}
# Check if backup file exists
if [ ! -f "$BACKUP_FILE" ]; then
error "Backup file not found: $BACKUP_FILE"
exit 1
fi
log "========================================="
log "Starting database restore"
log "Backup: $BACKUP_FILE"
log "========================================="
# Confirm with user
warn "This will OVERWRITE the current database!"
read -p "Are you sure you want to continue? (yes/no): " -r
if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
log "Restore cancelled by user"
exit 0
fi
# Create temporary restore directory
mkdir -p "$RESTORE_DIR"
# Extract backup
log "Extracting backup..."
if ! tar -xzf "$BACKUP_FILE" -C "$RESTORE_DIR"; then
error "Failed to extract backup"
rm -rf "$RESTORE_DIR"
exit 1
fi
# Find the extracted directory
BACKUP_DIR=$(find "$RESTORE_DIR" -maxdepth 1 -type d -name "luxvault_backup_*" | head -1)
if [ -z "$BACKUP_DIR" ] || [ ! -d "$BACKUP_DIR" ]; then
error "Invalid backup structure"
rm -rf "$RESTORE_DIR"
exit 1
fi
log "✓ Backup extracted"
# Show backup metadata
if [ -f "$BACKUP_DIR/metadata.json" ]; then
log "Backup metadata:"
cat "$BACKUP_DIR/metadata.json" | tee -a "$LOG_FILE"
fi
# Stop auction-viewer
log "Stopping auction-viewer service..."
if pm2 stop auction-viewer 2>&1 | tee -a "$LOG_FILE"; then
log "✓ Service stopped"
else
warn "Failed to stop service (may not be running)"
fi
# Create safety backup of current data
SAFETY_BACKUP="$PROJECT_DIR/backups/pre_restore_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$SAFETY_BACKUP"
log "Creating safety backup of current data..."
cp "$DATA_DIR/auctions.db" "$SAFETY_BACKUP/" 2>/dev/null || true
cp "$DATA_DIR/auction-history.csv" "$SAFETY_BACKUP/" 2>/dev/null || true
log "✓ Safety backup created: $SAFETY_BACKUP"
# Restore database
log "Restoring database..."
if [ -f "$BACKUP_DIR/auctions.db" ]; then
cp "$BACKUP_DIR/auctions.db" "$DATA_DIR/auctions.db"
log "✓ Database restored"
else
error "Database file not found in backup"
rm -rf "$RESTORE_DIR"
exit 1
fi
# Restore CSV
if [ -f "$BACKUP_DIR/auction-history.csv" ]; then
cp "$BACKUP_DIR/auction-history.csv" "$DATA_DIR/auction-history.csv"
log "✓ CSV restored"
fi
# Restore log
if [ -f "$BACKUP_DIR/scraper.log" ]; then
cp "$BACKUP_DIR/scraper.log" "$DATA_DIR/scraper.log"
log "✓ Scraper log restored"
fi
# Verify database integrity
log "Verifying database integrity..."
if sqlite3 "$DATA_DIR/auctions.db" "PRAGMA integrity_check;" | grep -q "ok"; then
log "✓ Database integrity verified"
RECORD_COUNT=$(sqlite3 "$DATA_DIR/auctions.db" "SELECT COUNT(*) FROM auctions;")
log "Record count: $RECORD_COUNT"
else
error "Database integrity check failed!"
log "Restoring safety backup..."
cp "$SAFETY_BACKUP/auctions.db" "$DATA_DIR/auctions.db"
rm -rf "$RESTORE_DIR"
exit 1
fi
# Restart auction-viewer
log "Restarting auction-viewer service..."
if pm2 restart auction-viewer 2>&1 | tee -a "$LOG_FILE"; then
log "✓ Service restarted"
else
error "Failed to restart service"
exit 1
fi
# Wait for service to be healthy
log "Waiting for service to be healthy..."
sleep 5
for i in {1..10}; do
if curl -sf --max-time 5 "http://45.61.58.125:7500/health" > /dev/null; then
log "✓ Service is healthy"
break
fi
if [ $i -eq 10 ]; then
error "Service failed to become healthy"
exit 1
fi
sleep 2
done
# Cleanup
rm -rf "$RESTORE_DIR"
log "========================================="
log "✅ RESTORE SUCCESSFUL"
log "========================================="
log "Restored from: $BACKUP_FILE"
log "Records: $RECORD_COUNT"
log "Safety backup: $SAFETY_BACKUP"
log "Service URL: http://45.61.58.125:7500"
log "========================================="
exit 0