# Restore Guide - How to Restore from Backups

## 📋 Viewing Available Versions/Builds

### List All Builds
```bash
./list-builds.sh
```

This shows:
- Build tag (version number)
- Commit hash
- Build date
- Commit message
- Total number of builds

**Example Output:**
```
[1] build-v1-20260112-103710
    Commit: 2927449
    Date: 2026-01-12 08:11:48 +0000
    Message: Complete Git backup system setup

[2] build-v2-20260112-120000
    Commit: abc1234
    Date: 2026-01-12 12:00:00 +0000
    Message: Added new feature
```

### View Detailed Build Information
```bash
./view-build-details.sh build-v1-20260112-103710
```

This shows:
- ✅ Build tag and commit hash
- ✅ Build date and author
- ✅ Files changed in that build
- ✅ Differences from current version
- ✅ Restore instructions

## 🔄 Restoring to a Previous Build

### Step 1: List Available Builds
```bash
./list-builds.sh
```

### Step 2: View Build Details (Optional)
```bash
./view-build-details.sh build-v1-20260112-103710
```

This shows you:
- What changed in that build
- Files that were modified
- Differences from current version

### Step 3: Restore
```bash
./restore-build.sh build-v1-20260112-103710
```

The restore script will:
1. ✅ Ask for confirmation
2. ✅ Stash uncommitted changes (if any)
3. ✅ Checkout the build tag
4. ✅ Rebuild backend
5. ✅ Rebuild frontend
6. ✅ Restart services (PM2)

## 📊 What Information You Get

### From `list-builds.sh`:
- **Build Number**: `build-v1`, `build-v2`, etc.
- **Date & Time**: When the build was created
- **Commit Hash**: Unique identifier for that version
- **Message**: What was changed in that build

### From `view-build-details.sh`:
- **Full Details**: Complete information about the build
- **Files Changed**: List of files modified
- **Statistics**: How many files changed
- **Comparison**: Differences from current version

### From `restore-build.sh`:
- **Confirmation**: Asks before restoring
- **Safety**: Stashes uncommitted changes
- **Automatic**: Rebuilds everything
- **Status**: Shows what's happening

## 🎯 Common Restore Scenarios

### Scenario 1: Restore to Last Working Build
```bash
# 1. List builds
./list-builds.sh

# 2. Find the last working build (e.g., build-v5)
# 3. Restore
./restore-build.sh build-v5-20260112-120000
```

### Scenario 2: Compare Builds Before Restoring
```bash
# 1. View details of build you want to restore
./view-build-details.sh build-v5-20260112-120000

# 2. View details of current build
git log -1 --oneline

# 3. Restore if needed
./restore-build.sh build-v5-20260112-120000
```

### Scenario 3: Restore from GitHub (if server fails)
```bash
# 1. Clone repository from GitHub
git clone https://github.com/YOUR_USERNAME/erp-codearya-backup.git
cd erp-codearya-backup

# 2. List available builds
git tag -l "build-*"

# 3. Restore to specific build
git checkout build-v5-20260112-120000

# 4. Rebuild
cd backend && cargo build --release
cd ../frontend && npm install && npm run build
```

## 🔍 Advanced: Viewing Build History

### See All Commits
```bash
git log --oneline --decorate --graph --all
```

### See What Changed Between Builds
```bash
# Compare two builds
git diff build-v1-20260112-103710 build-v2-20260112-120000
```

### See Files Changed in a Build
```bash
git show --stat build-v1-20260112-103710
```

## ⚠️ Important Notes

1. **Uncommitted Changes**: The restore script will stash them automatically
2. **Current Work**: Make sure to commit or note your current work before restoring
3. **Database**: Restoring code doesn't restore database - you need separate DB backup
4. **Services**: PM2 services are restarted automatically after restore

## 🆘 Troubleshooting

### "Build tag not found"
```bash
# List available tags
git tag -l "build-*"

# Or check GitHub (if remote is configured)
git fetch --tags
```

### "Restore failed"
```bash
# Check Git status
git status

# Check for uncommitted changes
git diff

# Try manual restore
git checkout build-v1-20260112-103710
```

### "Can't find build on GitHub"
```bash
# Fetch from remote
git fetch origin --tags

# List remote tags
git ls-remote --tags origin
```

## 📝 Quick Reference

```bash
# List all builds
./list-builds.sh

# View build details
./view-build-details.sh <build-tag>

# Restore to build
./restore-build.sh <build-tag>

# Check backup status
./check-backup-location.sh

# Manual Git commands
git tag -l "build-*"              # List all build tags
git log --oneline --decorate      # View commit history
git show <build-tag>              # Show build details
```

---

**Remember**: Every build is a complete snapshot. You can restore to any build anytime!
