backup-system.sh•11 kB
#!/bin/bash
# EuConquisto Composer MCP - System Backup Script
# Version: 1.0.0
# Purpose: Create comprehensive backup before migration
# Usage: ./scripts/backup-system.sh
set -e # Exit on any error
# Configuration
PROJECT_ROOT="/Users/ricardokawasaki/Desktop/euconquisto-composer-mcp-poc"
BACKUP_ROOT="$PROJECT_ROOT/backups/migration-safety"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="$BACKUP_ROOT/backup_$TIMESTAMP"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging function
log() {
echo -e "${BLUE}[BACKUP]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Create backup directory
create_backup_dir() {
log "Creating backup directory: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
mkdir -p "$BACKUP_DIR/tier1-essential"
mkdir -p "$BACKUP_DIR/tier2-configuration"
mkdir -p "$BACKUP_DIR/tier3-historical"
mkdir -p "$BACKUP_DIR/checksums"
}
# Calculate file checksum
calculate_checksum() {
local file="$1"
if [ -f "$file" ]; then
shasum -a 256 "$file" | cut -d' ' -f1
else
echo "FILE_NOT_FOUND"
fi
}
# Backup Tier 1: Essential System Files
backup_tier1() {
log "Backing up Tier 1: Essential System Files"
local tier1_dir="$BACKUP_DIR/tier1-essential"
# Production v4.0.3 system
if [ -f "$PROJECT_ROOT/dist/browser-automation-api-direct-save-v4.0.3.js" ]; then
cp "$PROJECT_ROOT/dist/browser-automation-api-direct-save-v4.0.3.js" "$tier1_dir/"
calculate_checksum "$PROJECT_ROOT/dist/browser-automation-api-direct-save-v4.0.3.js" > "$BACKUP_DIR/checksums/v4.0.3-main.sha256"
success "✅ Backed up v4.0.3 main file"
else
error "❌ v4.0.3 main file not found!"
exit 1
fi
# JWT Token
if [ -f "$PROJECT_ROOT/archive/authentication/correct-jwt-new.txt" ]; then
cp "$PROJECT_ROOT/archive/authentication/correct-jwt-new.txt" "$tier1_dir/"
calculate_checksum "$PROJECT_ROOT/archive/authentication/correct-jwt-new.txt" > "$BACKUP_DIR/checksums/jwt-token.sha256"
# Verify JWT token length
local jwt_length=$(wc -c < "$PROJECT_ROOT/archive/authentication/correct-jwt-new.txt")
if [ "$jwt_length" -eq 3276 ]; then
success "✅ JWT token backed up (3276 characters verified)"
else
warning "⚠️ JWT token length unexpected: $jwt_length characters"
fi
else
error "❌ JWT token not found!"
exit 1
fi
# JWT Redirect Server
if [ -f "$PROJECT_ROOT/tools/servers/jwt-redirect-server-v1.0.2.js" ]; then
cp "$PROJECT_ROOT/tools/servers/jwt-redirect-server-v1.0.2.js" "$tier1_dir/"
calculate_checksum "$PROJECT_ROOT/tools/servers/jwt-redirect-server-v1.0.2.js" > "$BACKUP_DIR/checksums/jwt-server.sha256"
success "✅ JWT redirect server backed up"
else
error "❌ JWT redirect server not found!"
exit 1
fi
# Package configuration
if [ -f "$PROJECT_ROOT/package.json" ]; then
cp "$PROJECT_ROOT/package.json" "$tier1_dir/"
calculate_checksum "$PROJECT_ROOT/package.json" > "$BACKUP_DIR/checksums/package-json.sha256"
success "✅ package.json backed up"
fi
if [ -f "$PROJECT_ROOT/package-lock.json" ]; then
cp "$PROJECT_ROOT/package-lock.json" "$tier1_dir/"
calculate_checksum "$PROJECT_ROOT/package-lock.json" > "$BACKUP_DIR/checksums/package-lock.sha256"
success "✅ package-lock.json backed up"
fi
# New modular infrastructure
if [ -d "$PROJECT_ROOT/src/infrastructure" ]; then
cp -r "$PROJECT_ROOT/src/infrastructure" "$tier1_dir/"
success "✅ Modular infrastructure backed up"
# Calculate checksums for each module
for module in "$PROJECT_ROOT/src/infrastructure"/*.js; do
if [ -f "$module" ]; then
local module_name=$(basename "$module" .js)
calculate_checksum "$module" > "$BACKUP_DIR/checksums/module-$module_name.sha256"
fi
done
else
warning "⚠️ Modular infrastructure not found"
fi
}
# Backup Tier 2: Configuration and Documentation
backup_tier2() {
log "Backing up Tier 2: Configuration and Documentation"
local tier2_dir="$BACKUP_DIR/tier2-configuration"
# Migration documentation
if [ -d "$PROJECT_ROOT/docs/euconquisto-migration" ]; then
cp -r "$PROJECT_ROOT/docs/euconquisto-migration" "$tier2_dir/"
success "✅ Migration documentation backed up"
fi
# Migration plan
if [ -f "$PROJECT_ROOT/docs/planning/EuConquisto Composer MCP - Claude Code Migration Plan v1.0.md" ]; then
mkdir -p "$tier2_dir/planning"
cp "$PROJECT_ROOT/docs/planning/EuConquisto Composer MCP - Claude Code Migration Plan v1.0.md" "$tier2_dir/planning/"
success "✅ Migration plan backed up"
fi
# Key context files
for file in "CLAUDE_DESKTOP_CONTEXT.md" "CURRENT_STATUS.md" "README.md" "tsconfig.json"; do
if [ -f "$PROJECT_ROOT/$file" ]; then
cp "$PROJECT_ROOT/$file" "$tier2_dir/"
success "✅ $file backed up"
fi
done
}
# Backup Tier 3: Historical and Development Files
backup_tier3() {
log "Backing up Tier 3: Historical and Development Files"
local tier3_dir="$BACKUP_DIR/tier3-historical"
# Historical versions
for version in "v4.0.0" "v4.0.1" "v4.0.2"; do
local file="$PROJECT_ROOT/dist/browser-automation-api-direct-save-$version.js"
if [ -f "$file" ]; then
cp "$file" "$tier3_dir/"
success "✅ Historical version $version backed up"
fi
done
# Backup file
if [ -f "$PROJECT_ROOT/dist/browser-automation-api-direct-save-v4.0.3-BACKUP.js" ]; then
cp "$PROJECT_ROOT/dist/browser-automation-api-direct-save-v4.0.3-BACKUP.js" "$tier3_dir/"
success "✅ v4.0.3 backup file backed up"
fi
# Development tools
if [ -d "$PROJECT_ROOT/tools" ]; then
cp -r "$PROJECT_ROOT/tools" "$tier3_dir/"
success "✅ Development tools backed up"
fi
}
# Create backup manifest
create_manifest() {
log "Creating backup manifest"
local manifest="$BACKUP_DIR/BACKUP_MANIFEST.md"
cat > "$manifest" << EOF
# EuConquisto Composer MCP - Backup Manifest
**Created**: $(date)
**Backup ID**: backup_$TIMESTAMP
**Purpose**: Migration safety backup before Phase 2
## Backup Contents
### Tier 1: Essential System Files
- \`browser-automation-api-direct-save-v4.0.3.js\` - Production system (v4.0.3)
- \`correct-jwt-new.txt\` - JWT authentication token
- \`jwt-redirect-server-v1.0.2.js\` - Authentication server
- \`package.json\` - Node.js dependencies
- \`package-lock.json\` - Dependency lock file
- \`infrastructure/\` - New modular infrastructure (v5.0.0-alpha)
### Tier 2: Configuration and Documentation
- \`euconquisto-migration/\` - Migration documentation
- \`planning/\` - Migration plan
- Configuration files (README, tsconfig, etc.)
### Tier 3: Historical and Development
- Historical versions (v4.0.0, v4.0.1, v4.0.2)
- Development tools and utilities
- Backup files
## File Integrity
Checksums stored in \`checksums/\` directory for verification.
## Recovery Instructions
See \`RECOVERY_PROCEDURES.md\` for detailed rollback instructions.
## System State at Backup
- **Working System**: v4.0.3 ✅
- **JWT Server**: Running on port 8080 ✅
- **Modular Infrastructure**: Implemented and tested ✅
- **Migration Status**: Phase 1 Complete, ready for Phase 2
## Emergency Contact
Migration managed by Claude Code
Documentation: /docs/euconquisto-migration/
EOF
success "✅ Backup manifest created"
}
# Verify backup integrity
verify_backup() {
log "Verifying backup integrity"
local errors=0
# Check essential files
local essential_files=(
"tier1-essential/browser-automation-api-direct-save-v4.0.3.js"
"tier1-essential/correct-jwt-new.txt"
"tier1-essential/jwt-redirect-server-v1.0.2.js"
"tier1-essential/package.json"
)
for file in "${essential_files[@]}"; do
if [ -f "$BACKUP_DIR/$file" ]; then
success "✅ Verified: $file"
else
error "❌ Missing: $file"
((errors++))
fi
done
# Verify JWT token length in backup
if [ -f "$BACKUP_DIR/tier1-essential/correct-jwt-new.txt" ]; then
local jwt_length=$(wc -c < "$BACKUP_DIR/tier1-essential/correct-jwt-new.txt")
if [ "$jwt_length" -eq 3276 ]; then
success "✅ JWT token integrity verified (3276 characters)"
else
error "❌ JWT token integrity failed: $jwt_length characters"
((errors++))
fi
fi
if [ $errors -eq 0 ]; then
success "✅ All backup verification checks passed"
return 0
else
error "❌ Backup verification failed with $errors errors"
return 1
fi
}
# Create compressed archive
create_archive() {
log "Creating compressed archive"
local archive_name="euconquisto-backup-$TIMESTAMP.tar.gz"
local archive_path="$BACKUP_ROOT/$archive_name"
tar -czf "$archive_path" -C "$BACKUP_ROOT" "backup_$TIMESTAMP"
if [ -f "$archive_path" ]; then
local archive_size=$(ls -lh "$archive_path" | awk '{print $5}')
success "✅ Compressed archive created: $archive_name ($archive_size)"
# Create checksum for archive
calculate_checksum "$archive_path" > "$archive_path.sha256"
success "✅ Archive checksum created"
else
error "❌ Failed to create compressed archive"
return 1
fi
}
# Main execution
main() {
echo "========================================"
echo " EuConquisto Composer MCP Backup"
echo "========================================"
echo ""
log "Starting system backup for migration safety"
log "Timestamp: $TIMESTAMP"
log "Backup location: $BACKUP_DIR"
echo ""
create_backup_dir
backup_tier1
backup_tier2
backup_tier3
create_manifest
if verify_backup; then
create_archive
echo ""
success "🎉 BACKUP COMPLETED SUCCESSFULLY"
echo ""
echo "Backup Details:"
echo " 📁 Directory: $BACKUP_DIR"
echo " 📦 Archive: euconquisto-backup-$TIMESTAMP.tar.gz"
echo " 📋 Manifest: $BACKUP_DIR/BACKUP_MANIFEST.md"
echo ""
echo "✅ System is ready for Phase 2 migration"
echo "✅ Rollback procedures available if needed"
else
error "❌ BACKUP FAILED - DO NOT PROCEED WITH MIGRATION"
exit 1
fi
}
# Execute main function
main "$@"