From bb6c9cf49e32be1ac7f67cf124822cc914591a42 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 29 Apr 2024 09:38:45 +0200 Subject: [PATCH] fix: backups --- app/Jobs/DatabaseBackupJob.php | 5 +- app/Jobs/SendMessageToTelegramJob.php | 2 +- .../Project/Database/Backup/Execution.php | 5 -- .../Project/Database/BackupExecutions.php | 8 +- app/Models/StandaloneClickhouse.php | 3 - app/Models/StandaloneDragonfly.php | 3 - app/Models/StandaloneKeydb.php | 3 - app/Models/StandaloneMariadb.php | 3 - app/Models/StandaloneMongodb.php | 3 - app/Models/StandaloneMysql.php | 3 - app/Models/StandalonePostgresql.php | 3 - app/Models/StandaloneRedis.php | 3 - app/Notifications/Database/BackupFailed.php | 13 ++- app/Notifications/Database/BackupSuccess.php | 8 +- .../database/backup-executions.blade.php | 84 ++++++++++--------- .../database/backup/execution.blade.php | 4 - .../database/scheduled-backups.blade.php | 14 ++-- 17 files changed, 71 insertions(+), 96 deletions(-) diff --git a/app/Jobs/DatabaseBackupJob.php b/app/Jobs/DatabaseBackupJob.php index befeffed0..a09ebeada 100644 --- a/app/Jobs/DatabaseBackupJob.php +++ b/app/Jobs/DatabaseBackupJob.php @@ -289,7 +289,7 @@ class DatabaseBackupJob implements ShouldQueue, ShouldBeEncrypted if ($this->backup->save_s3) { $this->upload_to_s3(); } - $this->team?->notify(new BackupSuccess($this->backup, $this->database)); + $this->team?->notify(new BackupSuccess($this->backup, $this->database, $database)); $this->backup_log->update([ 'status' => 'success', 'message' => $this->backup_output, @@ -305,8 +305,7 @@ class DatabaseBackupJob implements ShouldQueue, ShouldBeEncrypted ]); } send_internal_notification('DatabaseBackupJob failed with: ' . $e->getMessage()); - $this->team?->notify(new BackupFailed($this->backup, $this->database, $this->backup_output)); - throw $e; + $this->team?->notify(new BackupFailed($this->backup, $this->database, $this->backup_output, $database)); } } } catch (\Throwable $e) { diff --git a/app/Jobs/SendMessageToTelegramJob.php b/app/Jobs/SendMessageToTelegramJob.php index 4785da669..4191b02fe 100644 --- a/app/Jobs/SendMessageToTelegramJob.php +++ b/app/Jobs/SendMessageToTelegramJob.php @@ -57,7 +57,7 @@ class SendMessageToTelegramJob implements ShouldQueue, ShouldBeEncrypted } } $payload = [ - 'parse_mode' => 'markdown', + // 'parse_mode' => 'markdown', 'reply_markup' => json_encode([ 'inline_keyboard' => [ [...$inlineButtons], diff --git a/app/Livewire/Project/Database/Backup/Execution.php b/app/Livewire/Project/Database/Backup/Execution.php index 1f790d643..000b6fb2b 100644 --- a/app/Livewire/Project/Database/Backup/Execution.php +++ b/app/Livewire/Project/Database/Backup/Execution.php @@ -35,11 +35,6 @@ class Execution extends Component $this->executions = $executions; $this->s3s = currentTeam()->s3s; } - public function cleanupFailed() - { - $this->backup->executions()->where('status', 'failed')->delete(); - $this->dispatch('refreshBackupExecutions'); - } public function render() { return view('livewire.project.database.backup.execution'); diff --git a/app/Livewire/Project/Database/BackupExecutions.php b/app/Livewire/Project/Database/BackupExecutions.php index b127a685c..a34d849f2 100644 --- a/app/Livewire/Project/Database/BackupExecutions.php +++ b/app/Livewire/Project/Database/BackupExecutions.php @@ -2,9 +2,7 @@ namespace App\Livewire\Project\Database; -use Illuminate\Support\Facades\Storage; use Livewire\Component; -use Symfony\Component\HttpFoundation\StreamedResponse; class BackupExecutions extends Component { @@ -16,11 +14,15 @@ class BackupExecutions extends Component $userId = auth()->user()->id; return [ "echo-private:team.{$userId},BackupCreated" => 'refreshBackupExecutions', - "refreshBackupExecutions", "deleteBackup" ]; } + public function cleanupFailed() + { + $this->backup->executions()->where('status', 'failed')->delete(); + $this->refreshBackupExecutions(); + } public function deleteBackup($exeuctionId) { $execution = $this->backup->executions()->where('id', $exeuctionId)->first(); diff --git a/app/Models/StandaloneClickhouse.php b/app/Models/StandaloneClickhouse.php index 3746a32f5..2197d51df 100644 --- a/app/Models/StandaloneClickhouse.php +++ b/app/Models/StandaloneClickhouse.php @@ -207,7 +207,4 @@ class StandaloneClickhouse extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return $this->clickhouse_db; - } } diff --git a/app/Models/StandaloneDragonfly.php b/app/Models/StandaloneDragonfly.php index adc1ea6cc..7b18666b8 100644 --- a/app/Models/StandaloneDragonfly.php +++ b/app/Models/StandaloneDragonfly.php @@ -207,7 +207,4 @@ class StandaloneDragonfly extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return '0'; - } } diff --git a/app/Models/StandaloneKeydb.php b/app/Models/StandaloneKeydb.php index ff91322a0..c2c1b98da 100644 --- a/app/Models/StandaloneKeydb.php +++ b/app/Models/StandaloneKeydb.php @@ -208,7 +208,4 @@ class StandaloneKeydb extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return '0'; - } } diff --git a/app/Models/StandaloneMariadb.php b/app/Models/StandaloneMariadb.php index 37d39f882..5e18bbfde 100644 --- a/app/Models/StandaloneMariadb.php +++ b/app/Models/StandaloneMariadb.php @@ -208,7 +208,4 @@ class StandaloneMariadb extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return $this->mariadb_database; - } } diff --git a/app/Models/StandaloneMongodb.php b/app/Models/StandaloneMongodb.php index 30414e26b..8e4d327a3 100644 --- a/app/Models/StandaloneMongodb.php +++ b/app/Models/StandaloneMongodb.php @@ -223,7 +223,4 @@ class StandaloneMongodb extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return null; - } } diff --git a/app/Models/StandaloneMysql.php b/app/Models/StandaloneMysql.php index 53e9b6f22..eede451d7 100644 --- a/app/Models/StandaloneMysql.php +++ b/app/Models/StandaloneMysql.php @@ -209,7 +209,4 @@ class StandaloneMysql extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return $this->mysql_database; - } } diff --git a/app/Models/StandalonePostgresql.php b/app/Models/StandalonePostgresql.php index 6435c49de..cf449a815 100644 --- a/app/Models/StandalonePostgresql.php +++ b/app/Models/StandalonePostgresql.php @@ -208,7 +208,4 @@ class StandalonePostgresql extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return $this->postgres_db; - } } diff --git a/app/Models/StandaloneRedis.php b/app/Models/StandaloneRedis.php index de18c8c07..da4701df9 100644 --- a/app/Models/StandaloneRedis.php +++ b/app/Models/StandaloneRedis.php @@ -204,7 +204,4 @@ class StandaloneRedis extends BaseModel { return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); } - public function database_name() { - return '0'; - } } diff --git a/app/Notifications/Database/BackupFailed.php b/app/Notifications/Database/BackupFailed.php index 67fd27589..7cad486b3 100644 --- a/app/Notifications/Database/BackupFailed.php +++ b/app/Notifications/Database/BackupFailed.php @@ -15,21 +15,20 @@ class BackupFailed extends Notification implements ShouldQueue { use Queueable; - public $tries = 1; + public $backoff = 10; + public $tries = 2; public string $name; - public ?string $database_name = null; public string $frequency; - public function __construct(ScheduledDatabaseBackup $backup, public $database, public $output) + public function __construct(ScheduledDatabaseBackup $backup, public $database, public $output, public $database_name) { $this->name = $database->name; - $this->database_name = $database->database_name(); $this->frequency = $backup->frequency; } public function via(object $notifiable): array { - return [DiscordChannel::class, TelegramChannel::class, MailChannel::class]; + return setNotificationChannels($notifiable, 'database_backups'); } public function toMail(): MailMessage @@ -47,11 +46,11 @@ class BackupFailed extends Notification implements ShouldQueue public function toDiscord(): string { - return "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason: {$this->output}"; + return "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason:\n{$this->output}"; } public function toTelegram(): array { - $message = "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason: {$this->output}"; + $message = "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason:\n{$this->output}"; return [ "message" => $message, ]; diff --git a/app/Notifications/Database/BackupSuccess.php b/app/Notifications/Database/BackupSuccess.php index d69a31f46..c43a12276 100644 --- a/app/Notifications/Database/BackupSuccess.php +++ b/app/Notifications/Database/BackupSuccess.php @@ -12,15 +12,14 @@ class BackupSuccess extends Notification implements ShouldQueue { use Queueable; - public $tries = 1; + public $backoff = 10; + public $tries = 3; public string $name; - public ?string $database_name = null; public string $frequency; - public function __construct(ScheduledDatabaseBackup $backup, public $database) + public function __construct(ScheduledDatabaseBackup $backup, public $database, public $database_name) { $this->name = $database->name; - $this->database_name = $database->database_name(); $this->frequency = $backup->frequency; } @@ -48,6 +47,7 @@ class BackupSuccess extends Notification implements ShouldQueue public function toTelegram(): array { $message = "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was successful."; + ray($message); return [ "message" => $message, ]; diff --git a/resources/views/livewire/project/database/backup-executions.blade.php b/resources/views/livewire/project/database/backup-executions.blade.php index ae9428923..34e2b965d 100644 --- a/resources/views/livewire/project/database/backup-executions.blade.php +++ b/resources/views/livewire/project/database/backup-executions.blade.php @@ -1,43 +1,51 @@ -
- @forelse($executions as $execution) -
data_get($execution, 'status') === 'success', - 'border-red-500' => data_get($execution, 'status') === 'failed', - ])> - @if (data_get($execution, 'status') === 'running') -
- -
- @endif -
Database: {{ data_get($execution, 'database_name', 'N/A') }}
-
Status: {{ data_get($execution, 'status') }}
-
Started At: {{ data_get($execution, 'created_at') }}
- @if (data_get($execution, 'message')) -
Message: {{ data_get($execution, 'message') }}
- @endif -
Size: {{ data_get($execution, 'size') }} B / {{ round((int) data_get($execution, 'size') / 1024, 2) }} - kB / {{ round((int) data_get($execution, 'size') / 1024 / 1024, 3) }} MB -
-
Location: {{ data_get($execution, 'filename', 'N/A') }}
-
-
- @if (data_get($execution, 'status') === 'success') - Download +
+
+

Executions

+ Cleanup Failed Backups +
+
+ @forelse($executions as $execution) + data_get($execution, 'status') === 'success', + 'border-red-500' => data_get($execution, 'status') === 'failed', + ])> + @if (data_get($execution, 'status') === 'running') +
+ +
@endif - - - Delete - - This will delete this backup. It is not reversible.
Please think again. -
-
- +
Database: {{ data_get($execution, 'database_name', 'N/A') }}
+
Status: {{ data_get($execution, 'status') }}
+
Started At: {{ data_get($execution, 'created_at') }}
+ @if (data_get($execution, 'message')) +
Message: {{ data_get($execution, 'message') }}
+ @endif +
Size: {{ data_get($execution, 'size') }} B / + {{ round((int) data_get($execution, 'size') / 1024, 2) }} + kB / {{ round((int) data_get($execution, 'size') / 1024 / 1024, 3) }} MB +
+
Location: {{ data_get($execution, 'filename', 'N/A') }}
+
+
+ @if (data_get($execution, 'status') === 'success') + Download + @endif + + + Delete + + This will delete this backup. It is not reversible.
Please think again. +
+
+ - @empty -
No executions found.
- @endforelse + @empty +
No executions found.
+ @endforelse +