In this tutorials, you'll learn how to take a database backup with Spatie in a Laravel application quickly and easily. This Laravel spatie example will show you how to use the spatie package to take database backups.

The Spatie eloquently assists you in backing up your Laravel application in minutes. It makes a zip file backup that contains everything of your application's basic directory, files, and, most crucially, the database dump.

The Laravel Spatie has the advantage of allowing you to store laravel backups on whichever filesystem you wish for your laravel application.

Table of Content

  1. Install Laravel Project
  2. Configure Database Connection
  3. Install Laravel Spatie Package
  4. Register Service Provider
  5. Set Up Backup in Laravel
  6. Take Backup to Secure Laravel Data

1. Install Laravel Project

To build a new Laravel project from scratch, use the composer to install the app, open the command-line tool, and run the following command.

composer create-project laravel/laravel --prefer-dist laravel-demo-app
cd laravel-demo-app

2. Configure Database Connection

You can use MAMP or XAMPP as a local web server by editing the .env file with the database name, username, and password.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=

3. Install Laravel Spatie Package

This is the first stage, in which we'll show you how to install the spatie package in your Laravel app using the composer command.

composer require spatie/laravel-backup

The PHP artisan command spatie sends a backup email after execution; you must specify the email address where you wish to receive backup information.

In the .env file, put the email address.

MAIL_[email protected]
MAIL_FROM_NAME="${APP_NAME}"

4. Register Service Provider

In this stage, you'll register the service provider, check for package service providers in the config/app.php file, and carefully insert the BackupServiceProvider class.

'providers' => [
    ...
    ...
    ...
    Spatie\Backup\BackupServiceProvider::class,
];

5. Set Up Backup in Laravel

To get started with Laravel Backup, make sure the config file is published to config/laravel-backup.php.

Execute the specified command to accomplish this task.

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

Here is the file you can look at to see the backup configuration's default contents.

<?php
return [
    'backup' => [
        'name' => env('APP_NAME', 'laravel-backup'),
        'source' => [
            'files' => [
                
                'include' => [
                    base_path(),
                ],
               
                'exclude' => [
                    base_path('vendor'),
                    base_path('node_modules'),
                ],
                
                'follow_links' => false,
                
                'ignore_unreadable_directories' => false,
                
                'relative_path' => null,
            ],
            
            'databases' => [
                'mysql',
            ],
        ],
       
        'database_dump_compressor' => null,
        
        'database_dump_file_extension' => '',
        'destination' => [
           
            'filename_prefix' => '',
           
            'disks' => [
                'local',
            ],
        ],
       
        'temporary_directory' => storage_path('app/backup-temp'),
        
        'password' => env('BACKUP_ARCHIVE_PASSWORD'),
       
        'encryption' => 'default',
    ],
    
    'notifications' => [
        'notifications' => [
            \Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class => ['mail'],
            \Spatie\Backup\Notifications\Notifications\UnhealthyBackupWasFoundNotification::class => ['mail'],
            \Spatie\Backup\Notifications\Notifications\CleanupHasFailedNotification::class => ['mail'],
            \Spatie\Backup\Notifications\Notifications\BackupWasSuccessfulNotification::class => ['mail'],
            \Spatie\Backup\Notifications\Notifications\HealthyBackupWasFoundNotification::class => ['mail'],
            \Spatie\Backup\Notifications\Notifications\CleanupWasSuccessfulNotification::class => ['mail'],
        ],
        
        'notifiable' => \Spatie\Backup\Notifications\Notifiable::class,
        'mail' => [
            'to' => '[email protected]',
            'from' => [
                'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
                'name' => env('MAIL_FROM_NAME', 'Example'),
            ],
        ],
        'slack' => [
            'webhook_url' => '',
            
            'channel' => null,
            'username' => null,
            'icon' => null,
        ],
        'discord' => [
            'webhook_url' => '',
            'username' => null,
            'avatar_url' => null,
        ],
    ],
   
    'monitor_backups' => [
        [
            'name' => env('APP_NAME', 'laravel-backup'),
            'disks' => ['local'],
            'health_checks' => [
                \Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumAgeInDays::class => 1,
                \Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumStorageInMegabytes::class => 5000,
            ],
        ],
        
    ],
    'cleanup' => [
       
        'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,
        'default_strategy' => [
            
            'keep_all_backups_for_days' => 7,
            
            'keep_daily_backups_for_days' => 16,
            
            'keep_weekly_backups_for_weeks' => 8,
            
            'keep_monthly_backups_for_months' => 4,
            
            'keep_yearly_backups_for_years' => 2,
            
            'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
        ],
    ],
];

6. Take Backup to Secure Laravel Data

We've covered how to set up the laravel app, install the spatie plugin, and configure its basic settings; now it's time to take a backup.

But first, let's quickly delete Laravel's configuration cache; use the command below to do so.

php artisan config:clear

Taking a backup is straightforward; be sure to use the following command.

php artisan backup:run

The backup of the laravel application is required because there is a high likelihood of an error during the app development process.

Fortunately, using the Spatie package, we discovered a unique approach to backup the laravel app and database; in this article, we learned how to install and set up laravel spatie for laravel backup from scratch.

I hope you will like the content and it will help you to learn Laravel 9 Database Backup with Spatie Tutorial Example
If you like this content, do share.


Recommended Posts

View All

Laravel 9 File Manager Tutorial Example


Learn how to implement a robust file manager in Laravel 9 with our step-by-step tutorial. This example will help you manage and organize files efficie...

How To Use WYSIHTML5 Editor In Laravel 9?


wysihtml5 editor example, laravel wysihtml5 editor example, wysihtml5 laravel, wysihtml5 editor example in laravel, wysihtml5 with laravel, wysihtml5...

Laravel 9 Install React Auth Tutorial with Example


In this article, I'll demonstrate how to use the Laravel UI and React Auth scaffolding to create login, register, logout, forget password, profile, an...

Laravel 9 CKeditor Image Upload With Example


Learn how to easily upload images in CKEditor using Laravel 9 with this step-by-step tutorial and example code. Improve your web development skills no...

Laravel where clause with date_format() example


laravel db raw where, laravel where date_format, laravel date_format mysql where, laravel date_format example, laravel where with month