# Backup and Restore Plan

## Overview
This document outlines the comprehensive backup and restore strategy to ensure zero code breakage during the server organization process.

## Current State Backup

### 1. Complete Server Backup
```bash
# Create timestamped backup directory
BACKUP_DIR="/home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p $BACKUP_DIR

# Backup entire home directory
tar -czf $BACKUP_DIR/home_backup.tar.gz -C / home/jcbeasley

# Backup running processes information
ps aux > $BACKUP_DIR/running_processes.txt
ss -tulpn > $BACKUP_DIR/port_usage.txt
crontab -l > $BACKUP_DIR/crontab_backup.txt 2>/dev/null || echo "No crontab" > $BACKUP_DIR/crontab_backup.txt
```

### 2. Application-Specific Backups

#### IT Site Survey AI (Port 3003)
```bash
# Backup current working directory
tar -czf /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/site-survey-ai_backup.tar.gz -C /home/jcbeasley/.openclaw/workspace/Projects site-survey-ai

# Export current in-memory data (if possible)
# This would require adding an export endpoint to the running application
```

#### Client Onboarding (Port 5000)
```bash
# Backup current working directory
tar -czf /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/client-onboarding_backup.tar.gz -C /home/jcbeasley/.openclaw/workspace/Projects client-onboarding
```

#### Projects Manager (Port 3456)
```bash
# Backup current working directory
tar -czf /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/projects-manager_backup.tar.gz -C /home/jcbeasley projects-manager
```

### 3. Process Configuration Backup
```bash
# Document how each application is started
echo "IT Site Survey AI: cd /home/jcbeasley/.openclaw/workspace/Projects/site-survey-ai && source venv/bin/activate && nohup python3 app.py > app.log 2>&1 &" > /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/process_configs.txt
echo "Client Onboarding: cd /home/jcbeasley/.openclaw/workspace/Projects/client-onboarding && source venv/bin/activate && nohup python3 app.py > app.log 2>&1 &" >> /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/process_configs.txt
echo "Projects Manager: cd /home/jcbeasley/projects-manager && source venv/bin/activate && nohup python3 app.py > app.log 2>&1 &" >> /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/process_configs.txt
```

## Restore Procedures

### 1. Full Server Restore
```bash
# Stop all running applications
pkill -f "python.*app.py"
pkill -f "python.*server.py"

# Restore from backup
tar -xzf /home/jcbeasley/backups/{backup_timestamp}/home_backup.tar.gz -C /

# Restart applications using documented procedures
```

### 2. Individual Application Restore
```bash
# Stop specific application
pkill -f "site-survey-ai"

# Restore application directory
tar -xzf /home/jcbeasley/backups/{backup_timestamp}/site-survey-ai_backup.tar.gz -C /home/jcbeasley/.openclaw/workspace/Projects/

# Restart application
cd /home/jcbeasley/.openclaw/workspace/Projects/site-survey-ai && source venv/bin/activate && nohup python3 app.py > app.log 2>&1 &
```

### 3. Process Restore
```bash
# Use documented process configurations to restart applications
# Follow the exact commands from process_configs.txt
```

## Data Protection

### 1. In-Memory Data Export
Before any reorganization, export current data:

#### IT Site Survey AI Data Export
```bash
# Add endpoint to export all survey data
curl -s http://localhost:3003/api/surveys/responses > /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/site_survey_data.json
```

#### Client Onboarding Data Export
```bash
# Would need to add similar endpoint to export client data
```

### 2. Database Preparation
```bash
# Install PostgreSQL if not already installed
sudo apt update
sudo apt install postgresql postgresql-contrib

# Create databases for each application
sudo -u postgres createdb site_survey_ai
sudo -u postgres createdb client_onboarding
sudo -u postgres createdb projects_manager
```

## Safety Checks

### 1. Pre-Change Verification
```bash
# Verify all applications are running
curl -s http://localhost:3003/ > /dev/null && echo "IT Site Survey AI OK" || echo "IT Site Survey AI DOWN"
curl -s http://localhost:5000/ > /dev/null && echo "Client Onboarding OK" || echo "Client Onboarding DOWN"
curl -s http://localhost:3456/ > /dev/null && echo "Projects Manager OK" || echo "Projects Manager DOWN"

# Verify data integrity
curl -s http://localhost:3003/api/surveys/responses | jq '.responses | length' > /home/jcbeasley/backups/$(date +%Y%m%d_%H%M%S)/response_count_before.txt
```

### 2. Post-Change Verification
```bash
# Verify all applications are running after changes
# Verify data integrity matches before state
# Verify no functionality loss
```

## Rollback Triggers

### 1. Automatic Rollback Conditions
- Application fails to start after changes
- Data loss detected (>10% data missing)
- Critical functionality broken
- Performance degradation >50%

### 2. Manual Rollback Process
```bash
# Stop all new processes
pkill -f "new_application_structure"

# Restore from backup
./restore_script.sh {backup_timestamp}

# Verify restoration
./verification_script.sh
```

## Backup Schedule

### 1. Before Any Major Change
- Complete backup of affected areas
- Document current state
- Export critical data

### 2. Daily Incremental Backups
```bash
# Script to run daily
0 2 * * * /home/jcbeasley/scripts/daily_backup.sh
```

### 3. Weekly Full Backups
```bash
# Script to run weekly
0 3 * * 0 /home/jcbeasley/scripts/weekly_full_backup.sh
```

## Emergency Procedures

### 1. Immediate Rollback
```bash
# Single command to rollback entire server
/home/jcbeasley/scripts/emergency_rollback.sh {backup_timestamp}
```

### 2. Application-Specific Recovery
```bash
# Recover single application
/home/jcbeasley/scripts/recover_application.sh {app_name} {backup_timestamp}
```

## Testing Restore Procedures

### 1. Monthly Restore Drills
- Test full server restore to test environment
- Test individual application restores
- Verify data integrity after restore
- Document any issues found

### 2. Restore Time Targets
- Full server restore: < 30 minutes
- Individual application restore: < 5 minutes
- Data verification: < 10 minutes

## Backup Storage

### 1. Local Storage
- `/home/jcbeasley/backups/` - Primary backup location
- Rotating storage (keep last 30 days)

### 2. Offsite Storage
- Copy critical backups to external storage
- Cloud backup for disaster recovery

## Monitoring

### 1. Backup Success Monitoring
- Log all backup operations
- Alert on backup failures
- Verify backup integrity

### 2. Restore Readiness Monitoring
- Periodic restore testing
- Backup accessibility verification
- Storage space monitoring

This comprehensive backup and restore plan ensures that any changes to the server organization can be safely made with full confidence in the ability to restore to a working state if needed.