How to Backup on Dropbox using Spatie in Laravel 11

Hello, laravel web developers! In this article, we'll see how to backup on Dropbox using spatie/flysystem-dropbox in laravel 11. Dropbox is a file hosting service operated by the American company Dropbox, Inc.

Dropbox is a cloud storage solution, equipped with features to help you save time, improve your productivity, and collaborate with others.

You can do with Dropbox include: Store your files, documents, and photos online and access them from any device.

Laravel 11 Store Backup on Dropbox using spatie/flysystem-dropbox

laravel 11 store backup on dropbox using spatie, spatie/flysystem-dropbox

 

Step 1: Install Laravel 11 Application

We'll install the laravel 11 application using the following command in this step.

composer create-project laravel/laravel laravel-11-example

 

Step 2: Install spatie/flysystem-dropbox package

Next, we'll install the spatie/flysystem-dropbox composer package to access the Dropbox API using the following command.

composer require spatie/flysystem-dropbox

 

Step 3: Configure Dropbox as Driver

Then, we'll add Dropbox storage configuration in AppServiceProvider. we'll configure it as a driver in the config/filesystems.php file.

app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client as DropboxClient;
use Spatie\FlysystemDropbox\DropboxAdapter;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Storage::extend('dropbox', function (Application $app, array $config) {
            $adapter = new DropboxAdapter(new DropboxClient(
                $config['authorization_token']
            ));
   
            return new FilesystemAdapter(
                new Filesystem($adapter, $config),
                $adapter,
                $config
            );
        });
    }
}

config/filesystems.php

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

 

Step 4: Get Dropbox API Key & Secret Key

Next, get the Dropbox API key, secret key, and authorization token to store files on Dropbox.

Creating a new app. After navigating to the developer portal, click App Console.

new_app

Add files.content.write permission to your app.

permissions

Now, get the API Key, API Secret, and Access token.

dropbox-settings

Next, add credentials to the .env file.

.env

DROPBOX_APP_KEY=xxxxxx
DROPBOX_APP_SECRET=xxxxxx
DROPBOX_AUTH_TOKEN=xxxxxx

 

Step 5: Install spatie/laravel-backup package

Next, we'll install spatie/laravel-backup composer package to back up the project and store it in Dropbox.

composer require spatie/laravel-backup

Now, publish the laravel backup file using the following command.

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

config/backup.php

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

 

Step 6: Backup Laravel Application

Now, run the following command to backup laravel application.

php artisan backup:run

 


You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS