# Backup Strategy

> Automated backup solution for WorkRoot website - file-based content, configurations, and critical assets.

---

## 📋 Overview

This backup strategy covers:
- ✅ Content files (blog posts, markdown)
- ✅ Configuration files (Astro, Tailwind, Playwright)
- ✅ Environment files (.env)
- ✅ Source code (src/, public/)
- ✅ Automated scheduling and retention

**No database** - This is a static Astro site with file-based content.

---

## 🎯 Backup Scope

### What Gets Backed Up

| Category | Files/Directories | Priority | Frequency |
|----------|------------------|----------|-----------|
| **Content** | `src/content/**/*.md` | CRITICAL | Daily |
| **Source Code** | `src/**/*` | HIGH | Daily |
| **Public Assets** | `public/**/*` | HIGH | Daily |
| **Configurations** | `*.config.{js,ts,mjs}`, `package.json` | CRITICAL | Daily |
| **Environment** | `.env`, `.env.example` | CRITICAL | On change |
| **Documentation** | `*.md`, `.agents/**/*` | MEDIUM | Weekly |

### What's Excluded

- `node_modules/` - Reinstallable via npm
- `dist/` - Build artifacts (regenerated)
- `.astro/` - Temporary build cache
- `test-results/` - Test outputs
- `.git/` - Version control handles this

---

## 🔄 Backup Types

### 1. Full Backup
**When:** Daily at 2 AM (production), on-demand (manual)
**Contains:** All files in scope
**Retention:** 7 daily, 4 weekly, 3 monthly

### 2. Incremental Backup
**When:** Every 6 hours (production)
**Contains:** Changed files only
**Retention:** 72 hours

### 3. Critical Config Backup
**When:** Before any deployment
**Contains:** Environment and config files only
**Retention:** Last 10 deployments

---

## 📅 Retention Policy

| Backup Type | Retention Period | Storage Location |
|-------------|------------------|------------------|
| **Daily** | 7 days | `backups/daily/` |
| **Weekly** | 4 weeks | `backups/weekly/` |
| **Monthly** | 3 months | `backups/monthly/` |
| **Pre-deployment** | Last 10 | `backups/pre-deploy/` |

### Storage Requirements

- **Daily:** ~50-100 MB per backup
- **Weekly:** ~500 MB total
- **Monthly:** ~1.5 GB total
- **Estimated total:** ~2.5 GB

---

## 🛠️ Automated Backup Scripts

### Location
All backup scripts are in: `scripts/backup/`

### Available Scripts

| Script | Purpose | Usage |
|--------|---------|-------|
| `backup-full.sh` | Full backup of all critical files | `npm run backup:full` |
| `backup-content.sh` | Content files only (quick) | `npm run backup:content` |
| `backup-config.sh` | Config and env files only | `npm run backup:config` |
| `restore.sh` | Restore from backup | `npm run backup:restore` |
| `cleanup-old.sh` | Remove old backups per retention policy | Auto (cron) |

---

## ⚙️ Setup Instructions

### 1. Initial Setup

```bash
# Create backup directories
mkdir -p backups/{daily,weekly,monthly,pre-deploy}

# Make scripts executable
chmod +x scripts/backup/*.sh

# Test backup
npm run backup:full
```

### 2. Configure Automated Scheduling

#### Linux/macOS (cron)

```bash
# Edit crontab
crontab -e

# Add these lines:
# Daily full backup at 2 AM
0 2 * * * cd /path/to/project && npm run backup:full

# Incremental every 6 hours
0 */6 * * * cd /path/to/project && npm run backup:content

# Weekly cleanup on Sunday at 3 AM
0 3 * * 0 cd /path/to/project && npm run backup:cleanup
```

#### Windows (Task Scheduler)

```powershell
# Create daily backup task
schtasks /create /tn "WorkRoot-DailyBackup" /tr "npm run backup:full" /sc daily /st 02:00

# Create 6-hour incremental
schtasks /create /tn "WorkRoot-IncrementalBackup" /tr "npm run backup:content" /sc hourly /mo 6

# Weekly cleanup
schtasks /create /tn "WorkRoot-CleanupBackup" /tr "npm run backup:cleanup" /sc weekly /d SUN /st 03:00
```

#### Cloud Platform (PM2 or systemd)

```bash
# Using PM2 ecosystem
pm2 start ecosystem.config.js
pm2 save
pm2 startup
```

---

## 🔧 Restoration Procedures

### Full Restore

```bash
# List available backups
npm run backup:list

# Restore from specific backup
npm run backup:restore -- backups/daily/2026-03-21.tar.gz

# Verify restoration
npm run build
npm run test
```

### Partial Restore (Content Only)

