From 6e5c6eeeb86004c9235b02786697e0d359635b16 Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Tue, 3 Nov 2026 10:11:58 +0700 Subject: [PATCH] perf(backup,converter): optimize sql dump streaming, add direct server file conversion, and extend timeout to 30m --- .../Controllers/Admin/BackupController.php | 39 ++++--- app/Services/DatabaseBackupService.php | 98 +++++++++-------- app/Services/KmhConverterService.php | 102 ++++++++++++++++++ docker/nginx/default.conf | 12 +-- docker/php/local.ini | 4 +- resources/views/admin/backups/index.blade.php | 88 ++++++++++++++- tests/Feature/BackupAndRestoreTest.php | 29 +++++ 7 files changed, 305 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/Admin/BackupController.php b/app/Http/Controllers/Admin/BackupController.php index f104369..aa7ad89 100644 --- a/app/Http/Controllers/Admin/BackupController.php +++ b/app/Http/Controllers/Admin/BackupController.php @@ -434,21 +434,36 @@ class BackupController extends Controller public function converterInspect(Request $request): JsonResponse { $request->validate([ - 'backup_file' => 'required|file', + 'backup_file' => 'nullable|file', + 'server_filename' => 'nullable|string', ]); - $file = $request->file('backup_file'); - $originalName = $file->getClientOriginalName(); - - if (!str_ends_with(strtolower($originalName), '.kmh')) { - return response()->json([ - 'success' => false, - 'message' => 'Berkas yang dipilih harus berekstensi .kmh.', - ], 422); - } - try { - $metadata = $this->converterService->inspectUploadedFile($file); + if ($request->filled('server_filename')) { + $serverFile = $request->input('server_filename'); + if (!str_ends_with(strtolower($serverFile), '.kmh')) { + return response()->json([ + 'success' => false, + 'message' => 'Berkas yang dipilih harus berekstensi .kmh.', + ], 422); + } + $metadata = $this->converterService->inspectServerFile($serverFile); + } elseif ($request->hasFile('backup_file')) { + $file = $request->file('backup_file'); + $originalName = $file->getClientOriginalName(); + if (!str_ends_with(strtolower($originalName), '.kmh')) { + return response()->json([ + 'success' => false, + 'message' => 'Berkas yang dipilih harus berekstensi .kmh.', + ], 422); + } + $metadata = $this->converterService->inspectUploadedFile($file); + } else { + return response()->json([ + 'success' => false, + 'message' => 'Silakan pilih berkas .kmh dari komputer atau server.', + ], 422); + } return response()->json([ 'success' => true, diff --git a/app/Services/DatabaseBackupService.php b/app/Services/DatabaseBackupService.php index b8302f8..ac62168 100644 --- a/app/Services/DatabaseBackupService.php +++ b/app/Services/DatabaseBackupService.php @@ -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"); + } } /** diff --git a/app/Services/KmhConverterService.php b/app/Services/KmhConverterService.php index 652fca2..a4f2058 100644 --- a/app/Services/KmhConverterService.php +++ b/app/Services/KmhConverterService.php @@ -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, + * 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}"); diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf index 5ae15d8..9f38212 100644 --- a/docker/nginx/default.conf +++ b/docker/nginx/default.conf @@ -10,10 +10,10 @@ server { client_max_body_size 500M; client_body_buffer_size 1M; - client_body_timeout 600s; - client_header_timeout 600s; - send_timeout 600s; - keepalive_timeout 600s; + client_body_timeout 1800s; + client_header_timeout 1800s; + send_timeout 1800s; + keepalive_timeout 1800s; # Security Headers add_header X-Frame-Options "SAMEORIGIN" always; @@ -61,8 +61,8 @@ server { fastcgi_hide_header X-Powered-By; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; - fastcgi_read_timeout 600; - fastcgi_send_timeout 600; + fastcgi_read_timeout 1800; + fastcgi_send_timeout 1800; } location ~ /\.(?!well-known).* { diff --git a/docker/php/local.ini b/docker/php/local.ini index 7e4d0d3..7dfa37c 100644 --- a/docker/php/local.ini +++ b/docker/php/local.ini @@ -1,7 +1,7 @@ upload_max_filesize = 500M post_max_size = 500M memory_limit = 1024M -max_execution_time = 600 -max_input_time = 600 +max_execution_time = 1800 +max_input_time = 1800 date.timezone = Asia/Jakarta expose_php = Off diff --git a/resources/views/admin/backups/index.blade.php b/resources/views/admin/backups/index.blade.php index 31e480e..793f668 100644 --- a/resources/views/admin/backups/index.blade.php +++ b/resources/views/admin/backups/index.blade.php @@ -235,6 +235,13 @@ Unduh + +