---
name: step-01-backup
description: Create backup of all migration files
next_step: step-02-fetch.md
---

# Step 1: Backup

## YOUR TASK:

Create a complete backup of all migration files before destructive operations.

**Previous step:** `step-00-init.md`

---

## EXECUTION SEQUENCE:

### 1. Create Backup Directory

```bash
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR=".claude/gitflow/backup/migrations/squash_${TIMESTAMP}"

mkdir -p "$BACKUP_DIR"
echo "Backup directory: $BACKUP_DIR"
```

### 2. Backup All Migration Files

```bash
# Backup all .cs files from migrations directory
cp "$MIGRATIONS_DIR"/*.cs "$BACKUP_DIR/" 2>/dev/null || {
  echo "ERROR: No migration files found to backup"
  exit 1
}

# Count backed up files
BACKUP_COUNT=$(ls -1 "$BACKUP_DIR"/*.cs 2>/dev/null | wc -l)
echo "Backed up: $BACKUP_COUNT files"
```

### 3. Create Backup Manifest

```bash
cat > "$BACKUP_DIR/MANIFEST.md" << EOF
# Squash Backup Manifest

**Created:** $(date -Iseconds)
**Branch:** $CURRENT_BRANCH
**Parent:** $BASE_BRANCH
**DbContext:** $DBCONTEXT

## Files Backed Up

\`\`\`
$(ls -la "$BACKUP_DIR"/*.cs)
\`\`\`

## Migrations to Squash

$(for mig in "${BRANCH_ONLY_MIGRATIONS[@]}"; do echo "- $mig"; done)

## Restore Command

\`\`\`bash
cp "$BACKUP_DIR"/*.cs "$MIGRATIONS_DIR/"
\`\`\`
EOF

echo "Manifest created: $BACKUP_DIR/MANIFEST.md"
```

### 4. Verify Backup

```bash
# Ensure critical files exist
SNAPSHOT_BACKUP=$(ls "$BACKUP_DIR"/*ModelSnapshot.cs 2>/dev/null | head -1)

if [ -z "$SNAPSHOT_BACKUP" ]; then
  echo "WARNING: No ModelSnapshot in backup"
fi

echo ""
echo "Backup complete:"
echo "  Directory: $BACKUP_DIR"
echo "  Files: $BACKUP_COUNT"
echo "  Snapshot: $(basename "$SNAPSHOT_BACKUP" 2>/dev/null || echo 'N/A')"
```

---

## STATE OUTPUT:

| Variable | Value |
|----------|-------|
| `{backup_dir}` | `.claude/gitflow/backup/migrations/squash_{timestamp}` |
| `{backup_count}` | Number of files backed up |

---

## RECOVERY:

If squash fails, restore with:

```bash
cp "{BACKUP_DIR}"/*.cs "{MIGRATIONS_DIR}/"
```

---

## SUCCESS CRITERIA:

- Backup directory created
- All migration files copied
- Manifest created

## NEXT STEP:

→ `step-02-fetch.md`
