database backup without any package in laravel 10

Spread the love

Hello Guys,

In this article, we will explore how to perform daily database backup in Laravel 10 without relying on external packages. If you’re interested in weekly database backups for Laravel 10, you can find that information here. We will provide a step-by-step guide on how to set up monthly database backups in Laravel 10, with a focus on automated backup procedures.

Maintaining the integrity of large websites with critical data is crucial. To ensure the safety of such data, we often need to create daily, weekly, or monthly database backups. Achieving this requires the use of cron scheduling to automate the backup process. This article will guide you through the setup of an automatic database backup system in Laravel 10.

In this example, we’ll demonstrate how to create a ‘database:backup‘ command and schedule it to run daily. This command will generate a backup of your database and store it in the designated storage folder.

Let’s take a few steps to use Laravel to set up automatic daily database backup without any package.

Step 1: Create a Command

In this phase, we’ll leverage Laravel’s artisan command to craft the DatabaseBackup console command. Follow these steps to initiate the command:

Also Read: How to use Drag and Drop Datatable Rows for Sorting using laravel 10

php artisan make:command DatabaseBackUp

We’ve just generated DatabaseBackUp.php within the console directory. Now, it’s time to incorporate a daily update code into this file.

namespace App\Console\Commands;
use Illuminate\Console\Command;
class DatabaseBackUp extends Command
     * The name and signature of the console command.
     * @var string
    protected $signature = 'database:backup';
     * The console command description.
     * @var string
    protected $description = 'Command description';
     * Execute the console command.
    public function handle(): void
        $filename = "backup-" . now()->format('Y-m-d') . ".gz";
        $command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . "  | gzip > " . storage_path() . "/app/backup/" . $filename;
        $returnVar = NULL;
        $output  = NULL;
        exec($command, $output, $returnVar);

Step 2: Create a Backup Folder

During this phase, it’s essential to establish a ‘backup‘ directory within your storage folder. To do so, follow the steps outlined below:

Also Read: How to Upload Images with Spatie Media Library in LARAVEL 10


Be sure to grant permission for the backup file storage/app/backup to be placed.

Step 3: Schedule a Command

In order to proceed, it’s essential to schedule the previously written command. To do so, let’s make the necessary adjustments to the kernel file, as shown below:

namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
     * Define the application's command schedule.
    protected function schedule(Schedule $schedule): void
     * Register the commands for the application.
    protected function commands(): void
        require base_path('routes/console.php');

To ensure a database backup, you can manually validate the following command:

php artisan database:backup

A single backup file will be generated and stored in your designated backup directory. Feel free to access it from there.

Step 4: Setup on Your Live Server

You can easily configure cron jobs on your server. To schedule tasks, simply include the following line in your server’s crontab file.

Also Read: How to use Multiple Database Connections in LARAVEL 10

Execute this command:

crontab -e

You can include the following line in your crontab file, but be sure to customize the directory path as needed for optimal performance in your scheduled tasks.

* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Dive into our collection of Laravel blogs – your go-to source for expert insights.

Leave a Comment