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

Harish Kumar · · 633 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.

You may also like:

What are Laravel Macros and How to Extending Laravel’s Core Classes using Macros with example?

Laravel Macros are a great way of expanding Laravel's core macroable classes and add additional functionality needed for your application. In simple word, Laravel Macro is an (...)
Harish Kumar

Install Laravel Valet Linux+ development environment on Ubuntu System

The official Laravel Valet development environment is great if you are an Apple user. But there is no official Valet for Linux or Window system.
Harish Kumar

Laravel Sanctum API Token Authentication Tutorial with example

Laravel Sanctum is a popular package for API Token Authentication. There are many other packages available to authenticate the APIs request in Laravel. For example, We are already (...)
Harish Kumar

Create SPA authentication Using Laravel Sanctum and Vue.js

In this guide, we will focus on SPA authentication in a simple Vue.js app using Laravel Sanctum. Laravel Sanctum provides a featherweight authentication system for SPAs (single (...)
Harish Kumar

Create API Authentication with Laravel Passport

In this article, we'll see how to implement restful API authentication using Laravel Passport. You should have experience working with Laravel as this is not an introductory tutorial. (...)
Sumit Talwar

Laravel Themer: multi-theme support for Laravel application

This Laravel Themer package adds multi-theme support to your application. This theme package improves any application while allowing the freedom to organize and maintain your app's (...)
Harish Kumar