initial prototype based on nat-as-server
Some checks failed
PHPUnit / test (push) Has been cancelled
Some checks failed
PHPUnit / test (push) Has been cancelled
This commit is contained in:
33
handlers/backup.php
Normal file
33
handlers/backup.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
$tokenRec = require_valid_token_web();
|
||||
require_role(['admin'], $tokenRec);
|
||||
|
||||
if ($method !== 'POST') {
|
||||
json_error('METHOD_NOT_ALLOWED', 'Method not allowed', 405);
|
||||
}
|
||||
|
||||
$dbPath = QDB_PATH;
|
||||
$backupDir = QDB_UPLOADS_DIR . '/backups';
|
||||
|
||||
if (!file_exists($dbPath)) {
|
||||
json_error('NOT_FOUND', 'No database to back up', 404);
|
||||
}
|
||||
|
||||
if (!is_dir($backupDir)) {
|
||||
if (!mkdir($backupDir, 0755, true) && !is_dir($backupDir)) {
|
||||
json_error('SERVER_ERROR', 'Could not create backups directory', 500);
|
||||
}
|
||||
}
|
||||
|
||||
$timestamp = date('Y-m-d_H-i-s');
|
||||
$filename = "questionnaire_database.$timestamp";
|
||||
$backupFile = "$backupDir/$filename";
|
||||
|
||||
if (!copy($dbPath, $backupFile)) {
|
||||
json_error('SERVER_ERROR', 'Backup copy failed', 500);
|
||||
}
|
||||
|
||||
@chmod($backupFile, 0644);
|
||||
|
||||
json_success(['filename' => $filename]);
|
||||
Reference in New Issue
Block a user