Laravel: How to backup a database


Unfortunately, many new Laravel developers don’t realize this until they put their code in production, and something happens so they lose their data.

Laravel does not offer a database backup as part of their core functionality so new Laravel developers don’t get to work with that while reading Laravel articles or building a Laravel application.

Today, it becomes an increasingly important thing to store and preserve your backup data in case you ever need to use it.

Common mistakes new Laravel developers do is to go to phpmyadmin and try to run this export manually. While this will still work, it is not the best practice because sometimes you won’t be there to make the backup, and your users already generated new data.

What you want to do is to automate that.


There are many ways that you can do to backup your database but how many people do is using this package . The company behind it is very reliable and known in the industry.

First of all, the package is FREE, so you don’t have to pay anything.

On the plus side, the installation is quite simple and what I like in particular is that it not only does your database backup, you also have an option of backing up your files and then storing all of that in a zip file. When I say files, I mean storage files, such as the document uploads or files that you generate with your application.

In addition, you can also move that to AWS S3 which is a smart thing to do. All you need to do is (assuming you already have an AWS account, is to go and create a new S3 bucket with new permissions. Then you can just follow the documentation on their Github page.

Here is a quick preview of the config file that you can tweak so you receive notifications through other mediums such as Slack and Email.

‘notifications’ => [

    ‘notifications’ => [
        \Spatie\Backup\Notifications\Notifications\BackupHasFailed::class         => [mail, ‘slack’],
        \Spatie\Backup\Notifications\Notifications\UnhealthyBackupWasFound::class => [mail, ‘slack’],
        \Spatie\Backup\Notifications\Notifications\CleanupHasFailed::class        => [mail, ‘slack’],
        \Spatie\Backup\Notifications\Notifications\BackupWasSuccessful::class     => [mail, ‘slack’],
        \Spatie\Backup\Notifications\Notifications\HealthyBackupWasFound::class   => [mail, ‘slack’],
        \Spatie\Backup\Notifications\Notifications\CleanupWasSuccessful::class    => [mail, ‘slack’],

     * Here you can specify the notifiable to which the notifications should be sent. The default
     * notifiable will use the variables specified in this config file.

    ‘notifiable’ => \Spatie\Backup\Notifications\Notifiable::class,

    ‘mail=> [
        ‘to’ => [‘’],

    ‘slack’ => [
        ‘webhook_url’ => ‘’,

         * If this is set to null the default channel of the webhook will be used.

        ‘channel’ => null,

Set up automation

Finally, all of this wouldn’t make much sense if you decided to skip the automation part. For this, you want to rely on cron jobs that should already be enabled on your host and pointer to the Laravel schedule command like this:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

From here, in your app/Console/Kernel.php file you should do the following if you want to automate it at a particular time (I usually do this when most of our users are asleep):

if (app(‘env’) == ‘production’) {

About the author