Laravel

How to Backup Laravel Files and Database

February 13, 2018

author:

How to Backup Laravel Files and Database

When an application scales or gets complex, it becomes important to keep track of changes. Also, with the application in development, you always need to be on your toes to fix the bugs, handle unexpected situations like server downtime or database/file corrupt/delete. Such situations are very crucial for any business. Thus it is necessary to backup the database and application files on a regular basis. Let us now see how to create database backup in Laravel 5 applications:

# Laravel-backup Package Setup

Fortunately, Backups in Laravel can be pretty straightforward. Laravel community has awesome developers who provide a solution to various problems. For backup, we will use such third party library from spatie called laravel-backup. It can backup both files and database and store in a .zip file. This package can also send in email or slack notification when the problem arises with backups.

To use the latest package with its features, here are some basic requirements:

  • PHP 7 or higher
  • Laravel 5.5 or higher
  • package : “guzzlehttp/guzzle”: “^6.2”
  • Enough space to store backup

But if your application doesn’t fulfill these requirements, don’t worry, you just need to use a lower version of this package.

Install the package in your Laravel application:

composer require spatie/laravel-backup
Install laravel-backup package

In Laravel 5.5+, Service Providers are registered automatically, for other versions, you may need to add them manually.

# Backup Configuration

Now, to actually start working with backup, we need first to configure it. And for that we need to publish this package’s vendor file:

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

OR

php artisan vendor:publish
command to publish file in laravel

As you would have noticed, it publishes a backup.php file to config/backup.php namespace. Open this file and have a good look at available settings that you can apply to backup your Laravel application.

You can settings in this file to suit your application, here are some basic changes that you would need to change:

First of all, change the name attribute in the backup.php file at 2 places:

# config/backup.php

<?php

return [

    'backup' => [
        ...
        'name' => env('APP_URL'),
        ...
    ],
    ...
    'monitorBackups' => [
     [
         'name' => env('APP_URL'),
         ..
     ],
]

Next, change the mail attribute to your application email id:

# config/backup.php

<?php

return [
    ...
    'notifications' => [
        'notifications' => [
            ...
        ],
        ...
        'mail' => [
            'to' => 'mail@hashvel.com',
        ],
        ...
    ],
]

Also, set the APP_URL property in .env file of your application:

# .env
...
APP_URL=http://localhost
...

That’s it! Easy, isn’t it?

# Take Backup

Let us get our hands dirty now that we have configured everything.

This package provides numerous commands, let us try and test few of them:

First of all, check the list of available commands:

php artisan list
view backup commands
  • To backup all the files and database:

php artisan backup:run
laravel-backup run command
  • To backup only database:

php artisan backup:run --only-db
laravel backup only database
  • To backup application files only:

php artisan backup:run --only-files
laravel backup only files
  • To track the status of monitored backups:

php artisan backup:list
check running backups

These backups are stored in storage directory:
laravel-backup storage directory

# Automatic Database Backup

When you already have so many tasks to complete, it is difficult to remember to take backup every day. Thus, it’s always a good idea to setup automated backups. To schedule automatic backups add the commands in kernel.php:

# app/Console/Kernel.php

<?php

namespace App\Console;
...
class Kernel extends ConsoleKernel
{
    ...
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('backup:clean')->daily()->at('05:00');
        $schedule->command('backup:run')->daily()->at('06:00');
    }
    ...
}

In the above code, your application’s old backup will be removed at 5 am and the fresh backup is scheduled for 6 am.

Conclusion:

Backups are essential for any application in production. For this, we installed a third party library and configured it. We also saw various backup commands it provides and at the end automated the backup cron by scheduling it.

Though this works great for local database, in the coming post, we will also discuss about taking backup on Amazon S3. Excited enough?

QUESTIONS & COMMENTS:

Thank you for reading. As you would know by now, backups are very important for any application. Hope this tutorial served that purpose.. If you get stuck along the way, share issues in the comment section below.

Leave a comment

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