Hi DigitalOcean Community,
I’m currently working on a Laravel application deployed on DigitalOcean’s App Platform. I need to implement a feature to export the database from my Laravel application and store the SQL dump file on DigitalOcean Spaces.
I’ve tried using the mysqldump
command in my application code, but I’m encountering issues because it’s not available in the App Platform environment. Can someone please guide me on how to export the database in Laravel on DigitalOcean App Platform without relying on mysqldump
?
Here’s what I’ve tried so far:
public function exportDatabase()
{
// Set the filename for the exported SQL file
$fileName = 'database_backup_' . date('YmdHis') . '.sql';
$filePath = 'database/' . $fileName;
// Execute mysqldump command to export the database
$dumpContent = $this->executeMysqldump();
// Save the output to a file
Storage::put($filePath, $dumpContent);
// Upload the SQL dump file to DigitalOcean Spaces
Storage::disk('digitalocean')->put('uploads/backup/' . $fileName, $dumpContent);
// Return the URL of the uploaded file
return Storage::url($filePath);
}
private function executeMysqldump()
{
// Execute mysqldump command to export the database
$process = new Process([
'mysqldump',
'-u' . env('DB_USERNAME'),
'-p' . env('DB_PASSWORD'),
env('DB_DATABASE'),
]);
$process->run();
// Check for errors during command execution
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Heya, @nathanmwamba
I’m glad you’ve sorted this!
snapshooter
is indeed a powerful tool which you can use for database, file backups and etc.Regards
Hey!
Have you considered using the
spatie/laravel-backup
package instead?You will be able to just use the
php artisan backup:run --only-db
command to backup your database directly.Also, there is a configuration setting that you set to define the destination disk which you can configure to be the DigitalOcean spaces:
That way you don’t have to rely on the
mysqldump
cli to be present on the App Platform.Let me know if this works for you!
Best,
Bobby