Automatic database backups for Laravel applications to Dropbox

Automatic database backups for Laravel applications to Dropbox

I was tasked with setting up database backups for all of my Laravel applications. The idea was that in a case when an error happens or an outside force is at work, I would always have at least a days old database backup saved on a remote location.

I have a lot of Laravel 4 application that have not yet been upgraded to Laravel 5 and a few Laravel 5 applications, so I needed a simple way to backup both Laravel versions. In this post I will show you how to do backups for both versions.

We will be using the great Database Backup Manager package written by Shawn McCool, Mitchell van Wijngaarden and Graham Campbell.

Database backup manager for dumping to and restoring databases from S3, Dropbox, FTP, SFTP, and Rackspace Cloud

Installation

backup-manager by default is framework agnostic and in order to simplify this installation even more we will be using
Laravel Driver for the Database Backup Manager
.

This package pulls in the framework agnostic Backup Manager and provides seamless integration with Laravel.

Pull in the package through command line:

composer require backup-manager/laravel

Then, since we will be using Dropbox, we need to pull in the dropbox adapter:

composer require league/flysystem-dropbox

Laravel 4

For Laravel 4 applications add this to your service provider list in app/config/app.php:

BackupManager\Laravel\Laravel4ServiceProvider::class,

Copy the vendor/backup-manager/laravel/config/backup-manager.php file to app/config/backup-manager.php and configure it to suit your needs.

In backup-manager.php config file you will need to set key, token, secret, app and root for Dropbox, so be sure to copy the file as stated above.

Laravel 5

For Laravel 5 applications add this to your service provider list in config/app.php:

BackupManager\Laravel\Laravel5ServiceProvider::class,

And then publish the storage configuration file:

php artisan vendor:publish --provider="BackupManager\Laravel\Laravel5ServiceProvider"

Create a Dropbox API App

Go to Create a new Dropbox Platform app, select Dropbox API app and choose Yes to limit app access to only its own folder. Enter app name and click Create app.

Create a new Dropbox Platform app

On the second screen you will have to take note of app key and app secret. Then click on generate button to generate an access token. Write that token down somewhere, you will need to enter it in the config file backup-manager.php.

Create a Dropbox API App

Now go to backup-manager.php config file and enter the following:

'dropbox' => [
    'type' => 'Dropbox',
    'token' => 'your-token-here',
    'key' => 'your-key-here',
    'secret' => 'your-secret-here',
    'app' => 'your-app-name',
    'root' => '/backups',
],

This will store all your backups to a folder called backups under your App name root folder.

Test that everything works

After you have configured settings for Dropbox in backup-manager.php type this from the project root to test if everything works as expected:

php artisan db:backup --database=mysql --destination=dropbox --destinationPath=`date +\%s`-laravel4.sql --compression=null

%s stands for seconds since 00:00:00 1970-01-01 UTC. You can view more examples here

You will receive output something like this:

Successfully dumped mysql, compressed with null and store it to dropbox at /backups/1439902091-laravel4.sql

You can login to your Dropbox account now and verify that the file you just created is there. It will be in a folder called Apps under your Dropbox app name.

Create a cron job

Create a commit and push your changes to the server. The rest of this post is done on the server where your application is hosted at.

Login to your server using SSH.

Type:

crontab -e

If this is your first time running this command, you will be asked to choose an editor, I suggest using nano.

You will be presented will a file that looks like this:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

At the end of that file add a new line with the following:

0 0 * * * php /path/to/artisan db:backup --database=mysql --destination=dropbox --destinationPath=`date +\%s`-laravel4.sql --compression=null

The above job creates a new backup every day at midnight and stores in on dropbox.

Mario Bašić

About Mario Bašić

Sometimes backward to move forward. Always.