Files
HexaHost-Panel/app/Services/Hosting/Backups/BackupService.php
2026-05-17 13:26:14 +02:00

74 lines
2.3 KiB
PHP

<?php
namespace App\Services\Hosting\Backups;
use App\Exceptions\Hosting\ProvisioningException;
use App\Models\Customer;
use App\Models\User;
use App\Models\VmBackup;
use App\Services\Hosting\Proxmox\ProxmoxClient;
use Illuminate\Support\Facades\Log;
class BackupService
{
public function __construct(private readonly ProxmoxClient $proxmox) {}
public function start(Customer $vm, User $user): VmBackup
{
if (! config('hosting.backups.enabled', false)) {
throw new ProvisioningException('Backups sind noch nicht aktiviert (PBS-Rechte fehlen).', step: 'backup');
}
$max = $this->maxBackupsForUser($user);
$count = VmBackup::query()
->whereHas('vm', fn ($q) => $q->where('user_id', $user->id))
->whereIn('status', ['running', 'completed'])
->count();
if ($count >= $max) {
throw new ProvisioningException("Maximal {$max} Backups erlaubt.", step: 'backup_limit');
}
$storage = config('hosting.backups.pbs_storage', 'inett-PBS');
$upid = $this->proxmox->startBackup((int) $vm->vmid, $storage);
return VmBackup::query()->create([
'customer_id' => $vm->id,
'user_id' => $user->id,
'storage' => $storage,
'volume_id' => $upid,
'status' => 'running',
]);
}
public function maxBackupsForUser(User $user): int
{
return (int) config('hosting.backups.max_per_customer', 4);
}
public function deleteAllForVm(Customer $vm): void
{
$backups = VmBackup::query()->where('customer_id', $vm->id)->get();
foreach ($backups as $backup) {
Log::info('Backup record removed', ['id' => $backup->id, 'volume' => $backup->volume_id]);
// PBS purge via API when credentials available
$backup->delete();
}
}
public function enforceRetentionForUser(User $user): void
{
$max = $this->maxBackupsForUser($user);
$backups = VmBackup::query()
->whereHas('vm', fn ($q) => $q->where('user_id', $user->id))
->where('status', 'completed')
->orderByDesc('completed_at')
->get();
foreach ($backups->slice($max) as $old) {
$old->delete();
}
}
}