Laravel Database Backup with Spatie Backup

As we already know database backup is an essential part of the software development process. But sometimes it will become messy or time taking to manually take database backups and check which database is older and delete older databases. You will feel great after knowing Spatie Laravel-Backup library can perform most of the database backup operating easily. like creating database backups, handling events (success or failure), or deleting old database backups which take up unnecessary space.

Database backups are essential for protection against data loss that can disrupt operations and could lead to major problems specially with E-commerce, ERP, HRMS, or any other system that data is backbone.

In this example we will take database backup using Spatie Laravel backup library. We will follow below steps for better understanding:

  1. Create Laravel Project
  2. Configure Database
  3. Install & Configure Laravel Spatie Package
  4. Setting up backup
  5. Take Backup

Create Laravel Application

Let's begin with creating new laravel application or you can use existing application. Open your terminal end enter appropriate command:

It will take some time to create project.

Database Configuration

Our application is created. Let's configure database for our application. If you have don't have any database then create a new one. After creating database open .env file from root directory of your project. if .env file is missing from project then copy content from .env.example and create file. .env file defines many common environment variables

Install and Configure Laravel Spatie Package

To install package we will use composer. Open your terminal and enter below command :

This command will download and extract package using composer and install it into our application. While using Spatie database backup, we need to consider that Spatie will take backup and send it through email. So, we need to configure mail configuration in our application.

Add the mail address in the .env file.

Next step we will register service provide to our application. Get inside config/app.php and search for package service provide and modify it.

We also need to publish some package files. Open terminal and enter below command:

Setting up backup

After installation you can set spatie database configurations as per your requirements. In general use, pre-defined configuration is great but sometimes you need to change some values. You can achieve it by modifing config/backup.php file.

Take backup

Our setup is ready to take backup. We will take all the files and database using below command.

It will take database backup.

Conclusion

The backup of the Laravel application is necessary. Here, we have created Laravel app and database backup using Spatie backup for beginning to ending.


Share your thoughts

Ask anything about this examples