How to Set up Automatic Laravel Backup for database and files?

Harish Kumar · · 9298 Views

Do you want to schedule an automatic backup of the Laravel application? Keeping the backup of the app's database and files very important. You should have your backup prepared whenever if something turns out badly with your server or application. In this article, you will learn how to take a backup of the Laravel application with the database and files.

The Spatie team has created a package called spatie/laravel-backup, which helps store your Laravel backup. The best thing about this package is that it gives an option that can schedule your backup process.

Let’s take a look at how to take the backup of the Laravel application with the database.


The current stable version of the spatie/laravel-backup package is v6.13. And this version requires PHP 7.3, with the ZIP module and Laravel 6.0 or higher.

If you are using an older version of Laravel, take a look at one of the previous versions of this package.

Installation & Setup

You need to run the following command in the terminal to install this package:

composer require spatie/laravel-backup

Once the package is installed, run the next below command that will publish the config file to config/backup.php.

php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"

In this configuration file, configure your options for backup. For example, you can include and exclude the directories for backup. By default include option as base_path() that is going to take the backup of entire Laravel files. And in the exclude option has base_path('vendor'), base_path('node_modules'). It means don’t add vendor and node_modules directory in the final backup.

In the databases option specify the names of the connections to the databases that should be backed up. That package supports MySQL, PostgreSQL, SQLite, and Mongo databases.

Next, the important option is disks. Here specify the disk names where the backups should be stored. By default here local disk is given that is going to store the back in the storage/app/ directory.

And also add the mail options, where it is going to send email backup notifications.

Taking backups

Now We are all set to run our first backup. Open the terminal in your project root directory and run the following command:

php artisan backup:run

Scheduling for automatic backup

 In most cases you'll want to schedule backup so you don't have to manually run the backup:run every time you need a new backup. You can implement this using the cron job.

To enable cron job for your project run the following command:

crontab -e

It will open the Crontab file. Now add the following entry to your server’s crontab file:

* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

Make sure to replace /path/to/artisan with your project's artisan path.

Now, we can automate our backup task. For this, you should write the below code in the app/Console/Kernel.php file.

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)

The times used in the code above are just for examples. You can adjust them according to your requirements.

That’s it! It’s all about taking a backup of the Laravel application.


Please login or create new account to add your comment.

mutaib ·

tarting backup... Dumping database rakaf... Backup failed because The dump process failed with exitcode 127 : Command not found : sh: mysqldump: command not found .

i am having latest laravel , but i faced an error?

Harish Kumar
Harish Kumar ·

I guess, you are getting this error in crontab. Follow this steps to fix this:

Step 1: find the location of mysqldump bin file. You can find it by executing following command in your terminal.

which mysqldump

Step 2: Open crontab using following command:

crontab -e

And add the following code in the beginning of the file:


for example let's say path of mysqldump is /usr/local/mysql/bin/mysqldump, then add following snippet:

You may also like:

How to Use DTOs for Cleaner Code in Laravel, Best Practices and Implementation Guide

When developing APIs in Laravel, ensuring your responses are clear, concise, and consistent is crucial for creating a maintainable and scalable application. One effective way to (...)
Harish Kumar

Data Type Validation in Laravel Collections with the `ensure()` Method

Before moving on to the ensure() method, let us first know what Laravel Collections actually are. These are wrappers of PHP arrays, offering a fluent and helpful interface in interacting (...)
Harish Kumar

PHP Generators: Efficient Data Handling and Iteration Techniques

PHP Generators offer a powerful and memory-efficient way to handle large datasets and complex iteration scenarios in your applications. They provide a more elegant solution compared (...)
Harish Kumar

Compress and Download Files in Laravel Using ZipArchive with Examples

In web development, file compression is essential for optimizing data transfer and storage. Laravel provides tools for creating and downloading compressed files. This guide explores (...)
Harish Kumar

Implementing Multi-Authentication with Guards in Laravel

This guide provides a detailed explanation of how to implement multi-authentication using guards in a Laravel application. This is useful for applications that need to support (...)
Harish Kumar

Building Real-Time Chat Applications with Laravel Reverb and Vue 3

Real-time chat applications are increasingly important for seamless communication and instant connectivity. Laravel Reverb and Vue.js 3 provide developers with the tools to create (...)
Harish Kumar