2
2
3
3
namespace App \Jobs ;
4
4
5
- use App \Actions \Database \StopDatabase ;
6
5
use App \Events \BackupCreated ;
7
6
use App \Models \S3Storage ;
8
7
use App \Models \ScheduledDatabaseBackup ;
24
23
use Illuminate \Queue \InteractsWithQueue ;
25
24
use Illuminate \Queue \SerializesModels ;
26
25
use Illuminate \Support \Str ;
27
- use Visus \Cuid2 \Cuid2 ;
28
26
29
27
class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
30
28
{
@@ -63,30 +61,26 @@ class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
63
61
public function __construct ($ backup )
64
62
{
65
63
$ this ->backup = $ backup ;
66
- $ this ->team = Team::find ($ backup ->team_id );
67
- if (is_null ($ this ->team )) {
68
- return ;
69
- }
70
- if (data_get ($ this ->backup , 'database_type ' ) === 'App\Models\ServiceDatabase ' ) {
71
- $ this ->database = data_get ($ this ->backup , 'database ' );
72
- $ this ->server = $ this ->database ->service ->server ;
73
- $ this ->s3 = $ this ->backup ->s3 ;
74
- } else {
75
- $ this ->database = data_get ($ this ->backup , 'database ' );
76
- $ this ->server = $ this ->database ->destination ->server ;
77
- $ this ->s3 = $ this ->backup ->s3 ;
78
- }
79
64
}
80
65
81
66
public function handle (): void
82
67
{
83
68
try {
84
- // Check if team is exists
85
- if (is_null ($ this ->team )) {
86
- StopDatabase::run ($ this ->database );
87
- $ this ->database ->delete ();
88
-
89
- return ;
69
+ $ this ->team = Team::findOrFail ($ this ->backup ->team_id );
70
+ if (data_get ($ this ->backup , 'database_type ' ) === 'App\Models\ServiceDatabase ' ) {
71
+ $ this ->database = data_get ($ this ->backup , 'database ' );
72
+ $ this ->server = $ this ->database ->service ->server ;
73
+ $ this ->s3 = $ this ->backup ->s3 ;
74
+ } else {
75
+ $ this ->database = data_get ($ this ->backup , 'database ' );
76
+ $ this ->server = $ this ->database ->destination ->server ;
77
+ $ this ->s3 = $ this ->backup ->s3 ;
78
+ }
79
+ if (is_null ($ this ->server )) {
80
+ throw new \Exception ('Server not found?! ' );
81
+ }
82
+ if (is_null ($ this ->database )) {
83
+ throw new \Exception ('Database not found?! ' );
90
84
}
91
85
92
86
BackupCreated::dispatch ($ this ->team ->id );
@@ -237,7 +231,6 @@ public function handle(): void
237
231
}
238
232
}
239
233
$ this ->backup_dir = backup_dir ().'/databases/ ' .str ($ this ->team ->name )->slug ().'- ' .$ this ->team ->id .'/ ' .$ this ->directory_name ;
240
-
241
234
if ($ this ->database ->name === 'coolify-db ' ) {
242
235
$ databasesToBackup = ['coolify ' ];
243
236
$ this ->directory_name = $ this ->container_name = 'coolify-db ' ;
@@ -325,7 +318,9 @@ public function handle(): void
325
318
send_internal_notification ('DatabaseBackupJob failed with: ' .$ e ->getMessage ());
326
319
throw $ e ;
327
320
} finally {
328
- BackupCreated::dispatch ($ this ->team ->id );
321
+ if ($ this ->team ) {
322
+ BackupCreated::dispatch ($ this ->team ->id );
323
+ }
329
324
}
330
325
}
331
326
@@ -466,34 +461,6 @@ private function remove_old_backups(): void
466
461
}
467
462
}
468
463
469
- // private function upload_to_s3(): void
470
- // {
471
- // try {
472
- // if (is_null($this->s3)) {
473
- // return;
474
- // }
475
- // $key = $this->s3->key;
476
- // $secret = $this->s3->secret;
477
- // // $region = $this->s3->region;
478
- // $bucket = $this->s3->bucket;
479
- // $endpoint = $this->s3->endpoint;
480
- // $this->s3->testConnection(shouldSave: true);
481
- // $configName = new Cuid2;
482
-
483
- // $s3_copy_dir = str($this->backup_location)->replace(backup_dir(), '/var/www/html/storage/app/backups/');
484
- // $commands[] = "docker exec coolify bash -c 'mc config host add {$configName} {$endpoint} $key $secret'";
485
- // $commands[] = "docker exec coolify bash -c 'mc cp $s3_copy_dir {$configName}/{$bucket}{$this->backup_dir}/'";
486
- // instant_remote_process($commands, $this->server);
487
- // $this->add_to_backup_output('Uploaded to S3.');
488
- // } catch (\Throwable $e) {
489
- // $this->add_to_backup_output($e->getMessage());
490
- // throw $e;
491
- // } finally {
492
- // $removeConfigCommands[] = "docker exec coolify bash -c 'mc config remove {$configName}'";
493
- // $removeConfigCommands[] = "docker exec coolify bash -c 'mc alias rm {$configName}'";
494
- // instant_remote_process($removeConfigCommands, $this->server, false);
495
- // }
496
- // }
497
464
private function upload_to_s3 (): void
498
465
{
499
466
try {
@@ -515,10 +482,27 @@ private function upload_to_s3(): void
515
482
$ this ->ensureHelperImageAvailable ();
516
483
517
484
$ fullImageName = $ this ->getFullImageName ();
518
- $ commands [] = "docker run -d --network {$ network } --name backup-of- {$ this ->backup ->uuid } --rm -v $ this ->backup_location : $ this ->backup_location :ro {$ fullImageName }" ;
519
- $ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc config host add temporary {$ endpoint } $ key $ secret " ;
485
+
486
+ if (isDev ()) {
487
+ if ($ this ->database ->name === 'coolify-db ' ) {
488
+ $ backup_location_from = '/var/lib/docker/volumes/coolify_dev_backups_data/_data/coolify/coolify-db- ' .$ this ->server ->ip .$ this ->backup_file ;
489
+ $ commands [] = "docker run -d --network {$ network } --name backup-of- {$ this ->backup ->uuid } --rm -v $ backup_location_from: $ this ->backup_location :ro {$ fullImageName }" ;
490
+ } else {
491
+ $ backup_location_from = '/var/lib/docker/volumes/coolify_dev_backups_data/_data/databases/ ' .str ($ this ->team ->name )->slug ().'- ' .$ this ->team ->id .'/ ' .$ this ->directory_name .$ this ->backup_file ;
492
+ $ commands [] = "docker run -d --network {$ network } --name backup-of- {$ this ->backup ->uuid } --rm -v $ backup_location_from: $ this ->backup_location :ro {$ fullImageName }" ;
493
+ }
494
+ } else {
495
+ $ commands [] = "docker run -d --network {$ network } --name backup-of- {$ this ->backup ->uuid } --rm -v $ this ->backup_location : $ this ->backup_location :ro {$ fullImageName }" ;
496
+ }
497
+ if ($ this ->s3 ->isHetzner ()) {
498
+ $ endpointWithoutBucket = 'https:// ' .str ($ endpoint )->after ('https:// ' )->after ('. ' )->value ();
499
+ $ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc alias set --path=off --api=S3v4 temporary {$ endpointWithoutBucket } $ key $ secret " ;
500
+ } else {
501
+ $ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc config host add temporary {$ endpoint } $ key $ secret " ;
502
+ }
520
503
$ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc cp $ this ->backup_location temporary/ $ bucket {$ this ->backup_dir }/ " ;
521
504
instant_remote_process ($ commands , $ this ->server );
505
+
522
506
$ this ->add_to_backup_output ('Uploaded to S3. ' );
523
507
} catch (\Throwable $ e ) {
524
508
$ this ->add_to_backup_output ($ e ->getMessage ());
0 commit comments