How to store Laravel backup on Dropbox?

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

How to Use DTOs for Cleaner Code in Laravel, Best Practices and Implementation Guide

When developing APIs in Laravel, ensuring your responses are clear, concise, and consistent is crucial for creating a maintainable and scalable application. One effective way to (...)
Harish Kumar

Data Type Validation in Laravel Collections with the `ensure()` Method

Before moving on to the ensure() method, let us first know what Laravel Collections actually are. These are wrappers of PHP arrays, offering a fluent and helpful interface in interacting (...)
Harish Kumar

PHP Generators: Efficient Data Handling and Iteration Techniques

PHP Generators offer a powerful and memory-efficient way to handle large datasets and complex iteration scenarios in your applications. They provide a more elegant solution compared (...)
Harish Kumar

Compress and Download Files in Laravel Using ZipArchive with Examples

In web development, file compression is essential for optimizing data transfer and storage. Laravel provides tools for creating and downloading compressed files. This guide explores (...)
Harish Kumar

Implementing Multi-Authentication with Guards in Laravel

This guide provides a detailed explanation of how to implement multi-authentication using guards in a Laravel application. This is useful for applications that need to support (...)
Harish Kumar

Building Real-Time Chat Applications with Laravel Reverb and Vue 3

Real-time chat applications are increasingly important for seamless communication and instant connectivity. Laravel Reverb and Vue.js 3 provide developers with the tools to create (...)
Harish Kumar