PhyrePanel-mirror/web/app/Models/HostingSubscriptionBackup.php

218 lines
6.9 KiB
PHP
Raw Normal View History

2024-04-25 13:49:19 +00:00
<?php
namespace App\Models;
2024-04-25 13:58:30 +00:00
use App\Filament\Enums\BackupStatus;
use App\Helpers;
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-04-25 13:49:19 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2024-04-25 13:58:30 +00:00
use Illuminate\Support\Str;
2024-04-25 13:49:19 +00:00
class HostingSubscriptionBackup extends Model
{
use HasFactory;
2024-04-25 13:58:30 +00:00
const STATUS_PENDING = 'pending';
const STATUS_PROCESSING = 'processing';
const STATUS_COMPLETED = 'completed';
const STATUS_FAILED = 'failed';
const STATUS_CANCELLED = 'cancelled';
protected $fillable = [
'hosting_subscription_id',
'backup_type',
'status',
'path',
'size',
'disk',
];
protected $casts = [
'status' => BackupStatus::class,
];
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->status = 'pending';
$model->checkCronJob();
});
static::created(function ($model) {
$model->startBackup();
});
static::deleting(function ($model) {
if (is_dir($model->path)) {
shell_exec('rm -rf ' . $model->path);
}
});
}
public function checkCronJob()
{
$cronJobCommand = 'phyre-php /usr/local/phyre/web/artisan phyre:run-hosting-subscriptions-backup';
$findCronJob = CronJob::where('command', $cronJobCommand)->first();
if (! $findCronJob) {
$cronJob = new CronJob();
$cronJob->schedule = '*/5 * * * *';
$cronJob->command = $cronJobCommand;
$cronJob->user = 'root';
$cronJob->save();
return false;
}
return true;
}
public function checkBackup()
{
2024-04-26 08:04:49 +00:00
$findHostingSubscription = HostingSubscription::select(['id'])
->where('id', $this->hosting_subscription_id)
->first();
if (! $findHostingSubscription) {
$this->delete();
return [
'status' => 'failed',
'message' => 'Hosting subscription not found'
];
}
2024-04-25 13:58:30 +00:00
if ($this->status == BackupStatus::Processing) {
$backupDoneFile = $this->path.'/backup.done';
if (file_exists($backupDoneFile)) {
$this->size = Helpers::checkPathSize($this->path);
$this->status = 'completed';
$this->completed = true;
$this->completed_at = now();
$this->save();
return [
'status' => 'completed',
'message' => 'Backup completed'
];
}
$checkProcess = shell_exec('ps -p ' . $this->process_id . ' | grep ' . $this->process_id);
if (Str::contains($checkProcess, $this->process_id)) {
$this->size = Helpers::checkPathSize($this->path);
$this->save();
return [
'status' => 'processing',
'message' => 'Backup is still processing'
];
} else {
$this->status = 'failed';
$this->save();
return [
'status' => 'failed',
'message' => 'Backup failed'
];
}
}
}
public function startBackup()
{
2024-04-26 08:19:12 +00:00
$findHostingSubscription = HostingSubscription::where('id', $this->hosting_subscription_id)
2024-04-26 08:04:49 +00:00
->first();
if (! $findHostingSubscription) {
$this->delete();
return [
'status' => 'failed',
'message' => 'Hosting subscription not found'
];
}
2024-04-25 13:58:30 +00:00
if ($this->status == BackupStatus::Processing) {
return [
'status' => 'processing',
'message' => 'Backup is already processing'
];
}
2024-04-26 08:19:12 +00:00
$findMainDomain = Domain::where('hosting_subscription_id', $findHostingSubscription->id)
->where('is_main', 1)
->first();
if (! $findMainDomain) {
$this->delete();
return [
'status' => 'failed',
'message' => 'Main domain not found'
];
}
2024-04-25 13:58:30 +00:00
$storagePath = storage_path('backups');
if (! is_dir($storagePath)) {
mkdir($storagePath);
}
$backupPath = $storagePath.'/'.$this->backup_type.'/'.$this->id;
if (!is_dir(dirname($backupPath))) {
mkdir(dirname($backupPath));
}
if (! is_dir($backupPath)) {
mkdir($backupPath);
}
$backupTempPath = $backupPath.'/temp';
if (! is_dir($backupTempPath)) {
mkdir($backupTempPath);
}
2024-04-26 08:19:12 +00:00
dd($backupPath, $backupTempPath);
2024-04-25 13:58:30 +00:00
if ($this->backup_type == 'full') {
2024-04-26 08:04:49 +00:00
$backupFileName = Str::slug($findHostingSubscription->system_username .'-'. date('Ymd-His')) . '.tar.gz';
$backupFilePath = $backupPath.'/'.$backupFileName;
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
$backupLogFileName = 'backup.log';
$backupLogFilePath = $backupPath.'/'.$backupLogFileName;
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
$backupTempScript = '/tmp/backup-script-'.$this->id.'.sh';
$shellFileContent = '';
$shellFileContent .= 'echo "Backup up domain: '.$findHostingSubscription->domain . PHP_EOL;
$shellFileContent .= 'echo "Backup filename: '.$backupFileName. PHP_EOL;
$shellFileContent .= 'cp -r /home/'.$findHostingSubscription->system_username.' '.$backupTempPath.PHP_EOL;
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
$shellFileContent .= 'cd '.$backupTempPath .' && tar -czvf '.$backupFilePath.' ./* '. PHP_EOL;
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
$shellFileContent .= 'rm -rf '.$backupTempPath.PHP_EOL;
$shellFileContent .= 'echo "Backup complete"' . PHP_EOL;
$shellFileContent .= 'touch ' . $backupPath. '/backup.done' . PHP_EOL;
$shellFileContent .= 'rm -rf ' . $backupTempScript;
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
file_put_contents($backupTempScript, $shellFileContent);
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
$processId = shell_exec('bash '.$backupTempScript.' >> ' . $backupLogFilePath . ' & echo $!');
$processId = intval($processId);
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
if ($processId > 0 && is_numeric($processId)) {
2024-04-25 13:58:30 +00:00
2024-04-26 08:04:49 +00:00
$this->path = $backupPath;
$this->filepath = $backupFilePath;
$this->status = 'processing';
$this->queued = true;
$this->queued_at = now();
$this->process_id = $processId;
$this->save();
2024-04-25 13:58:30 +00:00
2024-04-26 08:19:12 +00:00
dd($this);
2024-04-26 08:04:49 +00:00
return [
'status' => 'processing',
'message' => 'Backup started'
];
2024-04-25 13:58:30 +00:00
} else {
$this->status = 'failed';
$this->save();
return [
'status' => 'failed',
2024-04-26 08:04:49 +00:00
'message' => 'Backup failed to start'
2024-04-25 13:58:30 +00:00
];
}
2024-04-26 08:04:49 +00:00
2024-04-25 13:58:30 +00:00
}
}
2024-04-25 13:49:19 +00:00
}