Schedule A Consultation

    Fields marked * are mandatory

      CA INQUIRY

      How to Backup Laravel Files and Database

      This entry was posted on Friday February 14, 2020

      At the point when an application scales or gets perplexing, it gets critical to monitor changes. Likewise, with the application being developed, you generally should be on your toes to fix the bugs, handle surprising circumstances like server vacation or database/record degenerate/erase. Such circumstances are extremely urgent for any business. Hence it is important to reinforcement the database and application records all the time. Let us presently perceive how to make database reinforcement in Laravel 6 applications:

      # Laravel-backup Package Setup

      Luckily, Backups in Laravel can be really clear. Laravel people group has great engineers who give an answer for different issues. For reinforcement, we will utilize such outsider library from spatie called laravel-reinforcement. It can reinforcement the two records and database and store in a .compress document. This bundle can likewise send in email or slack notice when the issue emerges with reinforcements. 

      To utilize the most recent bundle with its highlights, here are some fundamental prerequisites:

      PHP 7 or higher

      Laravel 5.8 or higher

      package : “guzzlehttp/guzzle”: “^6.2”

      Enough space to store reinforcement 

      In any case, if your application doesn’t satisfy these prerequisites, don’t stress, you simply need to utilize a lower form of this bundle.

      Install the package in your Laravel application:

      composer require spatie/laravel-backup

      In Laravel 5.8+, 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”

      OR

      php artisan vendor:publish

       

      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@cilected.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

      To backup all the files and database:

      php artisan backup:run

      To backup only database:

      php artisan backup:run –only-db

      To backup application files only:

      php artisan backup:run –only-files

      To track the status of monitored backups:

      php artisan backup:list

      These backups are stored in 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?