2023-08-09 15:57:27 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Database;
|
2023-08-09 15:57:27 +00:00
|
|
|
|
|
|
|
use App\Models\ScheduledDatabaseBackup;
|
2024-05-24 15:28:05 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-08-09 15:57:27 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class CreateScheduledBackup extends Component
|
|
|
|
{
|
|
|
|
public $database;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-09 15:57:27 +00:00
|
|
|
public $frequency;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-09 15:57:27 +00:00
|
|
|
public bool $enabled = true;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-30 18:26:42 +00:00
|
|
|
public bool $save_s3 = false;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-11 14:13:53 +00:00
|
|
|
public $s3_storage_id;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-24 15:28:05 +00:00
|
|
|
public Collection $s3s;
|
2023-08-09 15:57:27 +00:00
|
|
|
|
|
|
|
protected $rules = [
|
|
|
|
'frequency' => 'required|string',
|
|
|
|
'save_s3' => 'required|boolean',
|
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-09 15:57:27 +00:00
|
|
|
protected $validationAttributes = [
|
|
|
|
'frequency' => 'Backup Frequency',
|
|
|
|
'save_s3' => 'Save to S3',
|
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-07 11:11:47 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2024-06-06 09:09:27 +00:00
|
|
|
$this->s3s = currentTeam()->s3s;
|
2023-10-20 07:38:13 +00:00
|
|
|
if ($this->s3s->count() > 0) {
|
|
|
|
$this->s3_storage_id = $this->s3s->first()->id;
|
|
|
|
}
|
|
|
|
}
|
2023-08-09 15:57:27 +00:00
|
|
|
|
|
|
|
public function submit(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->validate();
|
2023-08-10 13:52:54 +00:00
|
|
|
$isValid = validate_cron_expression($this->frequency);
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $isValid) {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('error', 'Invalid Cron / Human expression.');
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-09 15:57:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-10-13 14:32:59 +00:00
|
|
|
$payload = [
|
2023-08-09 15:57:27 +00:00
|
|
|
'enabled' => true,
|
|
|
|
'frequency' => $this->frequency,
|
|
|
|
'save_s3' => $this->save_s3,
|
2023-08-11 14:13:53 +00:00
|
|
|
's3_storage_id' => $this->s3_storage_id,
|
2023-08-09 15:57:27 +00:00
|
|
|
'database_id' => $this->database->id,
|
|
|
|
'database_type' => $this->database->getMorphClass(),
|
2023-08-22 15:44:49 +00:00
|
|
|
'team_id' => currentTeam()->id,
|
2023-10-13 14:32:59 +00:00
|
|
|
];
|
|
|
|
if ($this->database->type() === 'standalone-postgresql') {
|
|
|
|
$payload['databases_to_backup'] = $this->database->postgres_db;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($this->database->type() === 'standalone-mysql') {
|
2023-10-24 12:31:28 +00:00
|
|
|
$payload['databases_to_backup'] = $this->database->mysql_database;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($this->database->type() === 'standalone-mariadb') {
|
2023-10-24 12:31:28 +00:00
|
|
|
$payload['databases_to_backup'] = $this->database->mariadb_database;
|
2023-10-13 14:32:59 +00:00
|
|
|
}
|
2023-11-07 11:11:47 +00:00
|
|
|
|
|
|
|
$databaseBackup = ScheduledDatabaseBackup::create($payload);
|
|
|
|
if ($this->database->getMorphClass() === 'App\Models\ServiceDatabase') {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('refreshScheduledBackups', $databaseBackup->id);
|
2023-11-07 11:11:47 +00:00
|
|
|
} else {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('refreshScheduledBackups');
|
2023-11-07 11:11:47 +00:00
|
|
|
}
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
handleError($e, $this);
|
2023-08-09 15:57:27 +00:00
|
|
|
} finally {
|
|
|
|
$this->frequency = '';
|
|
|
|
$this->save_s3 = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|