Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Apr 24, 2024
1 parent 2325192 commit a346d35
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
20 changes: 16 additions & 4 deletions web/app/Console/Commands/RunBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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
{
Expand Down Expand Up @@ -33,10 +35,20 @@ public function handle()
$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();
}
}

Expand Down
12 changes: 10 additions & 2 deletions web/app/Filament/Resources/BackupResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@ public static function table(Table $table): Table
->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([
Expand Down
25 changes: 25 additions & 0 deletions web/app/Models/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +45,19 @@ protected function backupRelated() : Attribute
);
}

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()
{

Expand Down Expand Up @@ -80,6 +103,8 @@ public function startBackup()
$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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function up(): void
$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();
Expand Down

0 comments on commit a346d35

Please sign in to comment.