# Git Automatic Backup System

This project uses Git for automatic backups on every build. Each build is automatically committed and tagged, allowing you to restore to any previous build.

## Quick Start

### 1. Build with Automatic Backup

Instead of building manually, use the backup script:

```bash
./build-with-backup.sh
```

This script will:
- ✅ Automatically commit all changes
- ✅ Create a build tag (e.g., `build-v1-20240101-120000`)
- ✅ Build backend and frontend
- ✅ Save build information

### 2. List All Builds

To see all available builds:

```bash
./list-builds.sh
```

This shows:
- Build tags
- Commit hashes
- Build dates
- Build messages

### 3. Restore to a Previous Build

To restore to a specific build:

```bash
./restore-build.sh <build-tag>
```

Example:
```bash
./restore-build.sh build-v5-20240115-143022
```

The restore script will:
- ✅ Checkout the build tag
- ✅ Rebuild backend and frontend
- ✅ Restart PM2 services (if configured)

## How It Works

### Build Process

1. **Pre-Build**: All changes are automatically committed
2. **Tag Creation**: A unique build tag is created (e.g., `build-v1-20240101-120000`)
3. **Build**: Backend and frontend are built
4. **Post-Build**: Build info is saved

### Build Tags Format

- Format: `build-v<number>-<date>-<time>`
- Example: `build-v1-20240115-143022`
- Number increments automatically
- Date/time format: `YYYYMMDD-HHMMSS`

## Integration with PM2

If you're using PM2, you can modify your build process to use the backup script:

### Option 1: Manual Build
```bash
./build-with-backup.sh
pm2 restart erp-backend
pm2 restart erp-frontend
```

### Option 2: Add to Package Scripts

Add to `package.json` (if you have one):
```json
{
  "scripts": {
    "build": "./build-with-backup.sh",
    "restore": "./restore-build.sh"
  }
}
```

## File Structure

```
erp_codearya/
├── .git/                    # Git repository
├── .gitignore              # Files to ignore in Git
├── build-with-backup.sh    # Main build script with backup
├── restore-build.sh        # Restore to a specific build
├── list-builds.sh          # List all available builds
└── .build-info             # Current build information (auto-generated)
```

## Important Notes

### What Gets Backed Up

✅ **Included:**
- All source code (backend, frontend)
- Configuration files
- Scripts
- Documentation

❌ **Excluded** (via `.gitignore`):
- `node_modules/`
- `target/` (Rust build artifacts)
- `.env` files
- Logs
- Temporary files

### Safety Features

1. **Uncommitted Changes**: The build script will commit all changes before building
2. **Build Tags**: Each build gets a unique tag for easy restoration
3. **Restore Confirmation**: Restore script asks for confirmation before restoring
4. **Stash on Restore**: Uncommitted changes are stashed before restore

## Troubleshooting

### Git Not Initialized
```bash
git init
git config user.name "Your Name"
git config user.email "your@email.com"
```

### Build Tag Not Found
```bash
# List all available tags
./list-builds.sh

# Or manually
git tag -l "build-*"
```

### Restore Failed
1. Check if you have uncommitted changes (stash them first)
2. Verify the build tag exists: `git tag -l "build-*"`
3. Check Git status: `git status`

### Build Script Permission Denied
```bash
chmod +x build-with-backup.sh restore-build.sh list-builds.sh
```

## Advanced Usage

### View Build History
```bash
git log --oneline --decorate --graph --all
```

### Compare Builds
```bash
git diff build-v1-20240101-120000 build-v2-20240102-120000
```

### Manual Tag Creation
```bash
git tag -a build-v10-20240115-120000 -m "Manual build backup"
```

### View Build Info
```bash
cat .build-info
```

## Best Practices

1. **Always use `build-with-backup.sh`** instead of manual builds
2. **Check build list regularly** to see build history
3. **Test restore process** on a development environment first
4. **Keep build tags** - don't delete them unless necessary
5. **Document major changes** in commit messages

## Example Workflow

```bash
# 1. Make code changes
# ... edit files ...

# 2. Build with automatic backup
./build-with-backup.sh

# Output:
# === Build with Automatic Git Backup ===
# Staging all changes...
# Committing changes...
# Creating build tag: build-v5-20240115-143022
# Building backend...
# Building frontend...
# === Build completed successfully! ===

# 3. If something goes wrong, restore
./list-builds.sh  # Find the build tag
./restore-build.sh build-v4-20240114-120000

# 4. Continue development
# ... make more changes ...
./build-with-backup.sh  # Next build will be build-v6
```

## Support

For issues or questions:
1. Check build logs
2. Verify Git status: `git status`
3. Check available builds: `./list-builds.sh`
4. Review `.build-info` file
