coolify/app/Livewire/SettingsBackup.php

114 lines
3.6 KiB
PHP
Raw Normal View History

2023-08-11 16:13:53 +02:00
<?php
namespace App\Livewire;
2023-08-11 16:13:53 +02:00
2023-08-11 20:48:52 +02:00
use App\Jobs\DatabaseBackupJob;
2023-08-11 16:13:53 +02:00
use App\Models\InstanceSettings;
use App\Models\S3Storage;
use App\Models\ScheduledDatabaseBackup;
use App\Models\Server;
use App\Models\StandalonePostgresql;
use Livewire\Component;
class SettingsBackup extends Component
2023-08-11 16:13:53 +02:00
{
public InstanceSettings $settings;
2024-06-10 22:43:34 +02:00
2023-08-11 16:13:53 +02:00
public $s3s;
2024-06-10 22:43:34 +02:00
public ?StandalonePostgresql $database = null;
2024-06-10 22:43:34 +02:00
2023-08-14 14:00:10 +02:00
public ScheduledDatabaseBackup|null|array $backup = [];
2024-06-10 22:43:34 +02:00
2023-08-11 20:48:52 +02:00
public $executions = [];
2023-08-11 16:13:53 +02:00
protected $rules = [
'database.uuid' => 'required',
'database.name' => 'required',
'database.description' => 'nullable',
'database.postgres_user' => 'required',
'database.postgres_password' => 'required',
];
2024-06-10 22:43:34 +02:00
2023-08-11 16:13:53 +02:00
protected $validationAttributes = [
'database.uuid' => 'uuid',
'database.name' => 'name',
'database.description' => 'description',
'database.postgres_user' => 'postgres user',
'database.postgres_password' => 'postgres password',
];
2023-08-11 20:48:52 +02:00
public function mount()
{
if (isInstanceAdmin()) {
$settings = InstanceSettings::get();
$this->database = StandalonePostgresql::whereName('coolify-db')->first();
$s3s = S3Storage::whereTeamId(0)->get() ?? [];
if ($this->database) {
if ($this->database->status !== 'running') {
$this->database->status = 'running';
$this->database->save();
}
$this->backup = $this->database->scheduledBackups->first();
$this->executions = $this->backup->executions;
}
$this->settings = $settings;
$this->s3s = $s3s;
2024-08-08 12:19:27 +02:00
} else {
return redirect()->route('dashboard');
}
2023-08-11 20:48:52 +02:00
}
2024-06-10 22:43:34 +02:00
2023-08-11 16:13:53 +02:00
public function add_coolify_database()
{
try {
$server = Server::findOrFail(0);
$out = instant_remote_process(['docker inspect coolify-db'], $server);
$envs = format_docker_envs_to_json($out);
$postgres_password = $envs['POSTGRES_PASSWORD'];
$postgres_user = $envs['POSTGRES_USER'];
$postgres_db = $envs['POSTGRES_DB'];
$this->database = StandalonePostgresql::create([
'id' => 0,
'name' => 'coolify-db',
'description' => 'Coolify database',
'postgres_user' => $postgres_user,
'postgres_password' => $postgres_password,
'postgres_db' => $postgres_db,
'status' => 'running',
'destination_type' => 'App\Models\StandaloneDocker',
'destination_id' => 0,
]);
$this->backup = ScheduledDatabaseBackup::create([
'id' => 0,
'enabled' => true,
'save_s3' => false,
'frequency' => '0 0 * * *',
'database_id' => $this->database->id,
'database_type' => 'App\Models\StandalonePostgresql',
'team_id' => currentTeam()->id,
]);
$this->database->refresh();
$this->backup->refresh();
$this->s3s = S3Storage::whereTeamId(0)->get();
} catch (\Exception $e) {
return handleError($e, $this);
}
2023-08-11 16:13:53 +02:00
}
2023-08-11 20:48:52 +02:00
public function backup_now()
{
2023-08-11 18:14:58 +02:00
dispatch(new DatabaseBackupJob(
backup: $this->backup
));
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Backup queued. It will be available in a few minutes.');
2023-08-11 18:14:58 +02:00
}
2024-06-10 22:43:34 +02:00
2023-08-11 16:13:53 +02:00
public function submit()
{
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Backup updated.');
2023-08-11 16:13:53 +02:00
}
}