How to Store Backup on Dropbox using Spatie in Laravel 11?

30-May-2024

.

Admin

How to Store Backup on Dropbox using Spatie in Laravel 11?

Hi, Dev

In this tutorial, I will show you how to store backups in Dropbox using Spatie in a Laravel 11 application.

Dropbox is a cloud-based file storage and sharing service that allows users to access their files from anywhere and share them with others. Founded in 2007 by Drew Houston and Arash Ferdowsi, the company is headquartered in San Francisco, California. With Dropbox, users can upload and store various types of files, including documents, photos, and videos. These files are then synced across all their devices, enabling access from a desktop computer, laptop, smartphone, or tablet, provided there is an internet connection. Additionally, Dropbox offers offline access, allowing users to work on their files even without an internet connection.

In this example, we will use Dropbox and Spatie Package to back up your Laravel project and store it on your Dropbox account.

So, let's follow the below step to make it done this example.

Step for Laravel 11 Project Backup and Store into Dropbox Example


Step 1: Install Laravel 11

Step 2: Configure Dropbox as Driver

Step 3: Get Dropbox API Key & Secret & Access Token

Step 4: Install spatie/laravel-backup

Step 5: Backup Laravel App

Step 1: Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Configure Dropbox as Driver

In this step, we will install the spatie/flysystem-dropbox package to access the Dropbox API. Then we will add Dropbox storage configuration in AppServiceProvider. We will also configure it as a driver in the config/filesystems.php file. So, let's run the following command to install the package.

composer require spatie/flysystem-dropbox

Next, update the AppServiceProvider 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

);

});

}

}

Now, let's add a new driver in the 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'),

],

],

]

Step 3: Get Dropbox API Key & Secret & Access Token

In this step, we will need to dropbox api key, secret and access token to store files on Dropbox Account.

1. You need to go here Dropbox App Console and you need to create new app as like the following screenshot:

laravel11-laravel-dropbox-app

2. After click on "Create App" button, you need to add name of App.

after-click-on-create-app

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

laravel11-laravel-dropbox-app-3

4. Here you need to get API Key, API Secret and Access token from here and need to set on .env file.

laravel11-laravel-dropbox-app-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 will install and configure the spatie/laravel-backup composer package to backup your Laravel project and send it to your Dropbox account.

So, let's run the following command:

composer require spatie/laravel-backup

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

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

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

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

config/backup.php

<?php

return [

...

...

'destination' => [

/*

* The disk names on which the backups will be stored.

*/

'disks' => [

'dropbox',

],

],

Step 5: Backup Laravel App

Now, we are ready to take a backup of the Laravel application. So, let's run the following command:

php artisan backup:run

You will see the backup stored in the Dropbox account as shown below:

dropbox-using-spatie-laravel11

I hope it can help you...

#Laravel 11