How to Store Backup on Dropbox using Spatie Library Laravel 9

This is a Laravel 9 Dropbox backup tutorial, where we will learn how to backup a Laravel application and store the app backup in Dropbox.

This guide will enable you to integrate Dropbox in Laravel to store your Laravel app backup, and the Spatie package will surely help you in this journey.

Save Laravel 9 App Backup in Dropbox Storage

  • Step 1: Install Laravel Project
  • Step 2: Add Database Credentials
  • Step 3: Install Laravel Spatie
  • Step 4: Get Dropbox Access Token
  • Step 5: Configure Dropbox FileSystem
  • Step 6: Add Dropbox Access Key
  • Step 7: Clear Config Cache
  • Step 8: Take a Backup and Store on Dropbox

Install Laravel Project

Firstly, lets install composer on your system, and create a fresh Laravel application using the command below.

composer create-project laravel/laravel --prefer-dist laravel-demo-app

Get inside the project.

cd laravel-demo-app

Configure Database Connection

The database configuration of Laravel applications is located in the application’s .env configuration file.

You need to open this file, and add your database name, username, and password.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=

If you are working on macOS and using the MAMP local server, ensure to append UNIX_SOCKET and DB_SOCKET database credentials as shown below in your .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Install Laravel Spatie

Next, you need to dynamically install the Laravel Spatie package via composer command as shown below.

composer require spatie/laravel-backup

After taking backup, Spatie sends a confirmation email about the backup. Hence, you should add an email address to receive a confirmation response on your Laravel backup.

So, add the email address using the MAIL_FROM_ADDRESS variable, as shown below.

MAIL_FROM_ADDRESS=demo@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

In the next step, we should run a command to publish the Spatie package separately.

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

Above the vendor publish command, you need to publish a new config/backup.php file, where you have to set backup values.

For example, add a dropbox name to disks property, this is the disk name that can store your backup.

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

Get Dropbox Access Token

In this Laravel dropbox example, we will need a Dropbox access token, that will show you how to get a dropbox access token from the Dropbox dashboard.

You have to go to the Dropbox admin console and make sure you create a new project.

Here you will see “Generated access token” button, click on it to generate and get the Dropbox access token.

Laravel Dropbox Backup Examle

Configure Dropbox FileSystem

Next step is to quickly install the spatie flysystem-dropbox package via the composer command shown below.

composer require spatie/flysystem-dropbox

Now, run PHP artisan make command and publish dropbox service provider.

php artisan make:provider DropboxServiceProvider

Open your app/config/app.php file, to add the dropbox service provider class into the providers’ array as shown below.

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

Open your app/Providers/DropboxServiceProvider.php file, and update the boot function inside the provider class as shown below.

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class DropboxServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('dropbox', function ($app, $config) {
            $client = new DropboxClient(
                $config['add_authorization_token']
            );
  
            return new Filesystem(new DropboxAdapter($client));
        });
    }
}

Add Dropbox Access Key

Finally, we will now add dropbox in Laravel, and this mutual consent requires an authentication key, a callback URL and a client id to be added into your config/filesystem.php file.

<?php
    
return [
    
    ...
    
    'disks' => [
    
        ...
    
        'dropbox' => [
            'driver' => 'dropbox',
            'key' => env('DROPBOX_APP_KEY'),
            'secret' => env('DROPBOX_APP_SECRET'),
            'authorization_token' => env('DROPBOX_ACCESS_TOKEN'),
        ],
    
    ],
];

Lastly, you need to open the .env file and, add here the dropbox auth token.

DROPBOX_AUTH_TOKEN=<dropbox_auth_token>

Clear Config Cache

Let us now first clear your application’s config cache, then execute the command from terminal.

php artisan config:clear

Take Backup and Store on Dropbox

Eventually, you need to run the command below to take a backup and then store it onto dropbox in this last step.

php artisan backup:run

Conclusion

In this tutorial, we have learned how to integrate Dropbox backup in a Laravel application, and we hope you liked and learnt from this tutorial.

The primary reason for backup is to store essential files at a safe place, in cases if an error occurs, or a website gets hacked. To secure your Laravel application, we have used the Laravel spatie package, that will prevent natural or man-made disasters.

Leave a Comment