43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
// /var/www/html/backup.php
|
|
// Admin-only endpoint: creates a timestamped backup of the encrypted database.
|
|
require_once __DIR__ . '/db_init.php';
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
$tokenRec = require_valid_token();
|
|
require_role(['admin'], $tokenRec);
|
|
|
|
$dbPath = QDB_PATH;
|
|
$backupDir = __DIR__ . '/uploads/backups';
|
|
|
|
if (!file_exists($dbPath)) {
|
|
http_response_code(404);
|
|
echo json_encode(["error" => "No database to back up"]);
|
|
exit;
|
|
}
|
|
|
|
if (!is_dir($backupDir)) {
|
|
if (!mkdir($backupDir, 0755, true) && !is_dir($backupDir)) {
|
|
http_response_code(500);
|
|
echo json_encode(["error" => "Could not create backups directory"]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$timestamp = date('Y-m-d_H-i-s');
|
|
$backupFile = $backupDir . "/questionnaire_database.$timestamp";
|
|
|
|
if (!copy($dbPath, $backupFile)) {
|
|
http_response_code(500);
|
|
echo json_encode(["error" => "Backup copy failed"]);
|
|
exit;
|
|
}
|
|
|
|
@chmod($backupFile, 0644);
|
|
|
|
echo json_encode([
|
|
"success" => true,
|
|
"message" => "Backup created",
|
|
"filename" => "questionnaire_database.$timestamp",
|
|
]);
|