25
25
use Illuminate \Queue \Middleware \WithoutOverlapping ;
26
26
use Illuminate \Queue \SerializesModels ;
27
27
use Illuminate \Support \Str ;
28
- use App \ Models \ InstanceSettings ;
28
+ use Visus \ Cuid2 \ Cuid2 ;
29
29
30
30
class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
31
31
{
@@ -399,6 +399,7 @@ private function backup_standalone_postgresql(string $database): void
399
399
$ backupCommand .= " $ this ->container_name pg_dump --format=custom --no-acl --no-owner --username {$ this ->database ->postgres_user } $ database > $ this ->backup_location " ;
400
400
401
401
$ commands [] = $ backupCommand ;
402
+ ray ($ commands );
402
403
$ this ->backup_output = instant_remote_process ($ commands , $ this ->server );
403
404
$ this ->backup_output = trim ($ this ->backup_output );
404
405
if ($ this ->backup_output === '' ) {
@@ -480,6 +481,7 @@ private function remove_old_backups(): void
480
481
private function upload_to_s3 (): void
481
482
{
482
483
try {
484
+ ray ($ this ->backup_location );
483
485
if (is_null ($ this ->s3 )) {
484
486
return ;
485
487
}
@@ -489,62 +491,20 @@ private function upload_to_s3(): void
489
491
$ bucket = $ this ->s3 ->bucket ;
490
492
$ endpoint = $ this ->s3 ->endpoint ;
491
493
$ this ->s3 ->testConnection (shouldSave: true );
492
- if (data_get ($ this ->backup , 'database_type ' ) === 'App\Models\ServiceDatabase ' ) {
493
- $ network = $ this ->database ->service ->destination ->network ;
494
- } else {
495
- $ network = $ this ->database ->destination ->network ;
496
- }
497
-
498
- $ this ->ensureHelperImageAvailable ();
494
+ $ configName = new Cuid2 ;
499
495
500
- $ fullImageName = $ this ->getFullImageName ();
501
- $ commands [] = "docker run -d --network {$ network } --name backup-of- {$ this ->backup ->uuid } --rm -v $ this ->backup_location : $ this ->backup_location :ro {$ fullImageName }" ;
502
- $ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc config host add temporary {$ endpoint } $ key $ secret " ;
503
- $ commands [] = "docker exec backup-of- {$ this ->backup ->uuid } mc cp $ this ->backup_location temporary/ $ bucket {$ this ->backup_dir }/ " ;
496
+ $ s3_copy_dir = str ($ this ->backup_location )->replace (backup_dir (), '/var/www/html/storage/app/backups/ ' );
497
+ $ commands [] = "docker exec coolify bash -c 'mc config host add {$ configName } {$ endpoint } $ key $ secret' " ;
498
+ $ commands [] = "docker exec coolify bash -c 'mc cp $ s3_copy_dir {$ configName }/ {$ bucket }{$ this ->backup_dir }/' " ;
504
499
instant_remote_process ($ commands , $ this ->server );
505
500
$ this ->add_to_backup_output ('Uploaded to S3. ' );
506
501
} catch (\Throwable $ e ) {
507
502
$ this ->add_to_backup_output ($ e ->getMessage ());
508
503
throw $ e ;
509
504
} finally {
510
- $ command = "docker rm -f backup-of- {$ this ->backup ->uuid }" ;
511
- instant_remote_process ([$ command ], $ this ->server );
512
- }
513
- }
514
-
515
- private function ensureHelperImageAvailable (): void
516
- {
517
- $ fullImageName = $ this ->getFullImageName ();
518
-
519
- $ imageExists = $ this ->checkImageExists ($ fullImageName );
520
-
521
- if (!$ imageExists ) {
522
- $ this ->pullHelperImage ($ fullImageName );
505
+ $ removeConfigCommands [] = "docker exec coolify bash -c 'mc config remove {$ configName }' " ;
506
+ $ removeConfigCommands [] = "docker exec coolify bash -c 'mc alias rm {$ configName }' " ;
507
+ instant_remote_process ($ removeConfigCommands , $ this ->server , false );
523
508
}
524
509
}
525
-
526
- private function checkImageExists (string $ fullImageName ): bool
527
- {
528
- $ result = instant_remote_process (["docker image inspect {$ fullImageName } >/dev/null 2>&1 && echo 'exists' || echo 'not exists' " ], $ this ->server , false );
529
- return trim ($ result ) === 'exists ' ;
530
- }
531
-
532
- private function pullHelperImage (string $ fullImageName ): void
533
- {
534
- try {
535
- instant_remote_process (["docker pull {$ fullImageName }" ], $ this ->server );
536
- } catch (\Exception $ e ) {
537
- $ errorMessage = "Failed to pull helper image: " . $ e ->getMessage ();
538
- $ this ->add_to_backup_output ($ errorMessage );
539
- throw new \RuntimeException ($ errorMessage );
540
- }
541
- }
542
-
543
- private function getFullImageName (): string
544
- {
545
- $ settings = InstanceSettings::get ();
546
- $ helperImage = config ('coolify.helper_image ' );
547
- $ latestVersion = $ settings ->helper_version ;
548
- return "{$ helperImage }: {$ latestVersion }" ;
549
- }
550
510
}
0 commit comments