This commit is contained in:
Bozhidar 2024-04-25 15:11:44 +03:00
parent 8863be26f4
commit 85d1d1bc2a
3 changed files with 89 additions and 18 deletions

23
web/app/Helpers.php Normal file
View file

@ -0,0 +1,23 @@
<?php
namespace App;
class Helpers
{
public static function checkPathSize($path)
{
// Check path size
$pathSize = shell_exec('du -sh ' . $path);
$pathSize = trim($pathSize);
$pathSize = explode("\t", $pathSize);
if (isset($pathSize[0])) {
$pathSize = $pathSize[0];
return $pathSize;
}
return 0;
}
}

View file

@ -3,6 +3,7 @@
namespace App\Models;
use App\Filament\Enums\BackupStatus;
use App\Helpers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
@ -41,6 +42,10 @@ class Backup extends Model
$model->checkCronJob();
});
static::created(function ($model) {
$model->startBackup();
});
static::deleting(function ($model) {
if (is_dir($model->path)) {
shell_exec('rm -rf ' . $model->path);
@ -82,7 +87,7 @@ class Backup extends Model
$backupDoneFile = $this->path.'/backup.done';
if (file_exists($backupDoneFile)) {
$this->size = $this->checkPathSize($this->path);
$this->size = Helpers::checkPathSize($this->path);
$this->status = 'completed';
$this->completed = true;
$this->completed_at = now();
@ -96,7 +101,7 @@ class Backup extends Model
$checkProcess = shell_exec('ps -p ' . $this->process_id . ' | grep ' . $this->process_id);
if (Str::contains($checkProcess, $this->process_id)) {
$this->size = $this->checkPathSize($this->path);
$this->size = Helpers::checkPathSize($this->path);
$this->save();
return [
@ -197,19 +202,4 @@ class Backup extends Model
}
}
private function checkPathSize($path)
{
// Check path size
$pathSize = shell_exec('du -sh ' . $this->path);
$pathSize = trim($pathSize);
$pathSize = explode("\t", $pathSize);
if (isset($pathSize[0])) {
$pathSize = $pathSize[0];
return $pathSize;
}
return 0;
}
}

View file

@ -2,6 +2,12 @@
namespace tests\Unit;
use App\Filament\Enums\BackupStatus;
use App\Helpers;
use App\Models\Backup;
use App\Models\Customer;
use App\Models\HostingPlan;
use App\Models\HostingSubscription;
use Faker\Factory;
use Tests\Feature\Api\ActionTestCase;
@ -9,7 +15,59 @@ class BackupTest extends ActionTestCase
{
public function testBackup()
{
$customer = new Customer();
$customer->name = 'UnitBackupTest' . time();
$customer->email = 'UnitBackupTest' . time() . '@unit-test.com';
$customer->save();
$hostingPlan = new HostingPlan();
$hostingPlan->name = 'UnitBackupTest' . time();
$hostingPlan->description = 'Unit Backup Test';
$hostingPlan->disk_space = 1000;
$hostingPlan->bandwidth = 1000;
$hostingPlan->databases = 1;
$hostingPlan->additional_services = ['daily_backups'];
$hostingPlan->features = [];
$hostingPlan->default_server_application_type = 'apache_php';
$hostingPlan->default_server_application_settings = [
'php_version' => '8.3',
];
$hostingPlan->save();
$hostingSubscription = new HostingSubscription();
$hostingSubscription->customer_id = $customer->id;
$hostingSubscription->hosting_plan_id = $hostingPlan->id;
$hostingSubscription->domain = 'unit-backup-test' . time() . '.com';
$hostingSubscription->save();
$backup = new Backup();
$backup->backup_type = 'hosting_subscription';
$backup->hosting_subscription_id = $hostingSubscription->id;
$backup->save();
$backupId = $backup->id;
$findBackup = false;
$backupCompleted = false;
for ($i = 0; $i < 50; $i++) {
$findBackup = Backup::where('id', $backupId)->first();
$findBackup->checkBackup();
if ($findBackup->status == BackupStatus::Completed) {
$backupCompleted = true;
break;
}
sleep(1);
}
$this->assertTrue($backupCompleted);
$this->assertNotEmpty($findBackup->filepath);
$this->assertTrue(file_exists($findBackup->filepath));
$getFilesize = filesize($findBackup->filepath);
$this->assertGreaterThan(0, $getFilesize);
$this->assertSame(Helpers::checkPathSize($findBackup->path), $findBackup->size);
}
}