Laravel 10 Store Backup on Dropbox using Spatie Code Example

Jun 06, 2023 . Admin



Hi dev,

This detailed guide will cover laravel 10 project backup dropbox. you can understand a concept of how to store laravel 10 backup on dropbox. This article will give you a simple example of laravel 10 backup dropbox. In this article, we will implement a laravel 10 dropbox integration example.

Dropbox is a cloud-based service that lets users share and access their files with others and view them from anywhere. The business is headquartered in San Francisco, California, and was established in 2007 by Drew Houston and Arash Ferdowsi. Users can upload and store files including papers, images, and videos on Dropbox, and those files will then sync across all of their devices. As long as they have an internet connection, users can view their files from a desktop computer, laptop, smartphone, or tablet. Additionally, Dropbox provides offline access, which enables users to edit files even when they aren't online.

In this example, we will use Dropbox and Spatie Package to backup your laravel project and upload on your dropbox account.

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

Step 1: Install Laravel

first of all, we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:

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

In this step, we will install spatie/flysystem-dropbox package for access dropbox api. Then we will add dropbox storage configuration on AppServiceProvider. Then also we will configure as driver on config/filesystems.php file. so, let's run following command to install package.

composer require spatie/flysystem-dropbox	

Next, update 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 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'),
                ],
        ],
    ]	
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:

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

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

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

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 spatie/laravel-backup composer package to backup your laravel project and send it to dropbox account.

So, let's run 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 vendor publish command, published a new config/backup.php file in this file; in this file, you have to set backup values.

For instance, set dropbox name to 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 back of all laravel application. so, let's run following command:

php artisan backup:run

You will see the backup store in Dropbox account as like the below:

I hope it can help you...

#Laravel 10