Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ivymasterman's avatar

Laravel database backup schedule

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 :)

0 likes
2 replies

Please or to participate in this conversation.