feat: add database restore from uploaded SQL file with RESTORE confirmation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -63,4 +63,35 @@ class BackupController extends Controller
|
||||
if (!file_exists($path)) abort(404);
|
||||
return response()->download($path);
|
||||
}
|
||||
|
||||
public function restore(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'file' => 'required|file|mimes:sql,txt|max:51200',
|
||||
'confirm' => 'required|in:RESTORE',
|
||||
]);
|
||||
|
||||
$db = config('database.connections.mysql');
|
||||
$tmpPath = $request->file('file')->getRealPath();
|
||||
|
||||
$process = new Process([
|
||||
'mysql',
|
||||
'-h', $db['host'],
|
||||
'-P', (string) $db['port'],
|
||||
'-u', $db['username'],
|
||||
$db['database'],
|
||||
], null, ['MYSQL_PWD' => $db['password']]);
|
||||
|
||||
$process->setInput(file_get_contents($tmpPath));
|
||||
$process->setTimeout(300);
|
||||
$process->run();
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
ActivityLog::record('restore', 'Restore database GAGAL: ' . $process->getErrorOutput());
|
||||
return response()->json(['message' => 'Restore gagal: ' . $process->getErrorOutput()], 500);
|
||||
}
|
||||
|
||||
ActivityLog::record('restore', 'Restore database berhasil dari file yang diunggah');
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user