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

Harish Kumar · · 8830 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:

Laravel Facades: Simplifying Code and Improve Readability

As an integral part of Laravel, a renowned PHP framework, Facades provide a static interface to classes stored in the application's service container. They serve as static proxies (...)
Harish Kumar

What is Laravel’s Service Container and How to Use Dependency Injection in Laravel App

Dependency injection and inversion of control are vital in clean web development. They make writing maintainable, testable code possible. Laravel is a famous PHP framework that (...)
Harish Kumar

Secure Your SPA with Laravel Sanctum: A Step-by-Step Guide

In today's web development landscape, Single Page Applications (SPAs) are increasingly popular. But securing their interaction with backend APIs is crucial. Laravel Sanctum provides (...)
Harish Kumar

Multi-Authentication with Guards in Laravel

Laravel's robust authentication system provides a powerful mechanism for securing your application. To cater to scenarios where you need different user roles with distinct login (...)
Harish Kumar

Laravel Pint & VS Code: Automate Your Code Formatting

Laravel Pint is an opinionated PHP code style fixer built on top of PHP-CS-Fixer, designed to simplify the process of ensuring clean and consistent code style in Laravel projects. (...)
Harish Kumar

Laravel Clockwork: A Deep Dive into Debugging, Profiling Skills and Best Practices

In the world of web development, building complex applications often comes with the challenge of identifying and resolving performance bottlenecks. This is where a reliable debugging (...)
Harish Kumar