Backing up your Laravel Application

Keep your data safe — set up automated backups in under an hour.

Welcm
4 min readNov 16, 2018

Introduction — Why backup?

So you’ve designed, built, tested and deployed your new application, and if all these stages have been successful, hopefully you now have an essential and popular application to manage and maintain.

Of course your application source code will be safely and securely stored in the online repo of your choice (…right? If not, do it now!), be it GitHub, Azure DevOps, BitBucket or similar, but what about your new and rapidly growing application database and other user-generated files?

If you have users relying on your application, you must make sure you have a backup and disaster recovery plan in place. There’s a well-known adage when it comes to maintaining backups:

There are two types of people: those who backup, and … those who have never lost their data.

It’s easy to just rely on the comforting words of your web host regarding backups and assume they will have everything covered, but there are a number of benefits of maintaining your own backups including:

  • You get to set the schedule and frequency of backups
  • You still have access to your data in the event of account issues with your host
  • The data can reside in a physically different location of your choice

Our recommendation

We recommend implementing laravel-backup by Spatie. With over 1.2 million downloads, a solid set of useful features and comprehensive documentation, it can be implemented into your Laravel project and configured in a matter of minutes.

Prerequisites

We’ll be assuming you’re as up-to-date as possible with your Laravel project so you’ll need:

  • PHP 7+
  • Laravel 5.5+

If you’re running older versions there are compatible versions of laravel-backup but they don’t benefit from new feature additions so upgrade if you can.

Getting started with laravel-backup

The laravel-backup package can be installed with composer in the usual way, just use:

composer require spatie/laravel-backup

You’ll need to set a few configuration options, so publish the configuration file by using:

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

The config file will be available in config/backup.php

The config is very clear and easy to understand, you can enable or disable notifications, set the backup and cleanup schedule, change storage locations and specify files to exclude. Make sure you add your email address here:

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

You will receive backup success or failure notifications at the address you specify here.

If you need any help with the configuration, check out Spatie’s documentation.

Once you’re happy with your configuration, you can run the backup process by using:

php artisan backup:run

You should see the status of the backup process in the console and receive an email if you have configured your email address. Backups by default are found in storage\app\*APPNAME*\*TIMEDATESTAMP*.zip

The package can also clean up and remove old backups based on your configuration settings to save space, run this by using:

php artisan backup:clean

Recommendations

So hopefully now you can run the console command and your application will be successfully backed up, which is a great start, give yourself a pat on the back!

But wait! There are a few more things you can do to improve this setup. The first, and quickest is to automate the backup. To do this you can add the commands to Laravel’s schedule function in app/Console/Kernel.php, adjust the times and intervals to your preference:

// app/Console/Kernel.phpprotected function schedule(Schedule $schedule)
{
$schedule->command('backup:clean')->daily()->at('01:00'); $schedule->command('backup:run')->daily()->at('02:00');
}

If you’re not already using Laravel’s Task Scheduler, you’ll need to make sure you have added a Cron entry on your server at an interval of your choice, this example will run the scheduler every minute:

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

Backing up to another location

By default your backups will reside on the same server as your application. In the event of a major server problem, this could leave you without backups, so it’s a good idea to specify a separate external backup location.

Luckily laravel-backups has support for Google Drive and Dropbox. We recommend Google Drive and whilst the setup steps would take a separate post, there already is an excellent guide by Dennis Smink here to get your Google Drive backups running.

Conclusion

We hope this has helped explain why maintaining your own backups for your application is important, and how you should be able to set this up in under an hour, including automating the process on a schedule of your choice.

We know at Welcm how important your data is, and so privacy, security, backups and recovery are built into everything we create by design, including our own Visitor Management solution.

We develop touch screen applications at Welcm Software and help customers with unsupported kiosk software. Get in touch to see how we can help with new software development or support and disaster recovery plans for legacy software.

Like this story? Please follow us on Twitter.

At Welcm we design, develop and support touch screen applications and systems.

If you have a project you would like to discuss please send an enquiry from our contact page, email us at enquiries@welcm.uk or call us on 01252 950 650.

We also make Visitor Management Easy at https://welcm.ly

--

--

Welcm
Welcm

Written by Welcm

We design, develop and support websites, apps and custom software. Find out more at https://welcm.uk. We also make Visitor Management Easy — https://welcm.ly

No responses yet