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

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

Requirements

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)
{
   $schedule->command('backup:clean')->daily()->at('01:00');
   $schedule->command('backup:run')->daily()->at('02:00');
}

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.

2

Please login or create new account to add your comment.

2 comments
mutaib
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:

#!/bin/sh
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:<path-of-mysqldump>

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

#!/bin/sh
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin
You may also like:

Part #3: Rule objects based custom validation in Laravel

Laravel comes with multiple ways to add custom validation rules to validate form request inputs. I have already explained some of the ways in the following article links:
Harish Kumar

Part #2: How to use Laravel's Validator::extend method for custom validation

Validation is important in any application as it validates a form before performing actions on it. It allows the user to know their input is accurate and confident about the operation (...)
Harish Kumar

Part #1: Closure-based Custom Laravel Validation

While I was working with Laravel, validation using closure came to my mind, and I know it will be helpful to you. This tutorial assists you with all what is the difference between (...)
Harish Kumar

How to use the enumerations(Enums) of PHP 8.1 in Laravel?

The release of PHP 8.1 brings native enumerations to PHP. There is no more requirement for custom solutions in your Laravel projects since the Laravel v8.69 release has you back. (...)
Harish Kumar

Mobile App Development Process

With businesses adopting a mobile-first approach and the growing number of mobile apps, successful mobile app development seems like a quest. But it’s the process that determines (...)
Narola Infotech

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