feat: add database backup download via mysqldump (admin-only)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\ActivityLog;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class BackupController extends Controller
|
||||
{
|
||||
public function download()
|
||||
{
|
||||
$db = config('database.connections.mysql');
|
||||
|
||||
$process = new Process([
|
||||
'mysqldump',
|
||||
'-h', $db['host'],
|
||||
'-P', (string) $db['port'],
|
||||
'-u', $db['username'],
|
||||
'--single-transaction',
|
||||
'--skip-lock-tables',
|
||||
'--no-tablespaces',
|
||||
$db['database'],
|
||||
], null, ['MYSQL_PWD' => $db['password']]);
|
||||
|
||||
$process->setTimeout(120);
|
||||
$process->run();
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
ActivityLog::record('backup', 'Gagal membuat backup database');
|
||||
return response()->json(['message' => 'Backup gagal: ' . $process->getErrorOutput()], 500);
|
||||
}
|
||||
|
||||
ActivityLog::record('backup', 'Mengunduh backup database');
|
||||
|
||||
$filename = 'backup-webgis-' . now()->format('Ymd-His') . '.sql';
|
||||
return response($process->getOutput(), 200, [
|
||||
'Content-Type' => 'application/sql',
|
||||
'Content-Disposition' => "attachment; filename=\"{$filename}\"",
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user