Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

Laravel 11 Store Backup on Dropbox using Spatie Tutorial

Last Updated on July 3, 2024 by

In Laravel 11, Dropbox provides PHP SDK that allows developers to store backup of Laravel applications with the help of the file system.

Let’s start to implement functionality in laravel application to store backup on dropbox:

Step 1 – Create Account Dropbox

Go to https://www.dropbox.com/login?cont=https%3A%2F%2Fwww.dropbox.com%2Fdevelopers%2Fapps link, and create console application in it, and find secret key, id, & token.

Step 2 – Define Secret id and key in .env

Edit .env file, and define dropbox console application secret id, key and token in it:

DROPBOX_APP_KEY=lsjkfasklfjsa...
DROPBOX_APP_SECRET=ljfkaslfjlsajf...
DROPBOX_AUTH_TOKEN=td.Bfsadlfjsfsa

Step 3 – Install and SetUp Spatie Flysystem-Dropbox

Run the following command to install spatie/flysystem-dropbox package that will help to connect and store backup on dropbox:

composer require spatie/flysystem-dropbox

Edit app/Providers/AppServiceProvider.php file, and add the following code in it:

<?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
            );
        });
    }
}

Edit config/filesystems.php file, and set up file system details in it:

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

Step 4 – Install spatie/laravel-backup

Run the following command to install spatie laravel backup package in application:

composer require spatie/laravel-backup

Step 5 – Test This Application

Run the following backup command to take backup of laravel application in dropbox:

php artisan backup:run

Go to your dropbox account and check backup of your laravel application.

Leave a Comment