Question

How to Export Database in Laravel on DigitalOcean App Platform?

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();
}

Submit an answer


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!

Sign In or Sign Up to Answer

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.

alexdo
Site Moderator
Site Moderator badge
April 22, 2024

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

Bobby Iliev
Site Moderator
Site Moderator badge
April 21, 2024

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

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.