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
|
|
|
|
2024-10-22 10:29:48 +00:00
|
|
|
use App\Models\InstanceSettings;
|
2024-05-24 07:35:36 +00:00
|
|
|
use App\Models\ScheduledDatabaseBackup;
|
2024-08-31 17:31:01 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-09-23 17:51:31 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
use Livewire\Component;
|
2023-08-10 13:52:54 +00:00
|
|
|
|
|
|
|
|
class BackupExecutions extends Component
|
|
|
|
|
{
|
2024-05-24 07:35:36 +00:00
|
|
|
public ?ScheduledDatabaseBackup $backup = null;
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2024-08-16 19:06:36 +00:00
|
|
|
public $database;
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2023-12-14 14:01:19 +00:00
|
|
|
public $executions = [];
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2023-10-24 12:31:28 +00:00
|
|
|
public $setDeletableBackup;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-08-31 17:31:01 +00:00
|
|
|
public $delete_backup_s3 = true;
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2024-08-31 17:31:01 +00:00
|
|
|
public $delete_backup_sftp = true;
|
|
|
|
|
|
2023-12-11 09:23:10 +00:00
|
|
|
public function getListeners()
|
|
|
|
|
{
|
2024-08-31 17:31:01 +00:00
|
|
|
$userId = Auth::id();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-11 09:23:10 +00:00
|
|
|
return [
|
|
|
|
|
"echo-private:team.{$userId},BackupCreated" => 'refreshBackupExecutions',
|
|
|
|
|
];
|
|
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
|
2024-04-29 07:38:45 +00:00
|
|
|
public function cleanupFailed()
|
|
|
|
|
{
|
2024-05-24 07:35:36 +00:00
|
|
|
if ($this->backup) {
|
2024-05-24 15:06:26 +00:00
|
|
|
$this->backup->executions()->where('status', 'failed')->delete();
|
2024-05-24 07:35:36 +00:00
|
|
|
$this->refreshBackupExecutions();
|
|
|
|
|
$this->dispatch('success', 'Failed backups cleaned up.');
|
|
|
|
|
}
|
2024-04-29 07:38:45 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-08-31 17:31:01 +00:00
|
|
|
public function deleteBackup($executionId, $password)
|
2023-10-24 12:31:28 +00:00
|
|
|
{
|
2024-10-24 14:20:01 +00:00
|
|
|
if (! data_get(InstanceSettings::get(), 'disable_two_step_confirmation')) {
|
2024-10-22 10:29:48 +00:00
|
|
|
if (! Hash::check($password, Auth::user()->password)) {
|
|
|
|
|
$this->addError('password', 'The provided password is incorrect.');
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2024-10-22 10:29:48 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2024-08-31 17:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$execution = $this->backup->executions()->where('id', $executionId)->first();
|
2023-10-24 12:31:28 +00:00
|
|
|
if (is_null($execution)) {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('error', 'Backup execution not found.');
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2023-10-24 12:31:28 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2024-08-31 17:31:01 +00:00
|
|
|
|
2024-10-28 13:56:13 +00:00
|
|
|
if ($execution->scheduledDatabaseBackup->database->getMorphClass() === \App\Models\ServiceDatabase::class) {
|
2023-11-07 11:11:47 +00:00
|
|
|
delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->service->destination->server);
|
|
|
|
|
} else {
|
|
|
|
|
delete_backup_locally($execution->filename, $execution->scheduledDatabaseBackup->database->destination->server);
|
|
|
|
|
}
|
2024-08-31 17:31:01 +00:00
|
|
|
|
|
|
|
|
if ($this->delete_backup_s3) {
|
|
|
|
|
// Add logic to delete from S3
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->delete_backup_sftp) {
|
|
|
|
|
// Add logic to delete from SFTP
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 12:31:28 +00:00
|
|
|
$execution->delete();
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Backup deleted.');
|
2024-03-07 09:27:21 +00:00
|
|
|
$this->refreshBackupExecutions();
|
2023-10-24 12:31:28 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-04-10 13:00:46 +00:00
|
|
|
public function download_file($exeuctionId)
|
2023-10-25 07:28:26 +00:00
|
|
|
{
|
2024-04-10 13:00:46 +00:00
|
|
|
return redirect()->route('download.backup', $exeuctionId);
|
2023-10-25 07:28:26 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-10 13:52:54 +00:00
|
|
|
public function refreshBackupExecutions(): void
|
|
|
|
|
{
|
2024-05-24 15:06:26 +00:00
|
|
|
if ($this->backup) {
|
2024-08-26 08:32:05 +00:00
|
|
|
$this->executions = $this->backup->executions()->get();
|
2024-05-24 15:06:26 +00:00
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|
2024-08-16 19:06:36 +00:00
|
|
|
|
|
|
|
|
public function mount(ScheduledDatabaseBackup $backup)
|
|
|
|
|
{
|
|
|
|
|
$this->backup = $backup;
|
|
|
|
|
$this->database = $backup->database;
|
|
|
|
|
$this->refreshBackupExecutions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function server()
|
|
|
|
|
{
|
|
|
|
|
if ($this->database) {
|
|
|
|
|
$server = null;
|
|
|
|
|
|
|
|
|
|
if ($this->database instanceof \App\Models\ServiceDatabase) {
|
|
|
|
|
$server = $this->database->service->destination->server;
|
|
|
|
|
} elseif ($this->database->destination && $this->database->destination->server) {
|
|
|
|
|
$server = $this->database->destination->server;
|
|
|
|
|
}
|
|
|
|
|
if ($server) {
|
|
|
|
|
return $server;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2024-08-16 19:06:36 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getServerTimezone()
|
|
|
|
|
{
|
|
|
|
|
$server = $this->server();
|
2024-09-23 17:51:31 +00:00
|
|
|
if (! $server) {
|
2024-08-16 19:06:36 +00:00
|
|
|
return 'UTC';
|
|
|
|
|
}
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2024-10-31 17:20:11 +00:00
|
|
|
return $server->settings->server_timezone;
|
2024-08-16 19:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function formatDateInServerTimezone($date)
|
|
|
|
|
{
|
|
|
|
|
$serverTimezone = $this->getServerTimezone();
|
|
|
|
|
$dateObj = new \DateTime($date);
|
|
|
|
|
try {
|
|
|
|
|
$dateObj->setTimezone(new \DateTimeZone($serverTimezone));
|
2024-10-31 14:19:37 +00:00
|
|
|
} catch (\Exception) {
|
2024-08-16 19:06:36 +00:00
|
|
|
$dateObj->setTimezone(new \DateTimeZone('UTC'));
|
2024-05-24 15:06:26 +00:00
|
|
|
}
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2024-08-16 19:06:36 +00:00
|
|
|
return $dateObj->format('Y-m-d H:i:s T');
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|
2024-08-31 17:31:01 +00:00
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.project.database.backup-executions', [
|
|
|
|
|
'checkboxes' => [
|
|
|
|
|
['id' => 'delete_backup_s3', 'label' => 'Delete the selected backup permanently form S3 Storage'],
|
|
|
|
|
['id' => 'delete_backup_sftp', 'label' => 'Delete the selected backup permanently form SFTP Storage'],
|
2024-09-23 17:51:31 +00:00
|
|
|
],
|
2024-08-31 17:31:01 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|