How To take Backup of Laravel Application

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

The team Spatie has developed a package called “laravel-backup” useful for creating a backup of your Laravel application. This package also provides an option to schedule your backup automatically.

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

Getting Started

At the time of writing, the latest version of this package requires PHP 8.0 with the ZIP module and Laravel 9.0 or higher.

Note : This package is not compatible with Windows servers.

For this tutorial, I am using the latest version of this library. If you are using the older version of Laravel then you can check out a former version of this package from the documentation. On respective pages, you’ll find the instructions accordingly. 

At first, install the package using the command:

composer require spatie/laravel-backup

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

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

Head over to your project directory and open the config/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, add the values for 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 name append to 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 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’. It will store the backup inside the storage/app/Laravel directory. You can also store the backups to external storage such as s3, Rackspace, SFTP, etc. These external storage settings require some additional steps to configure with the Laravel filesystem. If you are looking for external storage like S3 then check out our article – Store Laravel Backup on Amazon S3.

Backup of Laravel Application

So far we are done with the package installation and setup. Now, let’s run our first backup. Open the terminal in the project root directory and run the command:

php artisan backup:run

The above command would create a backup of the Laravel application. You will find the zip of backup under the storage/app/Laravel directory. The backup zip contains your application files, folders, and database stored in it.

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

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

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

That’s it! It’s all about taking a backup of a Laravel application. I highly recommend taking backup regularly and keeping 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.