fix(backup): use shared compression for database dumps

Centralize compressor selection and apply helper-image compression to dump-all database backups.
This commit is contained in:
Andras Bacsai committed 2026-08-15 15:51:11 +02:00
1 parent 909e776fbc
commit 0a6db15216
5 files changed
+106 -15

No files matched your search

@@ -1,9 +1,12 @@
<?php
use App\Jobs\DatabaseBackupJob;
use App\Models\InstanceSettings;
use App\Models\S3Storage;
use App\Models\ScheduledDatabaseBackup;
use App\Models\ScheduledDatabaseBackupExecution;
use App\Models\Server;
use App\Models\ServerSetting;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
@@ -322,3 +325,40 @@ test('database backup job escapes the S3 copy destination argument', function ()
->and($source)->toContain('mc cp {$escapedBackupLocation} {$escapedS3Destination}')
->and($source)->not->toContain('mc cp $this->backup_location temporary/$bucket{$this->backup_dir}/');
});
test('database dump compression uses the helper image and shared CPU setting', function (int $compressionCpuPercentage) {
InstanceSettings::unguarded(fn () => InstanceSettings::create(['id' => 0]));
$backup = new ScheduledDatabaseBackup(['timeout' => 3600]);
$job = new DatabaseBackupJob($backup);
$server = new Server;
$server->setRelation('settings', new ServerSetting([
'backup_compression_cpu_percentage' => $compressionCpuPercentage,
]));
$job->server = $server;
$command = (new ReflectionClass($job))
->getMethod('buildCompressedDumpCommand')
->invoke($job, 'docker exec database pg_dumpall');
expect($command)
->toStartWith('docker exec database pg_dumpall | docker run --rm -i')
->toContain('coolify-helper')
->toContain('command -v pigz')
->toContain('pigz -3 -p')
->toContain("\$(nproc) * {$compressionCpuPercentage} + 99")
->toContain('gzip -3');
})->with([
'low' => 25,
'high' => 75,
]);
test('all dump all database commands use shared helper compression', function () {
$source = file_get_contents(app_path('Jobs/DatabaseBackupJob.php'));
expect($source)
->toContain('$this->buildCompressedDumpCommand($backupCommand)')
->toContain('mysqldump -u root')
->toContain('mariadb-dump -u root')
->and(substr_count($source, '$this->buildCompressedDumpCommand($dumpCommand)'))->toBe(2)
->and($source)->not->toContain('| gzip >');
});