How to store Laravel backup on Dropbox?
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.
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.
In the App console, you can see a "Generate" button to generate an access token.
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.
Please login or create new account to add your comment.