How to store Laravel backup on Dropbox?

Harish Kumar · · 3251 Views

Storing backup on the cloud is always suggested. Dropbox is additionally a well-known choice to store files and documents. In this post, you will learn how to store a backup of the Laravel application on Dropbox.

Install and Configure Laravel Backup Package

We'll start by installing spatie/laravel-backup package. For that, run the following command in your terminal.

composer require spatie/laravel-backup

When the library is installed, run the following command, it will publish the config file to config/backup.php.

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

Now, configure your backup according to your need. You can read more about backup configuration and scheduling for automatic backup in this post: How to Set up Automatic Laravel Backup for database and files?

Now open the config/backup.php and add the dropbox to the disks array.

<?php
   
return [
   
    // ...

    'destination' => [
  
        // ...

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

Configure Dropbox as Filesystem in Laravel

For storing Laravel backup in Dropbox, we will utilize the Laravel Filesystem. By default in the Laravel filesystem provides the driver for local, Amazon S3, etc. As we need to manage Dropbox, So we need to add a Dropbox adapter in our application. For that, we will use spatie/flysystem-dropbox package. So, Run the below command to install it.

composer require spatie/flysystem-dropbox

Next, create a service provider DropboxServiceProvider using the below command. 

php artisan make:provider DropboxServiceProvider

Then, inside the boot() method, add the following code to register dropbox for the Laravel filesystem:

In the app\Providers\DropboxServiceProvider.php:

<?php
 
namespace App\Providers;
 
use Storage;
use League\Flysystem\Filesystem;
use Illuminate\Support\ServiceProvider;
use Spatie\Dropbox\Client as DropboxClient;
use Spatie\FlysystemDropbox\DropboxAdapter;
 
class DropboxServiceProvider extends ServiceProvider
{
    // ...
    public function boot()
    {
        Storage::extend('dropbox', function ($app, $config) {
            $client = new DropboxClient(
                $config['authorization_token']
            );
 
            return new Filesystem(new DropboxAdapter($client));
        });
    }
}

After this, register the service provider by adding the following line in the providers array of config/app.php.

'providers' => [
    // ...
    App\Providers\DropboxServiceProvider::class,
];

Now the Dropbox drive is ready. So next, we will add the storage disk configuration to config/filesystem.php:

<?php
   
return [
   
    // ...
   
    'disks' => [
   
        // ...
   
        'dropbox' => [
            'driver' => 'dropbox',
            'authorization_token' => env('DROPBOX_AUTH_TOKEN'),
        ],
   
    ],
];

Next, we need to define the DROPBOX_AUTH_TOKEN in the .env file. First, let's get the token from Dropbox.

Get Dropbox Token

To get the token, make a Dropbox app from the App Console. You need to first login with your Dropbox credentials.

Click on the "Create app" button. Then, fill the form and create the app.

How to store Laravel backup on Dropbox?

After your app is created in the Dropbox console, navigate to the permissions tab. Here allow files and folders to write permission as displayed in the following image.

How to store Laravel backup on Dropbox?

In the App console, you can see a "Generate" button to generate an access token.

How to store Laravel backup on Dropbox?

Copy this token value and define the constant DROPBOX_AUTH_TOKEN in the .env file.

DROPBOX_AUTH_TOKEN=<your token>

Backup Laravel Application on Dropbox

We are finished with the setup needed to store our Laravel backup on Dropbox storage. Now we are all set to run our first backup. Open the terminal in your project root directory and run the command:

php artisan backup:run

The above command will make a backup of the Laravel application and store it on your Dropbox account.

I hope you got to know about storing a backup of the Laravel project on Dropbox. We would like to hear your thoughts in the comment section below.

0

Please login or create new account to add your comment.

0 comments
You may also like:

Part #3: Rule objects based custom validation in Laravel

Laravel comes with multiple ways to add custom validation rules to validate form request inputs. I have already explained some of the ways in the following article links:
Harish Kumar

Part #2: How to use Laravel's Validator::extend method for custom validation

Validation is important in any application as it validates a form before performing actions on it. It allows the user to know their input is accurate and confident about the operation (...)
Harish Kumar

Part #1: Closure-based Custom Laravel Validation

While I was working with Laravel, validation using closure came to my mind, and I know it will be helpful to you. This tutorial assists you with all what is the difference between (...)
Harish Kumar

How to use the enumerations(Enums) of PHP 8.1 in Laravel?

The release of PHP 8.1 brings native enumerations to PHP. There is no more requirement for custom solutions in your Laravel projects since the Laravel v8.69 release has you back. (...)
Harish Kumar

Mobile App Development Process

With businesses adopting a mobile-first approach and the growing number of mobile apps, successful mobile app development seems like a quest. But it’s the process that determines (...)
Narola Infotech

What are Laravel Macros and How to Extending Laravel’s Core Classes using Macros with example?

Laravel Macros are a great way of expanding Laravel's core macroable classes and add additional functionality needed for your application. In simple word, Laravel Macro is an (...)
Harish Kumar