| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Verify that we can take and verify backups with various checksum types. |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
use File::Path qw(rmtree); |
| 9 |
use PostgreSQL::Test::Cluster; |
| 10 |
use PostgreSQL::Test::Utils; |
| 11 |
use Test::More; |
| 12 |
|
| 13 |
my $primary = PostgreSQL::Test::Cluster->new('primary'); |
| 14 |
$primary->init(allows_streaming => 1); |
| 15 |
$primary->start; |
| 16 |
|
| 17 |
sub test_checksums |
| 18 |
{ |
| 19 |
my ($format, $algorithm) = @_; |
| 20 |
my $backup_path = $primary->backup_dir . '/' . $format . '/' . $algorithm; |
| 21 |
my @backup = ( |
| 22 |
'pg_basebackup', |
| 23 |
'--pgdata' => $backup_path, |
| 24 |
'--manifest-checksums' => $algorithm, |
| 25 |
'--no-sync', |
| 26 |
'--checkpoint' => 'fast'); |
| 27 |
my @verify = ('pg_verifybackup', '--exit-on-error', $backup_path); |
| 28 |
|
| 29 |
if ($format eq 'tar') |
| 30 |
{ |
| 31 |
# Add switch to get a tar-format backup |
| 32 |
push @backup, ('--format' => 'tar'); |
| 33 |
|
| 34 |
# Add switch to skip WAL verification, which is not yet supported for |
| 35 |
# tar-format backups |
| 36 |
push @verify, ('--no-parse-wal'); |
| 37 |
} |
| 38 |
|
| 39 |
# A backup with a bogus algorithm should fail. |
| 40 |
if ($algorithm eq 'bogus') |
| 41 |
{ |
| 42 |
$primary->command_fails(\@backup, |
| 43 |
"$format format backup fails with algorithm \"$algorithm\""); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
# A backup with a valid algorithm should work. |
| 48 |
$primary->command_ok(\@backup, |
| 49 |
"$format format backup ok with algorithm \"$algorithm\""); |
| 50 |
|
| 51 |
# We expect each real checksum algorithm to be mentioned on every line of |
| 52 |
# the backup manifest file except the first and last; for simplicity, we |
| 53 |
# just check that it shows up lots of times. When the checksum algorithm |
| 54 |
# is none, we just check that the manifest exists. |
| 55 |
if ($algorithm eq 'none') |
| 56 |
{ |
| 57 |
ok( -f "$backup_path/backup_manifest", |
| 58 |
"$format format backup manifest exists"); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
my $manifest = slurp_file("$backup_path/backup_manifest"); |
| 63 |
my $count_of_algorithm_in_manifest = |
| 64 |
(() = $manifest =~ /$algorithm/mig); |
| 65 |
cmp_ok($count_of_algorithm_in_manifest, |
| 66 |
'>', 100, "$algorithm is mentioned many times in the manifest"); |
| 67 |
} |
| 68 |
|
| 69 |
# Make sure that it verifies OK. |
| 70 |
$primary->command_ok(\@verify, |
| 71 |
"verify $format format backup with algorithm \"$algorithm\""); |
| 72 |
|
| 73 |
# Remove backup immediately to save disk space. |
| 74 |
rmtree($backup_path); |
| 75 |
} |
| 76 |
|
| 77 |
# Do the check |
| 78 |
for my $format (qw(plain tar)) |
| 79 |
{ |
| 80 |
for my $algorithm (qw(bogus none crc32c sha224 sha256 sha384 sha512)) |
| 81 |
{ |
| 82 |
test_checksums($format, $algorithm); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
done_testing(); |