Backup your Laravel 5 App to Dropbox

Want a free way to backup your laravel app and database to dropbox? This guide should help!

Packages used

Step 1

Follow the steps to install spatie/laravel-backup and thephpleague/flysystem-dropbox

Step 2

Create a file name DropboxFilesystemServiceProvider.php in app/Providers/. Then add App\Providers\DropboxFilesystemServiceProvider::class to your providers in config/app.php

Below is a copy of my DropboxFilesystemServiceProvider.php. Take a look at $config[‘accessToken’] and $config[‘appSecret’]. You’ll get these when you create a dropbox app. You can set these values in your .env file.

<?php

namespace App\Providers;

use Storage;
use League\Flysystem\Filesystem;
use Dropbox\Client as DropboxClient;
use League\Flysystem\Dropbox\DropboxAdapter;
use Illuminate\Support\ServiceProvider;

class DropboxFilesystemServiceProvider extends ServiceProvider
{
public function boot()
{
Storage::extend('dropbox', function ($app, $config) {
$client = new DropboxClient($config['accessToken'], $config['appSecret']);

return new Filesystem(new DropboxAdapter($client));
});
}

public function register()
{
//
}
}

Step 3

Create a Dropbox API App

Make sure you generate an access token. Save the access token and app secret in your .env file. Generating an access token will create a folder Apps in your Dropbox folder. This is where your backups will be saved.

Step 4

Edit config/laravel-backup.php and set ‘disks’ => [‘dropbox’] for destination and monitorBackups. If you do not have a laravel-backup.php file in your config folder, run php artisan vendor:publish — provider=”Spatie\Backup\BackupServiceProvider”

Step 5

Add a dropbox filesystem to config/filesystems.php

'dropbox' => [
'driver' => 'dropbox',
'accessToken' => env('DROPBOX_ACCESS_TOKEN'),
'appSecret' => env('DROPBOX_APP_SECRET'),
]

Step 6

Edit kernel.php in app Console/. This will run your backup and cleanup old backups. You can define how many backups you save in config/laravel-backup.php.

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
$schedule->command('backup:clean')->daily()->at('01:00');
$schedule->command('backup:run')->daily()->at('02:00');
}

Step 7

Setup scheduler to run your backup. You can do this from a cron job or if you’re using Forge, you can set it up there. More documentation here.

Screen Shot 2015-07-09 at 9.47.32 PM

That’s It!

These same steps can be used to backup your app to several other cloud services. To use other FileSystems, check out Flysystem.

Hope these steps help you setup your own Laravel backup. Please leave a comment below if you have any issues.


Originally published at Tim Leland.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.