perf(backup,converter): optimize sql dump streaming, add direct server file conversion, and extend timeout to 30m

This commit is contained in:
Ray1111-aa
2026-11-03 10:11:58 +07:00
parent ff3ab616e0
commit 6e5c6eeeb8
7 changed files with 305 additions and 67 deletions
+55 -43
View File
@@ -149,8 +149,8 @@ class DatabaseBackupService
*/
public function createKmhBackup(?array $selectedTables = null, bool $includeMedia = false, ?string $customFilename = null): string
{
@set_time_limit(600);
@ini_set('memory_limit', '512M');
@set_time_limit(1800);
@ini_set('memory_limit', '1024M');
if (!class_exists('ZipArchive')) {
throw new RuntimeException('Ekstensi PHP ZipArchive diperlukan untuk memaketkan arsip.');
@@ -242,8 +242,8 @@ class DatabaseBackupService
*/
public function restore(string $backupFilenameOrPath): void
{
@set_time_limit(600);
@ini_set('memory_limit', '512M');
@set_time_limit(1800);
@ini_set('memory_limit', '1024M');
$filePath = File::exists($backupFilenameOrPath)
? $backupFilenameOrPath
@@ -674,55 +674,67 @@ class DatabaseBackupService
fwrite($handle, "-- Dumping Data untuk `{$tableName}`\n");
$query = DB::table($tableName);
// Pastikan data yang berada di tong sampah (soft-deleted) tidak bocor ke dalam data backup
$sql = "SELECT * FROM `{$tableName}`";
if (Schema::hasColumn($tableName, 'deleted_at')) {
$query->whereNull('deleted_at');
$sql .= " WHERE `deleted_at` IS NULL";
}
$totalRows = $query->count();
try {
$stmt = $pdo->query($sql);
} catch (\Throwable $e) {
$stmt = null;
}
if ($totalRows === 0) {
fwrite($handle, "-- (Tabel kosong)\n\n");
if (!$stmt) {
fwrite($handle, "-- (Tabel kosong atau gagal dibaca)\n\n");
return;
}
$query->orderBy(DB::raw('1'))->chunk(200, function ($rows) use ($handle, $pdo, $tableName) {
if ($rows->isEmpty()) return;
$batch = [];
$batchSize = 500;
$columnsStr = null;
$hasData = false;
$columns = array_keys((array) $rows->first());
$escapedColumns = array_map(fn ($c) => "`{$c}`", $columns);
$columnsStr = implode(', ', $escapedColumns);
fwrite($handle, "INSERT INTO `{$tableName}` ({$columnsStr}) VALUES\n");
$rowCount = count($rows);
$i = 0;
foreach ($rows as $row) {
$i++;
$values = [];
foreach ((array) $row as $val) {
if (is_null($val)) {
$values[] = 'NULL';
} elseif (is_int($val) || is_float($val)) {
$values[] = (string) $val;
} elseif (is_bool($val)) {
$values[] = $val ? '1' : '0';
} else {
$values[] = $pdo->quote((string) $val);
}
}
$line = ' (' . implode(', ', $values) . ')';
$line .= ($i === $rowCount) ? ";\n" : ",\n";
fwrite($handle, $line);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$hasData = true;
if ($columnsStr === null) {
$columns = array_keys($row);
$escapedColumns = array_map(fn ($c) => "`{$c}`", $columns);
$columnsStr = implode(', ', $escapedColumns);
}
});
fwrite($handle, "\n");
$values = [];
foreach ($row as $val) {
if (is_null($val)) {
$values[] = 'NULL';
} elseif (is_int($val) || is_float($val)) {
$values[] = (string) $val;
} elseif (is_bool($val)) {
$values[] = $val ? '1' : '0';
} else {
$values[] = $pdo->quote((string) $val);
}
}
$batch[] = ' (' . implode(', ', $values) . ')';
if (count($batch) >= $batchSize) {
fwrite($handle, "INSERT INTO `{$tableName}` ({$columnsStr}) VALUES\n");
fwrite($handle, implode(",\n", $batch) . ";\n");
$batch = [];
}
}
if (!empty($batch)) {
fwrite($handle, "INSERT INTO `{$tableName}` ({$columnsStr}) VALUES\n");
fwrite($handle, implode(",\n", $batch) . ";\n");
}
if (!$hasData) {
fwrite($handle, "-- (Tabel kosong)\n\n");
} else {
fwrite($handle, "\n");
}
}
/**
+102
View File
@@ -14,6 +14,102 @@ class KmhConverterService
protected DatabaseBackupService $backupService
) {}
/**
* Periksa metadata dan daftar tabel dari berkas .kmh yang sudah tersimpan di server.
* Tidak memerlukan pengunggahan ulang berkas dari PC (sangat cepat & ramah kuota).
*
* @return array{
* token: string,
* filename: string,
* size_human: string,
* created_at_human: string,
* created_at_diff: string,
* table_count: int,
* tables: array<string>,
* database: string,
* has_media: bool
* }
*/
public function inspectServerFile(string $filename): array
{
@set_time_limit(1800);
@ini_set('memory_limit', '1024M');
$backupDir = $this->backupService->getBackupDirectory();
$realPath = $backupDir . DIRECTORY_SEPARATOR . basename($filename);
if (!is_file($realPath)) {
throw new RuntimeException("Berkas arsip '{$filename}' tidak ditemukan di penyimpanan server.");
}
$kmhContent = file_get_contents($realPath);
if ($kmhContent === false) {
throw new RuntimeException("Gagal membaca berkas .kmh dari server.");
}
$decryptedZipContent = $this->backupService->decryptFromKmh($kmhContent);
unset($kmhContent);
$tempZipPath = storage_path('app/temp_inspect_' . uniqid() . '.zip');
file_put_contents($tempZipPath, $decryptedZipContent);
unset($decryptedZipContent);
$zip = new ZipArchive();
if ($zip->open($tempZipPath) !== true) {
@unlink($tempZipPath);
throw new RuntimeException("Konten terdekripsi bukan merupakan arsip ZIP yang sah.");
}
try {
$manifestJson = $zip->getFromName('manifest.json');
$manifest = $manifestJson ? json_decode($manifestJson, true) : [];
$tables = $manifest['tables'] ?? [];
if (empty($tables)) {
$sqlContent = $zip->getFromName('database.sql');
if ($sqlContent) {
preg_match_all('/CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?`?([a-zA-Z0-9_]+)`?/i', $sqlContent, $matches);
$tables = array_values(array_unique($matches[1] ?? []));
}
}
sort($tables);
$createdAt = isset($manifest['created_at'])
? Carbon::parse($manifest['created_at'])
: Carbon::createFromTimestamp(filemtime($realPath));
$tempDir = storage_path('app/temp_converter');
File::ensureDirectoryExists($tempDir);
// Bersihkan file sementara yang lebih tua dari 30 menit
foreach (File::files($tempDir) as $f) {
if ((time() - $f->getMTime()) > 1800) {
@unlink($f->getPathname());
}
}
$token = bin2hex(random_bytes(16));
$cachedKmhPath = $tempDir . '/' . $token . '.kmh';
copy($realPath, $cachedKmhPath);
return [
'token' => $token,
'filename' => basename($filename),
'size_human' => $this->backupService->formatBytes((int) filesize($realPath)),
'created_at_human' => $createdAt->translatedFormat('d F Y, H:i') . ' WIB',
'created_at_diff' => $createdAt->diffForHumans(),
'table_count' => count($tables),
'tables' => $tables,
'database' => $manifest['database'] ?? config('database.connections.' . config('database.default') . '.database', 'rkm_kmh'),
'has_media' => (bool) ($manifest['has_media'] ?? false),
];
} finally {
$zip->close();
@unlink($tempZipPath);
}
}
/**
* Periksa metadata dan daftar tabel dari berkas .kmh yang diunggah dari PC.
* Menyimpan berkas sementara dengan token aman agar saat klik konversi tidak perlu upload ulang 2x.
@@ -32,6 +128,9 @@ class KmhConverterService
*/
public function inspectUploadedFile(UploadedFile $file): array
{
@set_time_limit(1800);
@ini_set('memory_limit', '1024M');
$realPath = $file->getRealPath();
if (!$realPath || !is_file($realPath)) {
throw new RuntimeException("Berkas unggahan tidak ditemukan atau korup.");
@@ -160,6 +259,9 @@ class KmhConverterService
*/
public function convertKmhFilePath(string $kmhFilePath, string $originalFilename, array $selectedTables, string $format): array
{
@set_time_limit(1800);
@ini_set('memory_limit', '1024M');
$format = strtolower($format);
if (!in_array($format, ['xlsx', 'csv', 'sql', 'media'], true)) {
throw new RuntimeException("Format konversi tidak didukung: {$format}");