# Backup, Disaster Recovery, and Business Continuity Plan
## Kampong Speu Provincial Demographic Management System (KSP-DMS)

**Document Code:** KSP-DMS-DOC-BCP-01  
**Target Architecture:** On-Premises Local Provincial Server  
**Version:** 1.0 (Draft Proposal)  

---

## 1. Objectives and Recovery Metrics

This plan establishes operational procedures to guarantee zero permanent demographic data loss, rapid recovery from hardware or power failure, and business continuity for the Kampong Speu Provincial Administration.

### Key Recovery Metrics
- **Recovery Point Objective (RPO):** < 24 hours (Maximum acceptable data loss window; zero data loss during business hours via transaction log archiving).
- **Recovery Time Objective (RTO):** < 2 hours (Maximum acceptable duration to restore full operational capability on spare hardware).

---

## 2. Backup Strategy: The 3-2-1 Rule

KSP-DMS adheres to the industry-standard 3-2-1 backup methodology tailored to provincial administrative conditions:
1. **3 Copies of Data:**
   - Copy 1: Primary production database on the local server.
   - Copy 2: Local secondary storage media (dedicated internal backup SSD).
   - Copy 3: Physical external USB storage rotated off-site.
2. **2 Different Storage Media:** High-speed NVMe/SSD for daily dumps + encrypted external physical drives.
3. **1 Off-Site Copy:** Rotated weekly to a secure, climate-controlled provincial archive facility.

---

## 3. Automated Daily Backup Script (Windows PowerShell)

```powershell
# ksp-dms-backup.ps1 - Automated Daily Database & Asset Backup
$BackupDir = "C:\ksp-dms-backups"
$Timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$DbName = "ksp_dms"
$DumpFile = "$BackupDir\ksp_dms_db_$Timestamp.sql"
$ZipFile = "$BackupDir\ksp_dms_backup_$Timestamp.zip"

# Create backup directory if missing
if (-not (Test-Path $BackupDir)) { New-Item -ItemType Directory -Path $BackupDir }

Write-Host "Exporting MariaDB database..."
& "C:\xampp\mysql\bin\mysqldump.exe" --user=root --single-transaction --quick --routines --triggers $DbName > $DumpFile

Write-Host "Compressing database dump and environment configuration..."
Compress-Archive -Path $DumpFile, "C:\xampp\htdocs\ksp-dms\.env" -DestinationPath $ZipFile -Force
Remove-Item $DumpFile

# Maintain retention: Delete backups older than 30 days
Get-ChildItem -Path $BackupDir -Filter "*.zip" | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-30) } | Remove-Item

Write-Host "Backup successfully completed: $ZipFile"
```

---

## 4. Disaster Recovery Procedure (Step-by-Step)

In the event of total server drive failure:
1. **Hardware Preparation:** Provision replacement Windows 11 / Server machine with standard XAMPP environment.
2. **Repository Deployment:** Clone the private `ksp-dms` repository to `C:\xampp\htdocs\ksp-dms`.
3. **Database Restoration:**
   ```powershell
   mysql -u root -e "CREATE DATABASE IF NOT EXISTS ksp_dms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
   mysql -u root ksp_dms < C:\ksp-dms-backups\ksp_dms_db_YYYYMMDD.sql
   ```
4. **Environment Restoration:** Restore `.env` configuration file containing application secret keys.
5. **Asset Linking & Cache Clear:**
   ```powershell
   php artisan config:clear
   php artisan cache:clear
   php artisan storage:link
   ```
6. **Integrity Verification:** Execute automated smoke tests (`php artisan test --filter=SmokeTest`) to verify system health before resuming live service.
