user()->can('system.manage'), 403); $path = config('database.connections.sqlite.database'); abort_unless($path && is_file($path), 404, 'No database file to back up.'); $audit->log($request->user(), 'system.backup_downloaded'); return response()->download($path, 'backup-'.now()->format('Y-m-d-His').'.sqlite'); } public function restore(Request $request, AuditLogger $audit) { abort_unless($request->user()->can('system.manage'), 403); $request->validate(['backup' => ['required', 'file']]); $uploaded = $request->file('backup'); $header = file_get_contents($uploaded->getRealPath(), false, null, 0, 16); if ($header !== self::SQLITE_HEADER) { throw ValidationException::withMessages([ 'backup' => 'That file is not a valid SQLite database.', ]); } $path = config('database.connections.sqlite.database'); // Release Laravel's open handle on the current file before swapping it. DB::disconnect(); $safetyCopy = dirname($path).'/pre-restore-'.now()->format('Y-m-d-His').'.sqlite'; if (is_file($path)) { copy($path, $safetyCopy); } $uploaded->move(dirname($path), basename($path)); $audit->log($request->user(), 'system.backup_restored'); return response()->noContent(); } }