Report this

What is the reason for this report?

How to Export Database in Laravel on DigitalOcean App Platform?

Posted on April 18, 2024

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.

Hey!

Have you considered using the spatie/laravel-backup package instead?

https://spatie.be/docs/laravel-backup/v8/taking-backups/overview

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:

     'destination' => [

         /*
          * The disk names on which the backups will be stored.
          */
         'disks' => [
             'local',
         ],
     ],

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

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.