How To take Backup of Laravel Application

Do you want to take or schedule a backup of Laravel application? Keeping the backup of Laravel database and filesystem always a good idea. You should have your backup ready anytime if something goes wrong with your server or application. In this article, we study how to take backup of Laravel application with the database.

If you are running a WordPress website, then there are plugins like Updraftplus which allows us to store backups on server or on cloud. But when it comes to Laravel, there is no concept like a plugin. In Laravel, we need to install libraries and configure it.

Spatie has developed a package called laravel-backup which is useful for storing Laravel backup. This package also provides an option which can schedule your backup process.

That being said, let’s take a look at how to take backup of Laravel application with the database.

Getting Started

The latest version of this package requires PHP 7.2 or higher with the ZIP module and Laravel 5.7 or higher. If you are using the old version of Laravel then version 5 and version 4 of this package available.

For this tutorial, we are using the latest version of this library. At first, you need to install the package by running the command below:

composer require spatie/laravel-backup

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

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

Head over to your config directory and open the backup.php in the editor.

<?php

return [

        .......

        'source' => [

            'files' => [

                /*
                 * The list of directories and files that will be included in the backup.
                 */
                'include' => [
                    base_path(),
                ],

                /*
                 * These directories and files will be excluded from the backup.
                 *
                 * Directories used by the backup process will automatically be excluded.
                 */
                'exclude' => [
                    base_path('vendor'),
                    base_path('node_modules'),
                ],

                /*
                 * Determines if symlinks should be followed.
                 */
                'followLinks' => false,
            ],

        .......

        'destination' => [

            /*
             * The filename prefix used for the backup zip file.
             */
            'filename_prefix' => 'GIVE_PREFIX_HERE',

            /*
             * The disk names on which the backups will be stored.
             */
            'disks' => [
                'local',
            ],
        ],
    ],

    ......

        /*
         * 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' => 'YOUR_EMAIL_ADDRESS',
        ],

    ......
        

In this file, you need to replace 2 placeholders GIVE_PREFIX_HERE and YOUR_EMAIL_ADDRESS. For the GIVE_PREFIX_HERE you can give any prefix like your project-name. This is just a prefix name for the zip file of backup.

A user can also include and exclude the directories for backup. For the include we have passed the value base_path() and for exclude option values are base_path(‘vendor’), base_path(‘node_modules’). It means don’t include vendor and nod_modules folders in the final backup.

We are also passing a disks value as ‘local’. Doing so, our backup will stored in the storage/app/Laravel folder. We can also store our backups to external storage such as s3, Rackspace, SFTP, etc. These external storage settings require some additional steps to configure with Laravel filesystem. We will cover more about this in another future article. If you are looking for external storage then please read the Laravel documentation.

Backup of Laravel Application

So far we are done with the installation and setup. Now We all set to run our first backup. Open the terminal in your project root directory and run the command:

php artisan backup:run

Above command would create a backup of Laravel application. You will find the zip of backup in the folder storage/app/Laravel. Backup zip contains your application files, folders, and database store in it.

This library also allows us to schedule a backup process. We can automate our backup task. For this, you should write below code in the app/Console/Kernel.php file.

protected function schedule(Schedule $schedule)
{
    $schedule->command('backup:run')->daily()->at('06:00');
}

We set daily backup at time 06:00. The user can change this time as per their requirement.

That’s it! It’s all about taking a backup of Laravel application. We highly recommend taking backup regularly and keep your application safe from any harmful circumstances.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “How To take Backup of Laravel Application

  1. Thank you for this article, please can you publish how you would do a full restore, including the database if disaster struck, or you wanted to get back to a certain build of laravel. This would be greatly appreciated

Leave a Reply

Your email address will not be published. Required fields are marked *