Home > Enterprise >  Laravel database backup schedule
Laravel database backup schedule

Time:06-09

I am trying to set up a scheduler for weekly database backup in laravel. I have created a command and filled out some data like the command itself and description, and registered it in the console kernel as well. The issue is that the file never gets created and/or stored in storage.

This is the part of the code where is the command:

public function handle()
{
    Log::info('Database backup completed.');

    $filename = 'mysite' . Carbon::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);
}

This is the kernel part:

/**
 * The Artisan commands provided by your application.
 *
 * @var array
 */
protected $commands = [
    OtherCron::class,
    DatabaseBackupCron::class,
];

/**
 * Define the application's command schedule.
 *
 * @param Schedule $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
     $schedule->command('othercron')->dailyAt('00:00');
     $schedule->command('database-backup:cron')->everyFiveMinutes();
}

Note: I have used "->everyFiveMinutes()" just for testing purposes :)

CodePudding user response:

You could use a ready-made lib for that:
github.com/spresnac/laravel-artisan-database-helper
and then just call the command in the scheduler as you like ;)

You can also set the full path to your mysqldump binary, if it's not in your path ;)

CodePudding user response:

php artisan make:command DatabaseBackUp

Use this command make databasebackup file.

app/Console/Commands/DatabaseBackUp.php
<?php
  
namespace App\Console\Commands;
  
use Illuminate\Console\Command;
use Carbon\Carbon;
   
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';
  
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
  
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $filename = "backup-" . Carbon::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);
    }
}

In this step, we need to create "backup" folder in your storage folder. you must have to create "backup" on following path:

storage/app/backup

Now, in this step, we need to schedule our created command. so let's update kernel file as like bellow:

app/Console/Kernel.php
<?php
  
namespace App\Console;
  
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
  
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\DatabaseBackUp'
    ];
  
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('database:backup')->daily();
    }
  
    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
  
        require base_path('routes/console.php');
    }
}

you can check following command to getting database backup with this command:

php artisan database:backup

t will create one backup file on your backup folder. you can check there.

Now, we are ready to setup cron on our server.

At last you can manage this command on scheduling task, you have to add a single entry to your server’s crontab file:

Run following command:

crontab -e
  • Related