lasthourcloud/app/Http/Livewire/Settings/Backup.php

87 lines
2.8 KiB
PHP
Raw Normal View History

2023-08-11 14:13:53 +00:00
<?php
namespace App\Http\Livewire\Settings;
2023-08-11 18:48:52 +00:00
use App\Jobs\DatabaseBackupJob;
2023-08-11 14:13:53 +00: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 Backup extends Component
{
public InstanceSettings $settings;
public $s3s;
2023-08-14 12:00:10 +00:00
public StandalonePostgresql|null|array $database = [];
public ScheduledDatabaseBackup|null|array $backup = [];
2023-08-11 18:48:52 +00:00
public $executions = [];
2023-08-11 14:13:53 +00:00
protected $rules = [
'database.uuid' => 'required',
'database.name' => 'required',
'database.description' => 'nullable',
'database.postgres_user' => 'required',
'database.postgres_password' => 'required',
];
protected $validationAttributes = [
'database.uuid' => 'uuid',
'database.name' => 'name',
'database.description' => 'description',
'database.postgres_user' => 'postgres user',
'database.postgres_password' => 'postgres password',
];
2023-08-11 18:48:52 +00:00
public function mount()
{
2023-08-14 12:00:10 +00:00
$this->backup = $this->database?->scheduledBackups->first() ?? [];
$this->executions = $this->backup?->executions ?? [];
2023-08-11 18:48:52 +00:00
}
2023-08-11 14:13:53 +00:00
public function add_coolify_database()
{
$server = Server::find(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',
2023-08-22 15:44:49 +00:00
'team_id' => currentTeam()->id,
2023-08-11 14:13:53 +00:00
]);
$this->database->refresh();
$this->backup->refresh();
$this->s3s = S3Storage::whereTeamId(0)->get();
}
2023-08-11 18:48:52 +00:00
public function backup_now()
{
2023-08-11 16:14:58 +00:00
dispatch(new DatabaseBackupJob(
backup: $this->backup
));
2023-10-13 13:45:24 +00:00
$this->emit('success', 'Backup queued. It will be available in a few minutes.');
2023-08-11 16:14:58 +00:00
}
2023-08-11 14:13:53 +00:00
public function submit()
{
2023-10-13 13:45:24 +00:00
$this->emit('success', 'Backup updated successfully.');
2023-08-11 14:13:53 +00:00
}
}