2026-07-15 13:47:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-07-15 15:34:22 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-07-15 13:47:57 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2026-07-15 15:34:22 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
2026-07-15 13:47:57 +00:00
|
|
|
|
|
|
|
|
class ScheduledVolumeBackup extends BaseModel
|
|
|
|
|
{
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'uuid',
|
2026-07-15 15:34:22 +00:00
|
|
|
'backupable_type',
|
|
|
|
|
'backupable_id',
|
2026-07-15 13:47:57 +00:00
|
|
|
'team_id',
|
|
|
|
|
's3_storage_id',
|
|
|
|
|
'frequency',
|
|
|
|
|
'enabled',
|
|
|
|
|
'save_s3',
|
|
|
|
|
'disable_local_backup',
|
2026-07-15 13:59:35 +00:00
|
|
|
'stop_during_backup',
|
2026-07-15 13:47:57 +00:00
|
|
|
'retention_amount_locally',
|
|
|
|
|
'retention_days_locally',
|
|
|
|
|
'retention_max_storage_locally',
|
|
|
|
|
'retention_amount_s3',
|
|
|
|
|
'retention_days_s3',
|
|
|
|
|
'retention_max_storage_s3',
|
|
|
|
|
'timeout',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'enabled' => 'boolean',
|
|
|
|
|
'save_s3' => 'boolean',
|
|
|
|
|
'disable_local_backup' => 'boolean',
|
2026-07-15 13:59:35 +00:00
|
|
|
'stop_during_backup' => 'boolean',
|
2026-07-15 13:47:57 +00:00
|
|
|
'retention_amount_locally' => 'integer',
|
|
|
|
|
'retention_days_locally' => 'integer',
|
|
|
|
|
'retention_max_storage_locally' => 'float',
|
|
|
|
|
'retention_amount_s3' => 'integer',
|
|
|
|
|
'retention_days_s3' => 'integer',
|
|
|
|
|
'retention_max_storage_s3' => 'float',
|
|
|
|
|
'timeout' => 'integer',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 15:34:22 +00:00
|
|
|
public function scopeForApplication(Builder $query, Application $application): Builder
|
2026-07-15 13:47:57 +00:00
|
|
|
{
|
2026-07-15 15:34:22 +00:00
|
|
|
$volumeIds = $application->persistentStorages()->pluck('id');
|
|
|
|
|
$directoryIds = $application->fileStorages()
|
|
|
|
|
->where('is_directory', true)
|
|
|
|
|
->where('is_host_file', false)
|
|
|
|
|
->pluck('id');
|
|
|
|
|
|
|
|
|
|
return $query->where(function (Builder $query) use ($volumeIds, $directoryIds): void {
|
|
|
|
|
$query->where(function (Builder $query) use ($volumeIds): void {
|
|
|
|
|
$query->where('backupable_type', (new LocalPersistentVolume)->getMorphClass())
|
|
|
|
|
->whereIn('backupable_id', $volumeIds);
|
|
|
|
|
})->orWhere(function (Builder $query) use ($directoryIds): void {
|
|
|
|
|
$query->where('backupable_type', (new LocalFileVolume)->getMorphClass())
|
|
|
|
|
->whereIn('backupable_id', $directoryIds);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function backupable(): MorphTo
|
|
|
|
|
{
|
|
|
|
|
return $this->morphTo();
|
2026-07-15 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function team(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Team::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function s3(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(S3Storage::class, 's3_storage_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function executions(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ScheduledVolumeBackupExecution::class)->latest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function latestExecution(): HasOne
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(ScheduledVolumeBackupExecution::class)->latestOfMany();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function server(): ?Server
|
|
|
|
|
{
|
2026-07-15 15:34:22 +00:00
|
|
|
$resource = $this->targetResource();
|
2026-07-15 13:47:57 +00:00
|
|
|
|
|
|
|
|
if ($resource instanceof ServiceApplication || $resource instanceof ServiceDatabase) {
|
|
|
|
|
return $resource->service?->server?->fresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $resource?->destination?->server?->fresh();
|
|
|
|
|
}
|
2026-07-15 15:34:22 +00:00
|
|
|
|
|
|
|
|
public function targetResource(): ?Model
|
|
|
|
|
{
|
|
|
|
|
return $this->backupable?->resource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function targetType(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->backupable instanceof LocalFileVolume ? 'Directory' : 'Volume';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function targetName(): string
|
|
|
|
|
{
|
|
|
|
|
return match (true) {
|
|
|
|
|
$this->backupable instanceof LocalFileVolume => $this->backupable->fs_path,
|
|
|
|
|
$this->backupable instanceof LocalPersistentVolume => $this->backupable->name,
|
|
|
|
|
default => 'Unknown storage',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sourcePath(): string
|
|
|
|
|
{
|
|
|
|
|
$target = $this->backupable;
|
|
|
|
|
|
|
|
|
|
if ($target instanceof LocalPersistentVolume) {
|
|
|
|
|
return filled($target->host_path) ? $target->host_path : $target->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! $target instanceof LocalFileVolume || ! $target->is_directory) {
|
|
|
|
|
throw new \RuntimeException('The backup target is not a directory or persistent volume.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$path = str($target->fs_path);
|
|
|
|
|
if ($path->startsWith('.')) {
|
|
|
|
|
$resource = $this->targetResource();
|
|
|
|
|
if (! $resource || ! method_exists($resource, 'workdir')) {
|
|
|
|
|
throw new \RuntimeException('The directory backup workdir is unavailable.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $resource->workdir().$path->after('.')->toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $path->toString();
|
|
|
|
|
}
|
2026-07-15 13:47:57 +00:00
|
|
|
}
|