# Weekly Backup Setup - Windows Task Scheduler

This guide will help you set up automatic weekly backups of your database.

## Setup Instructions

### 1. Create Batch Script

Create a file `run_backup.bat` in the `backend` folder:

```batch
@echo off
cd /d "%~dp0"
python backup_db.py >> backup.log 2>&1
```

### 2. Open Task Scheduler

1. Press `Win + R`
2. Type `taskschd.msc`
3. Press Enter

### 3. Create New Task

1. Click "Create Task" (not "Create Basic Task")
2. In **General** tab:
   - Name: `Recipe DB Weekly Backup`
   - Description: `Automatic weekly database backup to Cloudflare R2`
   - Select "Run whether user is logged on or not"
   - Check "Run with highest privileges"

### 4. Configure Trigger

1. Go to **Triggers** tab
2. Click "New..."
3. Configure:
   - Begin the task: `On a schedule`
   - Settings: `Weekly`
   - Recur every: `1 weeks`
   - Days: Select `Sunday` (or your preferred day)
   - Time: `02:00:00` (2 AM)
   - Check "Enabled"
4. Click OK

### 5. Configure Action

1. Go to **Actions** tab
2. Click "New..."
3. Configure:
   - Action: `Start a program`
   - Program/script: `C:\Path\To\backend\run_backup.bat`
     *(Replace with your actual path)*
   - Start in: `C:\Path\To\backend\`
     *(Replace with your actual path)*
4. Click OK

### 6. Additional Settings

1. Go to **Conditions** tab:
   - Uncheck "Start the task only if the computer is on AC power"
   
2. Go to **Settings** tab:
   - Check "Run task as soon as possible after a scheduled start is missed"
   - If the task fails, restart every: `10 minutes`
   - Attempt to restart up to: `3 times`

3. Click OK

### 7. Enter Password

- You'll be prompted to enter your Windows password
- This allows the task to run even when you're not logged in

## Verify Setup

### Test the Task

1. In Task Scheduler, find your task
2. Right-click → "Run"
3. Check `backend/backup.log` for results

### View Scheduled Runs

- In Task Scheduler, select your task
- Check the "History" tab to see past runs

## Troubleshooting

### Task doesn't run

- Check Task Scheduler → Task History for errors
- Verify Python is in system PATH
- Try running `run_backup.bat` manually first

### No log file created

- Check file permissions in backend folder
- Verify the "Start in" path is correct

### Backup fails

- Check `backend/backup.log` for error messages
- Verify database credentials in `.env`
- Verify R2 credentials in `.env`
- Test by running `python backup_db.py` manually

## Change Backup Schedule

1. Open Task Scheduler
2. Find "Recipe DB Weekly Backup"
3. Right-click → Properties
4. Go to Triggers tab
5. Edit the trigger to change day/time
6. Click OK

## Disable Automatic Backups

1. Open Task Scheduler
2. Find "Recipe DB Weekly Backup"
3. Right-click → Disable

## View Backup Log

Check `backend/backup.log` to see backup history:

```batch
cd backend
type backup.log
```

Or open it in Notepad.
