
When building applications that handle critical data, it’s essential to implement a reliable backup strategy. In Laravel 12, you can automate your database backups on a daily, weekly, or monthly basis using Laravel’s task scheduling system.
In this guide, we’ll walk you through the process of setting up automatic database backups in Laravel 12. You’ll learn how to create a custom Artisan command that backs up your database and how to schedule it to run automatically.
Why Backups Matter
Data loss can happen unexpectedly—hardware failures, human errors, or system bugs can corrupt or delete vital information. Implementing automated backups ensures you have recovery points and can minimize the impact of such incidents.
Steps to Configure Automated Database Backups in Laravel 12
✅ Step 1: Create a New Laravel Project (Optional)
If you’re starting fresh, you can install a new Laravel 12 application using Composer:
composer create-project laravel/laravel db-backup
✅ Step 2: Create a Custom Backup Command
Use Artisan to create a new console command:
php artisan make:command DatabaseBackUp
This will generate a file located at app/Console/Commands/DatabaseBackUp.php.
Update the generated file with the following logic:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class DatabaseBackUp extends Command
{
protected $signature = 'database:backup';
protected $description = 'Command description';
public function handle()
{
$filename = 'backup-' . now()->format('Y-m-d') . '.sql';
$mysqldumpPath = 'D:\\xampp\\mysql\\bin\\mysqldump.exe';
$command = sprintf(
'"%s" --user=%s --password=%s --host=%s %s > %s',
$mysqldumpPath,
env('DB_USERNAME'),
env('DB_PASSWORD'),
env('DB_HOST'),
env('DB_DATABASE'),
storage_path('app/backup/' . $filename)
);
exec($command);
$this->info('Database backup (uncompressed) created: ' . $filename);
}
}
Note: Ensure that mysqldump is installed and accessible from your server’s command line.
✅ Step 3: Create the Backup Directory
Before running the backup command, you need a folder to store the backups:
storage/app/backup
Make sure you give permission to put the backup file.
✅ Step 4: Schedule the Backup Command
To run the command automatically, schedule it in Laravel’s task scheduler.
Open routes/console.php and add the following:
use Illuminate\Support\Facades\Schedule;
Schedule::command('database:backup')->daily();
You can also schedule it weekly or monthly like this:
Schedule::command('database:backup')->weekly();
Schedule::command('database:backup')->monthly();
To test it manually, run:
php artisan database:backup
This should create a new .gz backup file in storage/app/backup.
✅ Step 5: Configure Cron on the Server
To enable Laravel’s task scheduling in production, add a cron job on your server:
Run:
crontab -e
Then add the following line:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
OR
* * * * * cd /var/www/laravel-project-folder && php artisan schedule:run >> /dev/null 2>&1
Replace /path-to-your-project/ with the full path to your Laravel application’s root directory.
This cron job will execute Laravel’s scheduler every minute, and Laravel will handle the timing of your backup command.
Final Thoughts
Automated database backups are a crucial part of any robust deployment strategy. With Laravel 12, setting up scheduled backups is straightforward and integrates seamlessly into your application workflow.
By following the steps above, you can rest easy knowing your application data is being backed up on a regular basis.