Storing application backup on the cloud is always recommended. You never know when you might need to restore your site from the backup. Amazon S3, Google Cloud, Dropbox, etc are some of the popular services to store your application backup. In this tutorial, we study storing Laravel application backups on Dropbox. If you want to store it on Amazon S3 then please refer to our article storing Laravel backup on Amazon S3.
Dropbox offers an API service that can send the backup from our application to Dropbox. When it comes to Laravel, we have to configure Laravel Filesystem and set Dropbox as one of the file storage. It will then interact with Dropbox API behind the scenes.
During interaction with Dropbox API, we need to have an access token of the Dropbox account for authorization. Let’s first grab the Dropbox access token.
Get Dropbox Access Token
To get the access token, create a Dropbox application. Upon login, click on the ‘Create app’ button. In the next step, you will ask for choosing an API. Under the type of access, choose ‘App folder’ and give the name to your app. Refer to the screenshot below.
Next, from the ‘Permission’ tab add the scopes of files and folders so our application can write to Dropbox.
Finally, from the ‘Settings’ tab choose ‘No expiration’ of the Access token expiration field and click on the ‘Generate’ button which will give you an access token.
Configure Laravel Filesystem for Dropbox
Laravel provides a filesystem that gives drivers for local storage, SFTP, and Amazon S3. In order to add Dropbox as a driver, we need to configure it a little bit. It requires you to add a Dropbox adapter to the Laravel application. You can do it by following the steps below.
Install the spatie/flysystem-dropbox library using the command:
composer require spatie/flysystem-dropbox
Upon installing the library, create a service provider DropboxServiceProvider
using the below command.
php artisan make:provider DropboxServiceProvider
Next, define the custom driver dropbox
in the boot
method of DropboxServiceProvider
.
app\Providers\DropboxServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
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));
});
}
}
Register the service provider DropboxServiceProvider
to the providers
array of config/app.php
file.
'providers' => [
// ...
App\Providers\DropboxServiceProvider::class,
];
After this, add the ‘dropbox’ driver to the filesystem disks. Open the config/filesystems.php
file and add the ‘dropbox’ element to the disks
array.
'disks' => [
...
...
'dropbox' => [
'driver' => 'dropbox',
'authorization_token' => env('DROPBOX_AUTH_TOKEN'),
],
],
Here, I am getting the value of constant ‘DROPBOX_AUTH_TOKEN’ using the env()
method. It means we should add this constant to the .env
file.
.env
...
...
DROPBOX_AUTH_TOKEN=PASTE_ACCESS_TOKEN_HERE
Install and Configure Laravel Backup Library
So far, we are done with adding a Dropbox adapter to the Laravel application. Now, install the spatie/laravel-backup library which allows us to send backups on the different cloud services including Dropbox. Run the below command for installation.
composer require spatie/laravel-backup
Once the library is installed, publish the config file to config/backup.php
using the command:
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
After this, 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',
],
......
......
Optionally, you can also set your email address to which notification will be sent about the backup status. You will find this option in the mail
array of config/backup.php
file.
Backup Laravel Application on Dropbox
We have completed all the basic setup required for backing up the Laravel application on Dropbox. Go to the terminal and shoot the command below:
php artisan backup:run
The above command will create a ‘Laravel’ folder in your Dropbox account and store the backup zip inside it. This backup zip contains your Laravel application files, folders, and database.
This is the manual process of making a Laravel backup. The user can also schedule the backup task. For this, add the below code to the schedule
method of app/Console/Kernel.php
file.
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('06:00');
}
Here, I schedule the daily backup at 06:00. The user can change this time as per their requirement.
I hope you got to know about storing a backup of the Laravel application on Dropbox. I would like to hear your thoughts and suggestions in the comment section below.
Related Articles
- How to Upload file to S3 using Laravel Filesystem
- Upload Files to Amazon S3 Using AWS PHP SDK
- How to Upload Images to Another Server through FTP in Laravel
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
Thanks mate. Worked perfectly.
Thanks for sharing information.