Laravel 10 Store Backup on Dropbox using Spatie Tutorial

Last Updated on by in Laravel

Laravel Dropbox backup tutorial; In this tutorial, we will teach you how to backup the laravel application and store the laravel app backup on Dropbox.

This guide will make you able to integrate Dropbox in Laravel for storing laravel app backup, and the Spatie package will surely help you in this journey.

How to Save Laravel 10 App Backup on Dropbox Storage

  • Step 1: Install Laravel Project
  • Step 2: Add Database Credentials
  • Step 3: Install Laravel Spatie
  • Step 4: Get Dropbox Access Token
  • Step 5: Configure Dropbox FileSystem
  • Step 6: Add Dropbox Access Key
  • Step 7: Clear Config Cache
  • Step 8: Take Backup and Store on Dropbox

Install Laravel Project

In the first step, you have to install the composer on your system, create a laravel application using the composer command.

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

Get inside the project.

cd laravel-demo-app

Configure Database Connection

The database configuration of Laravel services is located in the application’s .env configuration file.

You have to open this file, insert the database name, username, and password.

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

If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Install Laravel Spatie

Next, you can dynamically install the Laravel Spatie package via composer using this command.

composer require spatie/laravel-backup

After taking backup, Spatie sends the confirmation mail related to backup; you need to add the email to receive a confirmation mail in response to laravel backup.

Add the mail address from in the MAIL_FROM_ADDRESS variable:

MAIL_FROM_ADDRESS=demo@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

In the next step, we will run a command to publish the Spatie package separately.

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

Above vendor publish command, published a new config/backup.php file in this file; in this file, you have to set backup values.

For instance, set dropbox name to disks property; this is the disk name that will store your backup.

<?php
    
return [

        'destination' => [

            /*
             * The disk names on which the backups will be stored.
             */
            'disks' => [
                'dropbox',
            ],
        ],

Get Dropbox Access Token

In this Laravel dropbox example, you will require a Dropbox access token, and we will show you how to get a dropbox access token from the Dropbox dashboard.

You need to go to the Dropbox admin console and make sure to create a new project.

You will see “Generated access token” button, you have to click on it to generate and get the Dropbox access token.

Laravel Dropbox Backup Examle

Configure Dropbox FileSystem

Next, you can quickly install the spatie flysystem-dropbox package via composer.

composer require spatie/flysystem-dropbox

Next, run PHP artisan make command to publish dropbox service provider.

php artisan make:provider DropboxServiceProvider

Open the app/config/app.php file, add the dropbox service provider class into the providers’ array.

'providers' => [
    ...
    ...
    ...
    App\Providers\DropboxDriveServiceProvider::class,
];

Open the app/Providers/DropboxServiceProvider.php file, and you have to update the boot function inside the provider class.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class DropboxServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('dropbox', function ($app, $config) {
            $client = new DropboxClient(
                $config['add_authorization_token']
            );
  
            return new Filesystem(new DropboxAdapter($client));
        });
    }
}

Add Dropbox Access Key

In the last step, we have to add dropbox in laravel, and this mutual consent requires auth key, callback url and client id to be added into the config/filesystem.php file.

<?php
    
return [
    
    ...
    
    'disks' => [
    
        ...
    
        'dropbox' => [
            'driver' => 'dropbox',
            'key' => env('DROPBOX_APP_KEY'),
            'secret' => env('DROPBOX_APP_SECRET'),
            'authorization_token' => env('DROPBOX_ACCESS_TOKEN'),
        ],
    
    ],
];

In the final step, you have to open the .env file and, in this file, add the dropbox auth token.

DROPBOX_AUTH_TOKEN=<dropbox_auth_token>

Clear Config Cache

Let us first clear the config cache, execute the command from the terminal.

php artisan config:clear

Take Backup and Store on Dropbox

Eventually, you have to run the command to take the backup and store it onto dropbox in this last step.

php artisan backup:run

Conclusion

In this tutorial, we learned how to integrate Dropbox backup in the laravel app, and we hope you loved this tutorial.

The primary reason for backup is to store essential files if any error occurred, or the site gets hacked. To secure the laravel application, we used the laravel spatie; it will prevent natural or man-made disasters.