2023-08-10 13:52:54 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Database;
|
2023-08-10 13:52:54 +00:00
|
|
|
|
2023-10-25 07:28:26 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-08-10 13:52:54 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class BackupExecutions extends Component
|
|
|
|
{
|
|
|
|
public $backup;
|
|
|
|
public $executions;
|
2023-10-24 12:31:28 +00:00
|
|
|
public $setDeletableBackup;
|
|
|
|
protected $listeners = ['refreshBackupExecutions', 'deleteBackup'];
|
2023-08-10 13:52:54 +00:00
|
|
|
|
2023-10-24 12:31:28 +00:00
|
|
|
public function deleteBackup($exeuctionId)
|
|
|
|
{
|
|
|
|
$execution = $this->backup->executions()->where('id', $exeuctionId)->first();
|
|
|
|
if (is_null($execution)) {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('error', 'Backup execution not found.');
|
2023-10-24 12:31:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-07 11:11:47 +00:00
|
|
|
if ($execution->scheduledDatabaseBackup->database->getMorphClass() === 'App\Models\ServiceDatabase') {
|
|
|
|
delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->service->destination->server);
|
|
|
|
} else {
|
|
|
|
delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->destination->server);
|
|
|
|
}
|
2023-10-24 12:31:28 +00:00
|
|
|
$execution->delete();
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Backup deleted successfully.');
|
|
|
|
$this->dispatch('refreshBackupExecutions');
|
2023-10-24 12:31:28 +00:00
|
|
|
}
|
2023-10-25 07:28:26 +00:00
|
|
|
public function download($exeuctionId)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$execution = $this->backup->executions()->where('id', $exeuctionId)->first();
|
|
|
|
if (is_null($execution)) {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('error', 'Backup execution not found.');
|
2023-10-25 07:28:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$filename = data_get($execution, 'filename');
|
2023-11-07 11:11:47 +00:00
|
|
|
if ($execution->scheduledDatabaseBackup->database->getMorphClass() === 'App\Models\ServiceDatabase') {
|
|
|
|
$server = $execution->scheduledDatabaseBackup->database->service->destination->server;
|
|
|
|
} else {
|
|
|
|
$server = $execution->scheduledDatabaseBackup->database->destination->server;
|
|
|
|
}
|
2023-10-25 07:28:26 +00:00
|
|
|
$privateKeyLocation = savePrivateKeyToFs($server);
|
|
|
|
$disk = Storage::build([
|
|
|
|
'driver' => 'sftp',
|
|
|
|
'host' => $server->ip,
|
|
|
|
'port' => $server->port,
|
|
|
|
'username' => $server->user,
|
|
|
|
'privateKey' => $privateKeyLocation,
|
|
|
|
]);
|
|
|
|
return $disk->download($filename);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
public function refreshBackupExecutions(): void
|
|
|
|
{
|
2023-08-11 08:42:57 +00:00
|
|
|
$this->executions = $this->backup->executions;
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|
|
|
|
}
|