This commit is contained in:
Bozhidar 2024-04-24 21:03:08 +03:00
parent 2325192f78
commit a346d35cf0
4 changed files with 52 additions and 6 deletions

View file

@ -5,6 +5,8 @@ namespace App\Console\Commands;
use App\Models\Backup;
use App\Models\HostingSubscription;
use Illuminate\Console\Command;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RunBackup extends Command
{
@ -33,10 +35,20 @@ class RunBackup extends Command
$backup->delete();
}
$getBackups = Backup::where('backup_type', 'hosting_subscription')->get();
if ($getBackups->count() > 0) {
foreach ($getBackups as $backup) {
$status = $backup->startBackup();
// Check for pending backups
$getPendingBackups = Backup::where('status', 'pending')
->get();
if ($getPendingBackups->count() > 0) {
foreach ($getPendingBackups as $pendingBackup) {
$pendingBackup->startBackup();
}
}
// Check for running backups
$getRunningBackups = Backup::where('status', 'running')->get();
if ($getRunningBackups->count() > 0) {
foreach ($getRunningBackups as $runningBackup) {
$runningBackup->checkBackup();
}
}

View file

@ -65,13 +65,21 @@ class BackupResource extends Resource
->columns([
Tables\Columns\TextColumn::make('backup_type'),
Tables\Columns\TextColumn::make('backupRelated'),
Tables\Columns\TextColumn::make('status'),
Tables\Columns\BadgeColumn::make('status')
->badge()
->color(fn (string $state): string => match ($state) {
'pending' => 'gray',
'running' => 'primary',
'completed' => 'success',
'failed' => 'danger',
default => 'gray',
}),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\ViewAction::make(),
])
->defaultSort('id', 'desc')
->bulkActions([

View file

@ -20,6 +20,16 @@ class Backup extends Model
'disk',
];
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->status = 'pending';
});
}
protected function backupRelated() : Attribute
{
$relatedWith = $this->backup_type;
@ -35,6 +45,19 @@ class Backup extends Model
);
}
public function checkBackup()
{
if ($this->status == 'running') {
$backupDoneFile = $this->path.'/backup.done';
if (file_exists($backupDoneFile)) {
$this->status = 'completed';
$this->completed = true;
$this->completed_at = now();
$this->save();
}
}
}
public function startBackup()
{
@ -80,6 +103,8 @@ class Backup extends Model
$shellFileContent .= 'touch ' . $backupPath. '/backup.done' . PHP_EOL;
$shellFileContent .= 'rm -rf ' . $backupTempScript;
$this->path = $backupPath;
$this->filepath = $backupFilePath;
$this->status = 'running';
$this->queued = true;
$this->queued_at = now();

View file

@ -17,6 +17,7 @@ return new class extends Migration
$table->string('backup_type')->nullable();
$table->string('status')->nullable();
$table->string('path')->nullable();
$table->string('filepath')->nullable();
$table->string('size')->nullable();
$table->string('disk')->nullable();
$table->longText('settings')->nullable();