How to store Laravel backup on Dropbox?

Harish Kumar · · 4823 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:

Laravel Facades: Simplifying Code and Improve Readability

As an integral part of Laravel, a renowned PHP framework, Facades provide a static interface to classes stored in the application's service container. They serve as static proxies (...)
Harish Kumar

What is Laravel’s Service Container and How to Use Dependency Injection in Laravel App

Dependency injection and inversion of control are vital in clean web development. They make writing maintainable, testable code possible. Laravel is a famous PHP framework that (...)
Harish Kumar

Secure Your SPA with Laravel Sanctum: A Step-by-Step Guide

In today's web development landscape, Single Page Applications (SPAs) are increasingly popular. But securing their interaction with backend APIs is crucial. Laravel Sanctum provides (...)
Harish Kumar

Multi-Authentication with Guards in Laravel

Laravel's robust authentication system provides a powerful mechanism for securing your application. To cater to scenarios where you need different user roles with distinct login (...)
Harish Kumar

Laravel Pint & VS Code: Automate Your Code Formatting

Laravel Pint is an opinionated PHP code style fixer built on top of PHP-CS-Fixer, designed to simplify the process of ensuring clean and consistent code style in Laravel projects. (...)
Harish Kumar

Laravel Clockwork: A Deep Dive into Debugging, Profiling Skills and Best Practices

In the world of web development, building complex applications often comes with the challenge of identifying and resolving performance bottlenecks. This is where a reliable debugging (...)
Harish Kumar