# Automatic Backup System

## ✅ Setup Complete!

The backend now automatically runs backups when the application starts. No cron setup needed!

## How It Works

When you start the backend:
```bash
cd backend
uvicorn main:app --reload
```

The backup scheduler starts automatically and you'll see:
```
⏰ Backup scheduler started: EVERY 1 MINUTE (testing mode)
⚠️  WARNING: Test mode active! Change BACKUP_INTERVAL to 'weekly' for production
```

## Testing (Current Setting)

**Currently set to: `BACKUP_INTERVAL=test`**

- Runs **every 1 minute**
- Check backend console logs for backup status
- Check R2 bucket for new files

**Expected console output:**
```
[2025-12-21 15:30:45] INFO: Starting scheduled backup...
[2025-12-21 15:30:53] INFO: ✅ Scheduled backup completed: recipes_db_20251221_153045.sql.gz
```

## Change to Production Schedule

After testing works, update `.env`:

```env
# Change from:
BACKUP_INTERVAL=test

# To one of these:
BACKUP_INTERVAL=weekly    # Sunday at 2:00 AM (recommended)
BACKUP_INTERVAL=daily     # Every day at 2:00 AM
BACKUP_INTERVAL=disabled  # Turn off automatic backups
```

Then restart the backend:
```bash
# Stop current server (Ctrl+C)
# Start again
uvicorn main:app --reload
```

You'll see:
```
⏰ Backup scheduler started: WEEKLY on Sundays at 2:00 AM
✅ Backup scheduler is running
```

## Available Options

| Setting | Description | When it runs |
|---------|-------------|--------------|
| `test` | Testing mode | Every 1 minute |
| `daily` | Daily backups | Every day at 2:00 AM |
| `weekly` | Weekly backups | Sundays at 2:00 AM |
| `disabled` | No automatic backups | Never (manual only) |

## Manual Backup Still Available

Admin users can still trigger manual backups from the Admin Panel in the UI, regardless of the automatic schedule.

## Monitoring

### Check if scheduler is running
Look for these messages in backend console when starting:
```
⏰ Backup scheduler started: ...
✅ Backup scheduler is running
```

### Watch backup logs in real-time
The scheduled backups show in your backend console:
```
[2025-12-21 15:30:45] INFO: Starting scheduled backup...
[2025-12-21 15:30:53] INFO: ✅ Scheduled backup completed: recipes_db_20251221_153045.sql.gz
```

### Verify backups are created
- Check R2 bucket in Cloudflare dashboard
- Look for files: `recipes_db_YYYYMMDD_HHMMSS.sql.gz`

## Production Deployment

When deploying to production:

1. **Update `.env`:**
   ```env
   BACKUP_INTERVAL=weekly
   ```

2. **Keep the backend running:**
   - Use systemd, docker, or process manager
   - The scheduler only runs while the backend is running

3. **Using Docker:**
   ```dockerfile
   # In your Dockerfile or docker-compose.yml
   # No additional cron setup needed!
   # The app handles scheduling internally
   ```

## Troubleshooting

### "Backup scheduler is DISABLED"
- Check `.env` has `BACKUP_INTERVAL` set
- Not set to `disabled`

### No backups running in test mode
- Check backend console for error messages
- Verify R2 credentials in `.env`
- Verify database credentials in `.env`
- Check that `APScheduler` is installed: `pip install APScheduler`

### Backups not running at scheduled time
- Backend must be running 24/7
- Use systemd or docker in production
- Check server timezone matches expected schedule

### Want to disable automatic backups
```env
BACKUP_INTERVAL=disabled
```

## Benefits Over Cron

✅ **No external setup** - Works immediately when backend starts
✅ **Cross-platform** - Works on Windows, Linux, Docker
✅ **Easy testing** - Just change one env variable
✅ **Logs in console** - See backup status in backend logs
✅ **No permissions issues** - Runs with same permissions as backend

## Next Steps

1. **Test now**: Start backend and wait 1-2 minutes
2. **Verify**: Check console logs and R2 bucket
3. **Switch to weekly**: Change `.env` to `BACKUP_INTERVAL=weekly`
4. **Restart backend**: The new schedule takes effect
