DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Website Backups in 7 Simple Steps

Making sure your data is stored safe and sound is always a good idea. Read on to learn how to backup a Laravel-based web app.

Łukasz Mądrzak user avatar by
Łukasz Mądrzak
CORE ·
Dec. 21, 18 · Tutorial
Like (5)
Save
Tweet
Share
20.51K Views

Join the DZone community and get the full member experience.

Join For Free

This tutorial will teach you how to create a complete backup of your Laravel application with a database in minutes. To accomplish this we will use an amazing open source library by Spatie that I found.

This tutorial is inspired by a story I’ve heard not so long ago. The story goes as follows.

Company X was using a custom cloud application for managing their website content. After over a year of the honeymoon phase, where everything worked as intended, BAM. One of the employees shows up to work on a fine autumn’s morning and, to their surprise, they could not log in to the system. The app returned a message that their credentials were not recognized. That’s surprising because they hadn't changed their password in over a year. Yes, despite the constant nagging of the compliance department about the fact that passwords should be changed at least once every quarter. It turns out a careless sys admin has mistyped something somewhere and all the data the cloud application used to work off is now gone.

But it’s all fine because surely they had an elaborate backup mechanism in place! Right?

Well turns out they didn’t.

I like to learn from mistakes but I prefer to learn from other people's mistakes. So this prompted me to review my own backup system. And, by the way, I decided to write a short tutorial on how you can easily backup your website so you can learn from some else’s mistake too.

Here are the steps.

Setup

In the terminal, navigate to your Laravel project and run the following command:

composer require spatie/laravel-backup

Then run the following command to reveal the config.php file:

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

Now, in your favorite IDE, head over to the backup.php file in the config folder.

  1. Setup your email address.
'mail' => [
    'to' => 'your@example.com',
],

The above email will be used for scheduled backups to notify you of their success, failure, or even of the health of your backups. Note that the above will not work unless you have an email service setup. I personally use Amazon Simple Email Service (SES) which allows up to 50k of free emails per month. If you didn’t know about it, it’s worth checking out.

On the other hand, if you don’t have an email service and do not care about being notified, set the above field to an empty string.

2. Add prefix to the backup file name (optional)

'filename_prefix' => 'backup-',

3. Make sure that, in your environment file (.env), you have specified a meaningful app name.

Your backups will be generated into /storage/app/<your_meaningful_app_name>/backup-2018-12-18-17-22-38.zip.

4. Set a limit on the amount of space you want the backups to take up (important).

By default, it’s set to 5GB, which is quite a lot. This is my suggested setting:

/*
 * After cleaning up the backups remove the oldest backup until
 * this amount of megabytes has been reached.
 */
'deleteOldestBackupsWhenUsingMoreMegabytesThan' => 800,

5. Compress

As we want to ensure that our backups do not take up our entire space in the “cloud,” why not compress them? In the config/backup.php file, find the following line:

'database_dump_compressor' => null,

and change it to

'database_dump_compressor' => Spatie\DbDumper\Compressors\GzipCompressor::class,

For science, I have seeded a users table with 10,000 fake users. Then I ran the backup scripts without compression and checked the size of the files. The uncompressed backup was 1.2MBs.

Image title

I then enabled backup compression, which almost halved the size of the backup. Hip-hip-hooray!

Image title

6. Backup Monitoring

The library provides you with another awesome command. The command to check the health of your backups! It’s also configurable but it will basically check if the latest backup is not older than 1 day and that the backups do not exceed a certain size.

php artisan backup:monitor

This is the result of running the command before I made any backups:

Image title

I then ran a backup command and checked the health of my backups:

Image title

7. Automate your life.

Automation is your friend. You do not want to have to think to run this command every day, week, or however often you would like to back up your data. Schedule it using cron to run every night and sleep well without any worries about losing any of your data.

Conclusion

If only Company X knew about this library, they would have saved themselves a lot of time and money. At least now you know, so you don’t have any excuses to not back up your data.

Here’s the Github repo:

https://github.com/spatie/laravel-backup

Backup

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Iptables Basic Commands for Novice
  • Utilize OpenAI API to Extract Information From PDF Files
  • Best Practices for Writing Clean and Maintainable Code
  • Top 5 Node.js REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: