How to backup MySQL Database
Hey guys how to backup(download) MySQL Database as file [ .sql ] ?
I mean using PHP in laravel for example when button clicked download database backup.
You can exec() a command to your OS to mysqldump the file... it could take a minute or so to run so this might be a use case for a queued job. Once it is done, you can move the file to a location that is web accessible and send the user a link to that file. Just realize that if you make it web accessible, someone you don't want to have that file might find it.
Looks like there are a few packages to do that.
Yep, here's a good one:
@nate.a.johnson Thanks
I'm using Laravel Backup & Backup Manger on a project.. works great.

My relevant controller:
<?php
namespace App\Http\Controllers;
use Alert;
use App\Http\Requests;
use Artisan;
use Log;
use Storage;
class BackupController extends Controller
{
public function index()
{
$disk = Storage::disk(config('laravel-backup.backup.destination.disks')[0]);
$files = $disk->files(config('laravel-backup.backup.name'));
$backups = [];
// make an array of backup files, with their filesize and creation date
foreach ($files as $k => $f) {
// only take the zip files into account
if (substr($f, -4) == '.zip' && $disk->exists($f)) {
$backups[] = [
'file_path' => $f,
'file_name' => str_replace(config('laravel-backup.backup.name') . '/', '', $f),
'file_size' => $disk->size($f),
'last_modified' => $disk->lastModified($f),
];
}
}
// reverse the backups, so the newest one would be on top
$backups = array_reverse($backups);
return view("backup.backups")->with(compact('backups'));
}
public function create()
{
try {
// start the backup process
Artisan::call('backup:run');
$output = Artisan::output();
// log the results
Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n" . $output);
// return the results as a response to the ajax call
Alert::success('New backup created');
return redirect()->back();
} catch (Exception $e) {
Flash::error($e->getMessage());
return redirect()->back();
}
}
/**
* Downloads a backup zip file.
*
* TODO: make it work no matter the flysystem driver (S3 Bucket, etc).
*/
public function download($file_name)
{
$file = config('laravel-backup.backup.name') . '/' . $file_name;
$disk = Storage::disk(config('laravel-backup.backup.destination.disks')[0]);
if ($disk->exists($file)) {
$fs = Storage::disk(config('laravel-backup.backup.destination.disks')[0])->getDriver();
$stream = $fs->readStream($file);
return \Response::stream(function () use ($stream) {
fpassthru($stream);
}, 200, [
"Content-Type" => $fs->getMimetype($file),
"Content-Length" => $fs->getSize($file),
"Content-disposition" => "attachment; filename=\"" . basename($file) . "\"",
]);
} else {
abort(404, "The backup file doesn't exist.");
}
}
/**
* Deletes a backup file.
*/
public function delete($file_name)
{
$disk = Storage::disk(config('laravel-backup.backup.destination.disks')[0]);
if ($disk->exists(config('laravel-backup.backup.name') . '/' . $file_name)) {
$disk->delete(config('laravel-backup.backup.name') . '/' . $file_name);
return redirect()->back();
} else {
abort(404, "The backup file doesn't exist.");
}
}
}
It automatically creates backups via cron, and I can also manually create backups on my backup page.. and the two packages take care of all the heavy lifting, clearing out old backups, etc.. It's pretty slick.
I need to see how you list in your blade, can you show me?
Open a new thread and ask your question instead of highjacking a thread that has been solved over four years ago.
Please or to participate in this conversation.