Last active
January 21, 2023 01:33
-
-
Save ManiruzzamanAkash/987f5080fed7ced357c9f3d93b0a355f to your computer and use it in GitHub Desktop.
DB backup controller laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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