['file', '/dev/null', 'r'], 1 => ['file', $backupFile, 'wb'], 2 => ['pipe', 'w'], ]; $process = @proc_open($command, $descriptors, $pipes, null, $environment); if (!is_resource($process)) { @unlink($backupFile); json_error('SERVER_ERROR', 'MySQL backup failed to start mysqldump', 500); } $errorOutput = stream_get_contents($pipes[2]); fclose($pipes[2]); $code = proc_close($process); if ($code !== 0 || !is_file($backupFile) || filesize($backupFile) === 0) { @unlink($backupFile); $detail = trim((string)$errorOutput); json_error('SERVER_ERROR', 'MySQL backup failed' . ($detail !== '' ? ": {$detail}" : ''), 500); } @chmod($backupFile, 0600); json_success(['filename' => $filename]); } $dbPath = QDB_PATH; if (!file_exists($dbPath)) { json_error('NOT_FOUND', 'No database to back up', 404); } $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]);