```bash
# Extract content from backup
tar -xzf backups/daily/2026-03-21.tar.gz src/content/

# Verify content
git status
```

### Emergency Recovery

If automation fails:

```bash
# Manual restore from backup file
tar -xzf /path/to/backup.tar.gz -C /recovery/location/

# Copy to project
cp -r /recovery/location/src ./
cp /recovery/location/.env ./

# Rebuild
npm install
npm run build
```

---

## 🔐 Security Best Practices

### 1. Environment Variables

- ✅ `.env` is backed up but **encrypted**
- ✅ Backups stored in secure location (not in git)
- ✅ Access restricted to DevOps team only

### 2. Backup Encryption

```bash
# Encrypt backup (recommended for cloud storage)
gpg --symmetric --cipher-algo AES256 backup.tar.gz

# Decrypt when restoring
gpg --decrypt backup.tar.gz.gpg > backup.tar.gz
```

### 3. Off-site Storage

**Recommended:** Store backups in multiple locations

| Location | Type | Purpose |
|----------|------|---------|
| **Local Server** | Primary | Fast recovery |
| **Cloud Storage** | Secondary | Disaster recovery |
| **Version Control** | Tertiary | Config files only |

Supported cloud providers:
- AWS S3
- Google Cloud Storage
- Azure Blob Storage
- Backblaze B2

---

## 📊 Monitoring & Alerts

### Backup Health Checks

```bash
# Verify latest backup
npm run backup:verify

# Check backup size and age
npm run backup:status
```

### Alert Conditions

| Condition | Action |
|-----------|--------|
| Backup fails | Email DevOps team |
| Backup > 24h old | Warning alert |
| Backup > 48h old | Critical alert |
| Storage > 80% full | Cleanup required |

---

## 🧪 Testing Restoration

**CRITICAL:** Test backups monthly

```bash
# Monthly drill procedure
1. Create test environment
2. Restore from last week's backup
3. Run build and tests
4. Verify content loads correctly
5. Document any issues

# Quick test (every backup)
npm run backup:verify
```

---

## 📝 Pre-Deployment Backup

**Always backup before deployment!**

```bash
# Automatic (included in deployment script)
npm run deploy  # Runs backup:config automatically

# Manual pre-deployment backup
npm run backup:pre-deploy
```

---

## 🆘 Troubleshooting

### Backup Fails

```bash
# Check disk space
df -h

# Check permissions
ls -la backups/

# Verify scripts are executable
ls -la scripts/backup/
```

### Restore Fails

```bash
# Verify backup integrity
tar -tzf backup.tar.gz

# Check for corruption
gzip -t backup.tar.gz
```

### Missing Files After Restore

```bash
# Compare with backup contents
tar -tzf backup.tar.gz | grep "missing-file"

# Check exclusions in backup script
cat scripts/backup/backup-full.sh
```

---

## 📞 Emergency Contacts

| Role | Responsibility | Contact |
|------|----------------|---------|
| **DevOps Lead** | Backup system owner | [Contact info] |
| **Platform Admin** | Server access, storage | [Contact info] |
| **Tech Lead** | Code verification post-restore | [Contact info] |

---

## 🔄 Backup Lifecycle

```
┌─────────────────┐
│  Trigger Event  │ (Cron, Manual, Pre-deploy)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Run Backup     │ (Full, Incremental, Config)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Compress &     │ (tar.gz, optional encryption)
│  Archive        │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Store Locally  │ (backups/daily|weekly|monthly/)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Sync to Cloud  │ (Optional: S3, GCS, Azure)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Verify Backup  │ (Size, integrity check)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Cleanup Old    │ (Per retention policy)
└─────────────────┘
```

---

## ✅ Checklist

### Setup Checklist
- [ ] Backup directories created
- [ ] Scripts installed and executable
- [ ] Cron jobs / Task Scheduler configured
- [ ] Cloud storage configured (if using)
- [ ] Email alerts set up
- [ ] First full backup completed
- [ ] Restore tested successfully

### Monthly Maintenance
- [ ] Test restoration procedure
- [ ] Verify backup integrity
- [ ] Check storage usage
- [ ] Review retention policy
- [ ] Update documentation
- [ ] Train team on procedures

---

## 📚 Related Documentation

- [DEPLOYMENT.md](../../DEPLOYMENT.md) - Deployment procedures
- [MIGRATION-CHECKLIST.md](../documentation-writer/MIGRATION-CHECKLIST.md) - Domain migration
- [Security Audit](../security-auditor/SECURITY-AUDIT.md) - Security configurations

---

**Last Updated:** 2026-03-21
**Version:** 1.0
**Maintained by:** DevOps Team
