# Database Backup & Restore Guide

## Automated Backup (Recommended)

### XAMPP (Windows)

Run the backup script manually from the command line:

```
php C:\xampp\htdocs\AiWebReporter\cron\backup_run.php
```

To schedule daily backups, use Windows Task Scheduler:
1. Open Task Scheduler
2. Create Basic Task > Name: "AiWebReporter Backup"
3. Trigger: Daily at your preferred time
4. Action: Start a program
   - Program: `C:\xampp\php\php.exe`
   - Arguments: `C:\xampp\htdocs\AiWebReporter\cron\backup_run.php`
5. Finish

### cPanel (Linux)

Add a cron job in cPanel > Cron Jobs:

```
0 2 * * * /usr/local/bin/php /home/USERNAME/public_html/cron/backup_run.php
```

This runs daily at 2:00 AM. Replace `USERNAME` with your cPanel username.

## Backup Storage

- Backups are saved to `storage/backups/` as `.sql` files
- Filename format: `aiwr_backup_YYYY-MM-DD_HHMMSS.sql`
- Old backups are automatically deleted after the retention period (default: 30 days)
- Backup log: `storage/backups/backup.log`
- The backup directory is outside the public web root and not directly accessible via URL

## Backup Methods

The system tries `mysqldump` first (faster, more reliable), then falls back to a PHP-PDO export if `mysqldump` is unavailable.

- **XAMPP**: `mysqldump` is typically at `C:\xampp\mysql\bin\mysqldump.exe`
- **cPanel**: `mysqldump` is at `/usr/bin/mysqldump`

## Manual Backup via phpMyAdmin

1. Open phpMyAdmin (XAMPP: `http://localhost/phpmyadmin`, cPanel: via cPanel dashboard)
2. Select the `aiwebreporter` database (or your database name from `.env`)
3. Click **Export** tab
4. Method: **Quick** (or Custom for more control)
5. Format: **SQL**
6. Click **Go** to download the `.sql` file
7. Store the file safely outside the web-accessible directory

## Restore Procedure

### From Command Line (mysqldump backup)

**XAMPP:**
```
C:\xampp\mysql\bin\mysql.exe -u root -p aiwebreporter < C:\xampp\htdocs\AiWebReporter\storage\backups\aiwr_backup_YYYY-MM-DD_HHMMSS.sql
```

**cPanel/Linux:**
```
mysql -u DB_USER -p DB_NAME < /home/USERNAME/public_html/storage/backups/aiwr_backup_YYYY-MM-DD_HHMMSS.sql
```

Replace `DB_USER`, `DB_NAME`, and the file path with your actual values from `.env`.

### From phpMyAdmin

1. Open phpMyAdmin
2. Select the target database
3. Click **Import** tab
4. Choose the `.sql` backup file
5. Click **Go**

### Important Notes

- Always back up the current database before restoring an older backup
- The restore will replace all existing data in the database
- After restoring, verify the site works correctly
- If restoring to a different server, update `.env` with the new database credentials
- The `storage/backups/` directory should never be web-accessible. Verify with: `http://yoursite.com/storage/backups/` should return 403 or 404

## Configuration

Backup settings in `.env`:

```
BACKUP_DIR=          # Custom backup directory (default: storage/backups/)
BACKUP_RETENTION=30  # Days to keep old backups
```

## Troubleshooting

- **Backup fails silently**: Check `storage/logs/app-*.log` for errors
- **mysqldump not found**: The PHP fallback will be used automatically. Check `storage/backups/backup.log` for the method used
- **Permission denied**: Ensure `storage/backups/` is writable by the web server user
- **Large database timeout**: For very large databases, run the backup from CLI rather than the web interface
