Oh ****, I should have had a backup
Tags: article, laravel, mysql, backmeup
Published: 2020-10-25 19:19:11

---
title: Oh ****, I should have had a backup
published: true
date: 2020-10-25 19:01:44 UTC
tags: Article,Laravel,MySQL,backmeup
canonical_url: https://www.danielwerner.dev/oh-i-should-have-had-a-backup
---

Just like the unit or integration tests in development, the backups may seem like unnecessary wasting time, money and effort… until a certain point. With tests this point usually comes when we do a “quick fix” on Friday afternoon, deploy it to production and realize that we unintentionally broke the whole system. The following weekend is not going to be nice for the dev team.

The situation with the lack of backup is even worse. Someone hacks into your system. The database server crashes. A developer runs an update script on production but forgets the where clause. These things happen sometimes, and we should be prepared for it by having a good backup plan.

In this post I’d like to introduce some useful packages and plugins what I recommend for backups.

## Backup a wordpress site

I know. WordPress has a low reputation amongst php developers. But I think WordPress is quite good when we use it for its primary purpose: a blog with some additional pages which can be edited in the admin interface. My blog also runs on WordPress. It was the quickest way to set it up. Unfortunately, I am not so active in blogging as I’d like to be, but it is still important to have regular backups of my content.

![](https://www.danielwerner.dev/wp-content/uploads/2020/10/UpdraftPlus_Logo___Small.png)

My choice for the backup is the **[UpdraftPlus](https://updraftplus.com/)** plugin, because:

- It has high rating
- Easy to install
- Has the ability to backup database, themes and uploads
- Has plenty of options to store the backup: local, Amazon S3, Google Drive, Dropbox just to name a few
- The backups can be scheduled
- It is free, but also have a pro plan if you need advanced features

## Backup Laravel projects

Lately I mainly work with Laravel, so obviously these projects should also have a backup solution. As Laravel has a big and great ecosystem, it is quite easy to find a package for backup as well. For my needs the [laravel-backup](https://spatie.be/docs/laravel-backup/v6/introduction) package from [Spatie](https://spatie.be/) is a great solution.

![](https://www.danielwerner.dev/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-5.17.45-PM-1024x330.png)

Although, it was not a life saver for me yet (fortunately), I already thank Spatie for it.

The documentation of this package is great as usual. I’d like to do a really quick introduction how to install and configure this package to backup a Laravel project and store the backup on dropbox.

Let’s start with installing the backup package with composer:

```
composer require spatie/laravel-backup
```

Publish the configuration file as usual:

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

The above command will publish the `config/backup.php` to your project. It has plenty of options to configure different aspects of the backup, I’ll focus on configuring the disk under the `disks` key.

We want to use dropbox as a backup storage, let’s configure the disk first. To use dropbox as a Laravel disk, we’ll use a package (again from Spatie): [flysystem-dropbox](https://github.com/spatie/flysystem-dropbox). Install it with composer:

```
composer require spatie/flysystem-dropbox
```

As described in the [Laravel documentation](https://laravel.com/docs/8.x/filesystem#custom-filesystems), let’s create a service provider for dropbox:

```
<?php

namespace App\Providers;

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

class DropboxServiceProvider extends ServiceProvider
{
    _/\*\* \* Register any application services. \* \*_ **_@return_** _void \*/_ public function register()
    {
    //
    }

    /_\*\* \* Bootstrap any application services. \* \*_ **_@return_** _void \*/_ 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 in your `config/app.php` configuration file:

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

Let’s add our new disk to the `config/filesystems.php`:

```
        'dropbox_backup' => [
            'driver' => 'dropbox',
            'authorization_token' => env('DROPBOX_ACCESS_TOKEN')
        ],
```

Generate the authorization token on the [App Console](https://www.dropbox.com/developers/apps) in Dropbox. I ‘d like to mention that I had problems with setting up the backup when I selected the **App folder** permissions for my dropbox app, so I used the **Full Dropbox access** and it worked fine. Once the app is created, generate the access token without expiration, and add it to the .env file:

```
 DROPBOX_ACCESS_TOKEN=yoursecretaccesstoken
```

Add this new disk to the `disks` key of your `config/backup.php`:

```
'disks' => [
    'dropbox_backup',
],
```

Now everything is ready to schedule a backup, or to create one manually by running the following command:

```
php artisan backup:run 
```

## Backup any project with Backmeup

I’ve created a small project called [Backmeup](https://github.com/daniel-werner/backmeup) to have a solution to backup any kind of web projects using Dropbox. It is basically a collection of shell scripts. You can recursively archive the configured directory, dump all databases with mysqldump and compress all these files using tar.gzip. It uses the native linux dropbox client to upload the archive to dropbox. I’ve already written a blog post about it, so you can find more info [here](https://www.danielwerner.dev/automated-linux-server-backup-with-dropbox/).

## Conclusion

Of course there are other methods to create regular backups of your important data including but not limited to creating images of your VPS, or use the backup service of the hosting provider, etc. The most important is to HAVE backup up and running way before you eventually need them. You’ll thank yourself afterwards ![🙂](https://s.w.org/images/core/emoji/13.0.0/72x72/1f642.png)

The post [Oh \*\*\*\*, I should have had a backup](https://www.danielwerner.dev/oh-i-should-have-had-a-backup/) appeared first on [Daniel Werner](https://www.danielwerner.dev).