In software development, we need to be ready for handling server crashes or data being mistakenly and this can cause a lot of trouble for the business. So developing a system that takes database backup regularly is considered best practice.
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 the backbone.
In this example, we will use mysqldump command line utility which can take backup of all types of table and provides command line interface. We will use exec() function to execute mysqldump command.
Further we will create a artisan command in Laravel which will take backup of database and stores into file system so whenever we lose some data we retrieve it.
Below are the steps to create automatic database backups in Laravel:
First of all, let's create new artisan command which will handle database export process and store exported file into file system. To create new command open terminal at root directory and enter below command :
It will create file at app/Console/Commands/DailyBackup.php. All out database backup functionality will be written here. Let's open it and modify as per our requirements.
In command, we have created signature and description for it. Into handle method we have created mysql dump command using .env file database configuration and created file name using current time.
Here, we have use exec() function to execute database export command. It will export database backup into file at our specified location.
After creating command we need to register it into Kernel.php. So it can be discovered and run when cron job is called.
To set command to run automatically, we need to add it into schedule function and set frequency of it. There are plenty of options are available with laravel. You can refer this link for it. For this example, we will set it to daily.
Make following changes to Kernel.php file :
In this step, we simply call our Daily backup command to schedule with daily frequency.
We can test our functionality using terminal in local or creating cron job on server. Here, we will test using terminal.
To test database backup functionality locally in Laravel, you need to open terminal and run below command :
It will create backup file to our backup path. This file can directly imported to any fresh database or we can use it for partial data import. You can set it to send this file directly to specific user through mail by modifying command.
While in server you can set up cron job to call laravel schedule and it perform daily database backup automatically.
Just for testing on server you can also create route that executes our artisan command like below example :
Here, we have created daily automatic database backup functionality in Laravel using artisan command and core PHP functions. We can extend it by adding some features like deleting old database backups or sharing database files via mail or more. Feel free to provide feedback or comment.
Ask anything about this examples