How to store Laravel backup on Dropbox?

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

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

Install Laravel Valet Linux+ development environment on Ubuntu System

The official Laravel Valet development environment is great if you are an Apple user. But there is no official Valet for Linux or Window system.
Harish Kumar

Laravel Sanctum API Token Authentication Tutorial with example

Laravel Sanctum is a popular package for API Token Authentication. There are many other packages available to authenticate the APIs request in Laravel. For example, We are already (...)
Harish Kumar

Create SPA authentication Using Laravel Sanctum and Vue.js

In this guide, we will focus on SPA authentication in a simple Vue.js app using Laravel Sanctum. Laravel Sanctum provides a featherweight authentication system for SPAs (single (...)
Harish Kumar

Create API Authentication with Laravel Passport

In this article, we'll see how to implement restful API authentication using Laravel Passport. You should have experience working with Laravel as this is not an introductory tutorial. (...)
Sumit Talwar

Laravel Themer: multi-theme support for Laravel application

This Laravel Themer package adds multi-theme support to your application. This theme package improves any application while allowing the freedom to organize and maintain your app's (...)
Harish Kumar