Ho to Store Backup on Dropbox in Laravel 9 with Spatie

Laravel Spatie Dropbox backup tutorial; In this small yet informative guide, we will explain how to implement dropbox in the Laravel application concerning storing the backup on dropbox.

Dropbox is one of the best storage services for your storing files and docs online and other digital data.

Using this detailed guide, you will learn how to integrate Dropbox storage for taking up backup of Laravel application using the Laravel spatie backup package.

The Laravel Spatie provides powerful and profound backup solutions. Backing up your laravel app with this user-friendly package has never been so easy.

We will learn how to install and set up spatie in laravel for taking the laravel app in a zip file, not just the laravel project files, but we will also take the database dump backup with this amazing plugin.

Laravel 9 Store Backup on Dropbox Storage Example

  • Step 1: Download Laravel Project
  • Step 2: Update Database Credentials
  • Step 3: Add Spatie Package in Laravel
  • Step 4: Configure Spatie Package
  • Step 5: Create Dropbox Token
  • Step 6: Set Up Dropbox File System
  • Step 7: Insert Dropbox Keys
  • Step 8: Store Backup on Dropbox

Download Laravel Project

The first step begins by using the composer command for installing the new laravel application.

composer create-project laravel/laravel --prefer-dist blog

Update Database Credentials in ENV

To connect the database to the Laravel app, add the database details in the .env config file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_username
DB_PASSWORD=db_password

Add Spatie Package in Laravel

Further, head over to the terminal and add the Spatie library for building laravel backup functionality with the help of provided command.

composer require spatie/laravel-backup

Configure Spatie Package

You have to define the email where you will receive the backup confirmation mail, open the .env file and define your email.

MAIL_FROM_ADDRESS=your_email@hotmail.com
MAIL_FROM_NAME="${APP_NAME}"

Subsequently, you have to publish the backup.php file using the php artisan vendor command.

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

After running the command, head over to the newly generated config/backup.php and look for the destination array; within this, change the disks property to dropbox.

<?php
    
return [

        'destination' => [

            'disks' => [
                'dropbox',
            ],
        ],

Create Dropbox Token

Next, use the email id to sign up for the dropbox admin dashboard; you will generate and obtain the access token from this locus.

After logging in, make sure to create a dropbox project, click on the Generate to get the access token.

Ho to Store Backup on Dropbox in Laravel with Spatie

Set Up Dropbox File System

Now that the access token has been created, its time to install the spatie flysystem-dropbox package, so run the command to install it.

composer require spatie/flysystem-dropbox

Let us make the separate DropboxServiceProvider provider by running the given command.

php artisan make:provider DropboxServiceProvider

Now, head over to config/app.php, and define the DropboxDriveServiceProvider class into the providers property.

'providers' => [
    ...

    App\Providers\DropboxDriveServiceProvider::class,
];

In this step, you also have to visit app/Providers/DropboxServiceProvider.php, then update the following code into the file as suggested below.

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;


class DropboxServiceProvider extends ServiceProvider
{

    public function register()
    {
        
    }

    public function boot()
    {
        Storage::extend('dropbox', function ($app, $config) {
            $client = new DropboxClient(
                $config['AuthorizationToken']
            );
            return new Filesystem(new DropboxAdapter($client));
        });
    }
}

Insert Dropbox Keys in Laravel

Head over to config/filesystem.php file, define dropbox key, secret and authorization token within the disks and dropbox props.

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

Lastly, get inside the .env, define the dropbox auth token to make the laravel dropbox backup module work.

DROPBOX_AUTH_TOKEN = <dropbox_auth_token>

Store Backup on Dropbox

Now let us swiftly reset your laravel app’s configuration cache right after clearing the cache; the configuration cached successfully message will appear on your terminal screen.

php artisan config:clear

Once the basic configuration has been done, initiate the command for taking a backup of your laravel files and databases.

php artisan backup:run

Summary

During this guide, you have seen the best tactics to implement dropbox in laravel and learned how to take the laravel application backup.

Moreover, we have seen how to integrate the laravel spatie backup package and activate it in the laravel app using the dropbox keys.