Blog Detail

15

Nov
How to Create Backup of your Application in Laravel 8 cover image

arrow_back How to Create Backup of your Application in Laravel 8

If you are feeling paranoid about backups? Don’t be! You can backup your application to multiple filesystems at once with the Laravel-backup Package. Laravel-backup is a package by Spatie that generates a backup of your application. The backup is a zip file that includes all files in the directories you specify along with a dump of your database. The backup can be stored on any of the filesystems you have configured. The package can also inform you via Mail, Slack, or any notification provider when something goes wrong with your backups.

After installation, You can back up your files and databases very easily. Just you’ve to run this artisan command:

php artisan backup:run

Requirements

  • This backup package requires PHP 8.0, with the ZIP module and Laravel 8.0 or higher. It’s not compatible with Windows servers.
  • If you are using an older version of Laravel, take a look at one of the previous versions of this package.
  • The package needs free disk space where it can create backups. Ensure that you have at least as much free space as the total size of the files you want to backup.
  • Make sure mysqldump is installed on your system if you want to backup MySQL databases.
  • Make sure pg_dump is installed on your system if you want to backup PostgreSQL databases.
  • Make sure mongodump is installed on your system if you want to backup Mongo databases.

Installation and Setup

Basic installation

You can install this package via composer using:

composer require spatie/laravel-backup

The package will automatically register its service provider.

To publish the config file to config/backup.php run:

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

Configuring the backup disk

By default, the backup will be saved into the public/laravel-backup/ directory of your laravel application. This folder most probably is configured to be public. We recommend that you create a disk named backups (you can use any name you prefer) in filesystems.php and specify that name in the disk key of the backup.php config file.

Scheduling

After the basic installation you can begin utilizing the backup:run, backup:clean, backup:list and backup:monitor-commands. In most cases you’ll want to schedule these commands so you don’t have to manually run backup:runeverytime you need a new backup.
The commands can be scheduled in Laravel’s
console kernel, just like any other command.

// app/Console/Kernel.php

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

Of course, the times utilized in the code above are just examples. Adjust them to suit your preferences. It is generally a good idea to avoid the timeslot between 02:00 and 03:00 at night in areas where daylight saving time changes occur, as this causes sometimes a double backup or (worse) no backup at all.
If a backup cannot be taken successfully, the backup:run command returns an exit code of 1 which signals a general error, so you can use laravel’s task hooks to specify code to be executed if the scheduled backup succeeds or fails:

   $schedule
      ->command('backup:run')->daily()->at('01:00')
      ->onFailure(function () {
         ...
      })
      ->onSuccess(function () {
         ...
      });

Taking backups

You can backup your app by running:

php artisan backup:run

If you want to backup to a specific disk instead of all disks, run:

php artisan backup:run --only-to-disk=name-of-your-disk

If you only need to backup the db, run:

php artisan backup:run --only-db

If you only need to back up the files and want to skip dumping the databases, run:

php artisan backup:run --only-files

Be very careful with --only-db and --only-files. When monitoring backups, the package does not make a distinction between full backups and a backup that only contains files or databases. It may be the case that you will not be able to recover from a partial backup.

For more details visit Github

Published at : 15-11-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project