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.

My choice for the backup is the UpdraftPlus 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 package from Spatie is a great solution.

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. Install it with composer:

composer require spatie/flysystem-dropbox

As described in the Laravel documentation, 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 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 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.

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 🙂


  Follow me on on Twitter for more software development tips.