# Backup & Recovery Architecture Report (Consultation Portal)

---

## 1. Backup Structure

### 1.1 Button Stores (Dynamic Database Content inside the SQL file)
When clicked in the admin navbar by `madhavjoshi02@gmail.com`, the **Backup Database** button calls the server to trigger a `mysqldump` command and downloads a `.sql` database backup file:
* **User Accounts & Profiles**:
  * Admin, nutritionist, executive, and client data (names, emails, phones, dates of birth).
* **Consultation & Health Records**:
  * Detailed medical history, blood test metrics, lifestyle preferences, and customized client food plans.
* **Consent & Compliance Metrics (DPDP Act)**:
  * Verification logs confirming when and how consent was given (`consent_given` flags, consent IP addresses, and exact timestamps).
* **Audit Logs**:
  * Records of the last login activity of both clients and staff.

### 1.2 GitHub Stores (Static Application Code)
Your GitHub repository contains the structural code, dashboard interfaces, and backend server files. This includes:
* **Frontend Templates**: All administrative and dashboard HTML files (`public/*.html` such as `admin.html`, `client-food-plan.html`, etc.).
* **Styles & Designs**: Styling files inside `public/css/` (e.g., `admin_style.css`).
* **Interactive Code**: Client-side JavaScript logic inside `public/js/` (dashboard components, password resets, nutritionist/client dashboards).
* **Backend Server**: The Node.js ES modules (`server.mjs`, `adminRoutes.mjs`, `authRoutes.mjs`, `clientRoutes.mjs`, `staffRoutes.mjs`).
* **DB Schemes & Migrations**: Initialize scripts and database tables configuration files inside the `sql/` folder.

### 1.3 Not Covered (Excluded files to be handled manually)
These files are not stored in GitHub and are not exported by the navbar backup button:
* **`.env`**: Contains sensitive database passwords, Gmail SMTP email credentials (`EMAIL_USER`, `EMAIL_PASS`), and security tokens (`JWT_SECRET`).
* **Server Action Logs**: Daily application debug logs (`logs/` directory and root `app.log`).

> [!WARNING]
> **Setup Variables (`.env`) are Critical Data!**
> The `.env` file holds the database passwords and SMTP email credentials. Because it is excluded from GitHub and your Backup ZIP for security reasons, **you must back up and store these variables securely in a separate location** (like a password manager or secure notebook). Without these exact variables, the portal cannot connect to the database and will fail to start.

---

## 2. How to use this backup (Emergency Restoration Guide)

In the event of a server crash or database failure, follow these steps to restore the Consultation Portal:

```mermaid
graph TD
    A[Step 1: Clone from GitHub] --> B[Step 2: Recreate Database Schema]
    B --> C[Step 3: Import Backup SQL]
    C --> D[Step 4: Restore .env Config]
    D --> E[Step 5: Install & Start Portal]
```

### Step 1: Restore the Codebase from GitHub
Clone the repository to get the application layout and server engine:
```bash
git clone <your-consultation-repository-url>
cd consultation
```

### Step 2: Set Up a Fresh MySQL Database
Log into your database administrator tool (like phpMyAdmin or MySQL CLI) and create a fresh database matching your project name:
```sql
CREATE DATABASE consultation;
```

### Step 3: Import the SQL Backup File
Import the latest downloaded database backup (`consultation_backup_*.sql`) into your new database:
* **Option A (phpMyAdmin)**: Open phpMyAdmin, select the new `consultation` database, click the **Import** tab, upload the backup SQL file, and click **Go**.
* **Option B (Command Line)**:
  ```bash
  mysql -u <your_db_user> -p consultation < consultation_backup_XXXXXX.sql
  ```

### Step 4: Recreate Credentials (`.env`)
Create a new `.env` file in the root of the project directory and fill it with your database host, user, password, app URL, Gmail SMTP credentials, and JWT keys.

### Step 5: Install Dependencies & Start the Portal
Run the installer to retrieve required NPM packages and launch the server:
```bash
npm install
npm run server
```

---

## 3. Host-Level Automated Backups (Hostripples)
As configured in `BACKUP_LOG.md` and `BACKUP_SECURITY_LOG.md` for DPDP compliance:
* **Weekly Automated Backups**: Your web hosting provider (**Hostripples**) is verified to run weekly automated remote backups of the cPanel hosting account.
* **Storage**: These backups are stored securely on a separate remote server. They act as a full recovery fail-safe for the entire portal directory and database.
