How to Set up Automatic Laravel Backup for database and files?
Do you want to schedule an automatic backup of the Laravel application? Keeping the backup of the app's database and files very important. You should have your backup prepared whenever if something turns out badly with your server or application. In this article, you will learn how to take a backup of the Laravel application with the database and files.
The Spatie team has created a package called spatie/laravel-backup, which helps store your Laravel backup. The best thing about this package is that it gives an option that can schedule your backup process.
Let’s take a look at how to take the backup of the Laravel application with the database.
Requirements
The current stable version of the spatie/laravel-backup
package is v6.13. And this version requires PHP 7.3, with the ZIP module and Laravel 6.0 or higher.
If you are using an older version of Laravel, take a look at one of the previous versions of this package.
Installation & Setup
You need to run the following command in the terminal to install this package:
composer require spatie/laravel-backup
Once the package is installed, run the next below command that will publish the config file to config/backup.php
.
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
In this configuration file, configure your options for backup. For example, you can include
and exclude
the directories for backup. By default include
option as base_path()
that is going to take the backup of entire Laravel files. And in the exclude
option has base_path('vendor')
, base_path('node_modules')
. It means don’t add vendor
and node_modules
directory in the final backup.
In the databases
option specify the names of the connections to the databases that should be backed up. That package supports MySQL, PostgreSQL, SQLite, and Mongo databases.
Next, the important option is disks. Here specify the disk names where the backups should be stored. By default here local
disk is given that is going to store the back in the storage/app/
directory.
And also add the mail
options, where it is going to send email backup notifications.
Taking backups
Now We are all set to run our first backup. Open the terminal in your project root directory and run the following command:
php artisan backup:run
Scheduling for automatic backup
In most cases you'll want to schedule backup so you don't have to manually run the backup:run every time you need a new backup. You can implement this using the cron job.
To enable cron job for your project run the following command:
crontab -e
It will open the Crontab file. Now add the following entry to your server’s crontab file:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
Make sure to replace /path/to/artisan
with your project's artisan path.
Now, we can automate our backup task. For this, you should write the below code in the app/Console/Kernel.php
file.
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:clean')->daily()->at('01:00');
$schedule->command('backup:run')->daily()->at('02:00');
}
The times used in the code above are just for examples. You can adjust them according to your requirements.
That’s it! It’s all about taking a backup of the Laravel application.
tarting backup... Dumping database rakaf... Backup failed because The dump process failed with exitcode 127 : Command not found : sh: mysqldump: command not found .
i am having latest laravel , but i faced an error?
I guess, you are getting this error in crontab. Follow this steps to fix this:
Step 1: find the location of
mysqldump
bin file. You can find it by executing following command in your terminal.Step 2: Open crontab using following command:
And add the following code in the beginning of the file:
for example let's say path of
mysqldump
is/usr/local/mysql/bin/mysqldump
, then add following snippet: