PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Upload your Laravel Database Backup to Dropbox

Upload your Laravel Database Backup to Dropbox

We always need to have a daily database backup because an emergency could happen at any moment in our server. Here I show you briefly how to backup our database to Dropbox in a Laravel application

Laravel Installation

  • Install Laravel 9 asa
  • Install javascript dependencies.
npm install
  • Create the database locally and register it in the .env file.

File .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_backup_dropbox
DB_USERNAME=root
DB_PASSWORD=
  • Run migrations.
php artisan migrate

Our database bd

Install Spatie Backup Package

Install the package spatie/laravel-backup

composer require spatie/laravel-backup

Publish the configuration file

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

Run the backup command

php artisan backup:run

Ejecutando comando backup

We can see that the command exported the backup successfully locally in the path below storage/app/Laravel

Backups

Dropbox Integration

Register in Dropbox in the next url, and click in Console App

Registro en Dropbox

Create a new application

Crear nueva aplicación Dropbox

Datos aplicación Dropbox

Configure the permissions to enable our application to save to Dropbox

Tab Permisos

Permisos Dropbox

Generate the token

Generar token

Save the token as a variable in our .env file

DROPBOX_AUTH_TOKEN=**********

Install the following package to integrate Dropbox

composer require spatie/flysystem-dropbox

Generate the service provider

php artisan make:provider DropboxServiceProvider

Register the service in the file

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

Add a new file system in the file below

"disks" => [
    "dropbox" => [
        "driver" => "dropbox",
        "authorizationToken" => env("DROPBOX_AUTH_TOKEN"),
    ],

Configure the service provider

<?php

namespace App\Providers;

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

class DropboxServiceProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend("dropbox", function ($app, $config) {
            $adapter = new DropboxAdapter(
                new DropboxClient($config["authorizationToken"])
            );

            return new FilesystemAdapter(
                new Filesystem($adapter, $config),
                $adapter,
                $config
            );
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Edit the file to add Dropbox as a new disk backup in the following file config/backup.php.

'destination' => [
    /*
     * The filename prefix used for the backup zip file.
     */
    'filename_prefix' => '',
    
    /*
     * The disk names on which the backups will be stored.
     */
    'disks' => [
        'local', 'dropbox'
    ],
],

That's all! Run the command below

php artisan backup:run

Ejecutar comando backup

Check that our backup has been uploaded to Dropbox in the following url link

Backup en Dropbox

As simple as that we can generate our laravel database backup to Dropbox.

In the next post, I'll be publishing about how to make automatics backups at a specific time

The project repository

Comments (0)

loading comments