40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\BackupJob;
|
|
use App\Models\BackupPolicy;
|
|
use Illuminate\Console\Command;
|
|
|
|
class BackupScheduled extends Command
|
|
{
|
|
protected $signature = 'backup:scheduled {policyId}';
|
|
protected $description = 'Legt einen BackupJob an und startet backup:run (wird vom Scheduler aufgerufen)';
|
|
|
|
public function handle(): int
|
|
{
|
|
$policy = BackupPolicy::find($this->argument('policyId'));
|
|
|
|
if (! $policy || ! $policy->enabled) {
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
if (BackupJob::whereIn('status', ['queued', 'running'])->exists()) {
|
|
$this->warn('Backup läuft bereits — übersprungen.');
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$job = BackupJob::create([
|
|
'policy_id' => $policy->id,
|
|
'status' => 'queued',
|
|
'started_at' => now(),
|
|
]);
|
|
|
|
$artisan = base_path('artisan');
|
|
exec("nohup php {$artisan} backup:run {$job->id} > /dev/null 2>&1 &");
|
|
|
|
$this->info("Backup-Job #{$job->id} gestartet.");
|
|
return self::SUCCESS;
|
|
}
|
|
}
|