Do I need / can I run laravel backups froma controller?

47 views Asked by At

Here's a bit of context:

  • Running Laravel 10, and SPATIE Backup v8;

  • An admin should be able to choose, and this choice will be stored in a DB, to shedule backups (whatever the options (DB only , Files only , or both ..) or the timeframes ( 'e.g. ->every weeks, daily, etc...), those not strictly relevant to my question :

  • According to the admin's choice, a backup could be run immediately (upon click of a button, lauching a route, I guess...) or using a scheduled command , with the relevant options (I have not yet decided I I should suggest weekly or monthly backups, but these options should still be "allowed" / availables as scheduled tasks, obviously...

  • Apparently using the backup commands , which are normally CLI commands within controllers is complicated at best, or plain impossible ?

Anyway could someone kindly lead the way to a solution, for this, as I guess it's not such an unusual use case ...?

Many thanks,

JMB

1

There are 1 answers

6
Jeyhun Rashidov On BEST ANSWER

If I understand correctly, when the admin changes the Backup-related setting, you also want to change the scheduling cron. It is possible to do this from the controller. You can set a scheduling in Controller:

Illuminate\Console\Scheduling\Schedule;

    protected function schedule(Schedule $schedule): void
    {
        switch ($adminChoice) {
        case 'daily':
            $schedule->command('backup:run')->daily();
            break;
        case 'weekly':
            $schedule->command('backup:run')->weekly();
            break;
       }
    }