How to backup laravel app and database?

How to backup your laravel app and database?

In this article, we are going to back up our laravel application along with its database. For this, we will use a package called laravel-backup by Spatie. This package will back up our application files into a zip file including the database.

Need for Backup

Why do we need to back up our application? The main reason for this is that we don’t want to lose any of our files and database. No hosting provider can guarantee the 100% uptime to your server. Popular hosting provider such as DigitalOcean, HostGator, GoDaddy, and others have also been down sometimes.

A small hope to get the latest database despite down server is that some hosting providers also provide database backup feature. The problem to this is that database is backed up weekly not daily. So, when the server is down on last day before backup, then you lose your 6 days data.

Solution

To provide a solution to such problems, we are going to implement this feature. As mentioned, we will use laravel-backup package. This package provides other many features besides backing up files and database. Some of the highlighted features are:

  • Backing up files and database
  • Cleaning up old backups
  • Sending out notifications
  • Monitoring the backups

After the introduction, let’s start implementing in our project.

Project Setup & Migrations

We will backup our fresh laravel application along with its default laravel authentication.
Create Project
Let’s create a fresh projet using composer.

Database Setup
After creating project, let’s setup database. For this, create a new database and modify our .env file accordingly.

Migration
We will migrate our default migration files that is used for authentication. To migrate, just run the artisan command:

Seeding Database
Let’s seed our users table using tinker. If you are new to tinker package, you can learn about How to use Tinker in Laravel Application. To seed our users table, we will run the following command:

Install Package

If you have been following spatie packages, they are pretty much updated about the latest version of everything including PHP and Laravel. So, they have precise requirements for installation of their package. Before installing this version of the package, please make sure that you meet these requirements.

  • Requires PHP 7 or higher
  • Requires Laravel 5.3 or higher
  • Need enough free space for backed zip file
  • Should have mysqldump installed to backup MYSQL DB
  • Should have pg_dump installed to backup Postgre SQL DB
  • Should have mongodump installed to backup Mongo DB
  • Optional guzzlehttp/guzzle package for Slack notification

Let’s install our laravel-backup package. We will install via composer. Run the command below:

Publish Configuration

This version (v5) will automatically register its service provider. We will publish config files with the command below:

This will create a new config file config/backup.php. If you open it, you will find a different configuration option.

Running Backup

Now, we can start backing up our application. To backup all the files including database, run the following command:

This will output as screenshot below:
How to backup your laravel app and database - kodementor

This will backup our files in the directory specified in configuration. By default, it will export to App\storage\app\Laravel directory. We can also only backup application files without database with the following command:

Cleaning Backup

At some point of time, we may need to clean up our old backed storage. For this, run the command below:

Output:
How to backup your laravel app and database - kodementor

You may see that the running the command did not delete your old files. This is because deleting old backup files is a bit tricky. You need to understand the basic rules for this. The rules for cleaning old backups can be found in this link. We will get a notification email when something goes wrong during this process.

Monitoring Health of Backup

Another useful feature of this package is that we can check the health of the backed up files. Normally, if the backup file is too far old or if the amount of storage space required for all backups is not available, then the backup is considered to be unhealthy.

We can check the status of our backup using the following command:

As stated in the official documentation, “To install the monitor follow the regular installation instructions. Instead of scheduling the backup:run and backup:clean commands, you should schedule the monitor command”.

Besides this, you can configure more monitor settings from the configuration file.

Notification

The laravel-backup package send notifications to user through mail and slack. But, we should install guzzlehttp/guzzle for slack. By default, we get mails for every actions. If you are want to know more about sending notifiations in laravel, you can read this article title “” which explains pretty well about notification.

If you want to dig in more detail about the notification classes, follow the below code in backup.php.

Below is a screenshot of emails sent during the execution of backup commands.
How to backup your laravel app and database - kodementor

Conslusion

Please note that, backing up the application files back up all your files including configurations and other sensitive credentials. So, be sure to make the file secure on the server. These are some of the main functionalities of laravel-backup package But you can dig in more details about other features as well in the official documentation.