How To Store Backup on Dropbox using Spatie Laravel 11?

Jun 15, 2024 | Laravel 11 Laravel


Hello Dev,

This comprehensive guide will walk you through backing up a Laravel 10 project to Dropbox. You'll gain insight into storing Laravel 10 backups on Dropbox with a straightforward example. This tutorial will illustrate a Laravel 10 Dropbox integration example.

Dropbox is a cloud-based service for file storage and sharing, founded in 2007 by Drew Houston and Arash Ferdowsi, headquartered in San Francisco, California. With Dropbox, users can upload and store various files like documents, photos, and videos, which are then synchronized across all their devices. This synchronization enables users to access their files from desktop computers, laptops, smartphones, or tablets, provided they have an internet connection. Moreover, Dropbox offers offline access, allowing users to work on files even without an internet connection.

In our example, we'll utilize Dropbox and the Spatie Package to backup your Laravel project and upload it to your Dropbox account.

Step 1: Install Laravel

If you haven't already, create a new Laravel project:

composer create-project laravel/laravel example-app
Step 2: Configure Dropbox as Driver

In this step, we'll install the "spatie/flysystem-dropbox" package to access the Dropbox API. Next, we'll add Dropbox storage configuration in the "AppServiceProvider". Finally, we'll configure Dropbox as a driver in the "config/filesystems.php" file. Let's execute the following command to install the package:

composer require spatie/flysystem-dropbox
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
            );
        });
    }
}

Now, let's add new driver on config/filesystems.php file.

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'),
                ],
        ],
    ]
Read Also: How To Import Large CSV File Into Database in Laravel 11? Step 3: Get Dropbox API Key & Secret & Access Token

In this step, we'll require the Dropbox API key, secret, and access token to store files on the Dropbox account.

1. Visit the Dropbox App Console Dropbox App Console and create a new app, following the instructions provided in the following screenshot:

How To Store Backup on Dropbox using Spatie Laravel? dropbox - 1

2. After clicking on the "Create App" button, you'll need to add a name for your app.

How To Store Backup on Dropbox using Spatie Laravel dropbox -2

3. Add "files.content.write" permission to your app.

How To Store Backup on Dropbox using Spatie Laravel -- dropbox -3

4.You need to obtain the API Key, API Secret, and Access Token from this location and then configure them in the .env file.

How To Store Backup on Dropbox using Spatie Laravel -- dropbox -4

You need to set all configuration on your .env file.

.env
DROPBOX_APP_KEY=wx0qfff2ly...
DROPBOX_APP_SECRET=gqdi00g...
DROPBOX_AUTH_TOKEN=sl.Bc5_aUxtGFDZP6...
Step 4: Install spatie/laravel-backup

Here, we'll install and set up the spatie/laravel-backup composer package to back up your Laravel project and transfer it to your Dropbox account.

So, let's execute the following command:

composer require spatie/laravel-backup

Here, we will execute a command to separately publish the Spatie package.

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

The vendor publish command mentioned above has created a new config/backup.php file. In this file, you need to configure backup settings.

For example, assign "dropbox" to the disks property. This is the disk name where your backups will be stored.

config/backup.php
 [
            /*
             * The disk names on which the backups will be stored.
             */
            'disks' => [
                'dropbox',
            ],
        ],
Read Also: How To Generate PDF and Send Email in Laravel 11? Step 5: Backup Laravel App

Now, we're prepared to back up the entire Laravel application. Let's execute the following command:

php artisan backup:run
How To Store Backup on Dropbox using Spatie Laravel -- dropbox - 5

Feel free to ask if you have any questions or need further clarification!



Tags :
#Laravel 11
#Laravel
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"