Able to backup Coolify itself
This commit is contained in:
@@ -7,6 +7,7 @@ use Livewire\Component;
|
||||
class BackupEdit extends Component
|
||||
{
|
||||
public $backup;
|
||||
public $s3s;
|
||||
public array $parameters;
|
||||
|
||||
protected $rules = [
|
||||
@@ -14,17 +15,25 @@ class BackupEdit extends Component
|
||||
'backup.frequency' => 'required|string',
|
||||
'backup.number_of_backups_locally' => 'required|integer|min:1',
|
||||
'backup.save_s3' => 'required|boolean',
|
||||
'backup.s3_storage_id' => 'nullable|integer',
|
||||
];
|
||||
protected $validationAttributes = [
|
||||
'backup.enabled' => 'Enabled',
|
||||
'backup.frequency' => 'Frequency',
|
||||
'backup.number_of_backups_locally' => 'Number of Backups Locally',
|
||||
'backup.save_s3' => 'Save to S3',
|
||||
'backup.s3_storage_id' => 'S3 Storage',
|
||||
];
|
||||
protected $messages = [
|
||||
'backup.s3_storage_id' => 'Select a S3 Storage',
|
||||
];
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = get_route_parameters();
|
||||
if (is_null($this->backup->s3_storage_id)) {
|
||||
$this->backup->s3_storage_id = 'default';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,21 +46,43 @@ class BackupEdit extends Component
|
||||
|
||||
public function instantSave()
|
||||
{
|
||||
$this->backup->save();
|
||||
$this->backup->refresh();
|
||||
$this->emit('success', 'Backup updated successfully');
|
||||
try {
|
||||
$this->custom_validate();
|
||||
$this->backup->save();
|
||||
$this->backup->refresh();
|
||||
$this->emit('success', 'Backup updated successfully');
|
||||
} catch (\Exception $e) {
|
||||
$this->emit('error', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function custom_validate()
|
||||
{
|
||||
// if ($this->backup->save_s3) {
|
||||
// if (!is_numeric($this->selected_storage_id)) {
|
||||
// throw new \Exception('Invalid S3 Storage');
|
||||
// } else {
|
||||
// $this->backup->s3_storage_id = $this->selected_storage_id;
|
||||
// }
|
||||
// }
|
||||
$isValid = validate_cron_expression($this->backup->frequency);
|
||||
if (!$isValid) {
|
||||
throw new \Exception('Invalid Cron / Human expression');
|
||||
}
|
||||
$this->validate();
|
||||
}
|
||||
|
||||
public function submit()
|
||||
{
|
||||
$isValid = validate_cron_expression($this->backup->frequency);
|
||||
if (!$isValid) {
|
||||
$this->emit('error', 'Invalid Cron / Human expression');
|
||||
return;
|
||||
ray($this->backup->s3_storage_id);
|
||||
try {
|
||||
$this->custom_validate();
|
||||
$this->backup->save();
|
||||
$this->backup->refresh();
|
||||
$this->emit('success', 'Backup updated successfully');
|
||||
} catch (\Exception $e) {
|
||||
$this->emit('error', $e->getMessage());
|
||||
}
|
||||
$this->validate();
|
||||
$this->backup->save();
|
||||
$this->backup->refresh();
|
||||
$this->emit('success', 'Backup updated successfully');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ class CreateScheduledBackup extends Component
|
||||
public $frequency;
|
||||
public bool $enabled = true;
|
||||
public bool $save_s3 = true;
|
||||
public $s3_storage_id;
|
||||
public $s3s;
|
||||
|
||||
protected $rules = [
|
||||
'frequency' => 'required|string',
|
||||
@@ -27,13 +29,14 @@ class CreateScheduledBackup extends Component
|
||||
$this->validate();
|
||||
$isValid = validate_cron_expression($this->frequency);
|
||||
if (!$isValid) {
|
||||
$this->emit('error', 'Invalid Cron / Human expression');
|
||||
$this->emit('error', 'Invalid Cron / Human expression.');
|
||||
return;
|
||||
}
|
||||
ScheduledDatabaseBackup::create([
|
||||
'enabled' => true,
|
||||
'frequency' => $this->frequency,
|
||||
'save_s3' => $this->save_s3,
|
||||
's3_storage_id' => $this->s3_storage_id,
|
||||
'database_id' => $this->database->id,
|
||||
'database_type' => $this->database->getMorphClass(),
|
||||
'team_id' => auth()->user()->currentTeam()->id,
|
||||
|
||||
75
app/Http/Livewire/Settings/Backup.php
Normal file
75
app/Http/Livewire/Settings/Backup.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Settings;
|
||||
|
||||
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;
|
||||
public StandalonePostgresql|null $database = null;
|
||||
public ScheduledDatabaseBackup|null $backup = null;
|
||||
|
||||
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',
|
||||
];
|
||||
|
||||
public function add_coolify_database()
|
||||
{
|
||||
ray('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',
|
||||
'team_id' => auth()->user()->currentTeam()->id,
|
||||
]);
|
||||
$this->database->refresh();
|
||||
$this->backup->refresh();
|
||||
ray($this->backup);
|
||||
$this->s3s = S3Storage::whereTeamId(0)->get();
|
||||
}
|
||||
|
||||
public function submit()
|
||||
{
|
||||
$this->emit('success', 'Backup updated successfully');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user