Skip to content

Instantly share code, notes, and snippets.

@ManiruzzamanAkash
Last active January 21, 2023 01:33
Show Gist options
  • Save ManiruzzamanAkash/987f5080fed7ced357c9f3d93b0a355f to your computer and use it in GitHub Desktop.
Save ManiruzzamanAkash/987f5080fed7ced357c9f3d93b0a355f to your computer and use it in GitHub Desktop.
DB backup controller laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BackupController extends Controller
{
public function exportDB(Request $request) {
$request->validate([
'password' => 'required|string|min:15|max:15'
]);
if ($request->password !== 'astrongpassword') {
abort(403, "Sorry, You're not authorized to do this.");
return;
}
$db_name = env('DB_DATABASE');
$db_user = env('DB_USERNAME');
$db_password = env('DB_PASSWORD');
$today = date('Y-m-d');
$command = "mysqldump --user={$db_user} --password={$db_password} {$db_name} > {$db_name}-{$today}.sql";
exec($command);
return response()->download("{$db_name}-{$today}.sql")->deleteFileAfterSend(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment