Backup a Laravel Website to DigitalOcean Spaces

In my 10 years of Software Development career I have seen enough and more people make terrible mistakes with web apps where their mistakes are irreversible. Life doesn't have an undo option like most of our apps do with CMD+Z or CTRL+Z.

An example for these kinds of mistakes is losing the production database or a worst case an attacker erases your entire database. Trust me it has happened to me once in my early career. Well, to be honest it happened to me twice.

I am not going to talk about it today, rather I am going to focus my energy on writing this post to help you avoid this situation preemptively.

Summary

  1. Configuring DigitalOcean Spaces as a file system driver in your Laravel app
  2. Installing spatie/laravel-backup
  3. Scheduling backups
  4. Conclusion

Step 1. Configuring DigitalOcean Spaces as a file system driver

First of all we need to install league/flysystem-aws-s3-v3 package. Yeah, you guessed it right. aws-s3 is compatible with DigitalOcean Spaces and vice versa.

composer require league/flysystem-aws-s3-v3

Now we have to add the disk in config/filesystems.php

'spaces' => [
    'driver' => 's3',
    'key' => env('DO_SPACES_KEY'),
    'secret' => env('DO_SPACES_SECRET'),
    'endpoint' => env('DO_SPACES_ENDPOINT'),
    'region' => env('DO_SPACES_REGION'),
    'bucket' => env('DO_SPACES_BUCKET'),
],

You can either provide the default keys or add the configuration values in your .env file.

DO_SPACES_KEY=YOUR_SPACES_KEY_HERE
DO_SPACES_SECRET=YOUR_SPACES_SECRET_HERE
DO_SPACES_ENDPOINT=https://sfo2.digitaloceanspaces.com
DO_SPACES_REGION=SFO2
DO_SPACES_BUCKET=YOUR_BUCKET_NAME_HERE

I left DO_SPACES_ENDPOINT and DO_SPACE_REGION to the default values but you have to adjust it accordingly.

Now we are ready to move to the actual backup part.

Step 2. Installing spatie/laravel-backup

Let's install freek's package for laravel-backup. Make sure to check the docs here.

composer require spatie/laravel-backup

This package registers the service provider automatically (assuming you are using Laravel 5.5+), So we can move to the next step and publish the configuration file.

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

Above command will create a config file names config/backup.php

All you have to do is change the disk on the backup.php file.

  'name' => 'your-backup-folder-in-spaces-bucket',
     ........   
  'destination' => [
              'filename_prefix' => '',
              'disks' => [
                  'spaces',
              ],
          ],

You can also configure the notification channels to get notified on backup events.

'mail' => [
            'to' => 'your@example.com',
        ],

I like to setup slack notification because email is little annoying. It's totally up to you.

We are ready folks! Now you can simply run php artisan:backup run to backup your app to DigitalOcean Spaces.

Step 3. Scheduling backups

You can add scheduled commands to automate these backups. Make sure you have your cron job set on the server.

// 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');
}
Of course, the times used in the code above are just examples. Adjust them to suit your own preferences.

4. Conclusion

That's it folks. You can breath now. But you don't have to stop here. Please check the docs and make sure you are backing up the correct folders and correct database.

Feel free to comment here if you have any questions or suggestions. If you get any issues don't break your head. I can help your debug your code. Feel free to request a session from me on codementor.

I'll see you in another post. Have a nice day :)






Related articles

Signature Pad with Alpine.js

The post describes the implementation of signature pads with Alpine.js. It also discusses how to integrate it with laravel livewire.