Debian Codegraph @

src/bin/pg_verifybackup/t/008_untar.pl

raw
1 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
2
3 # This test case aims to verify that server-side backups and server-side
4 # backup compression work properly, and it also aims to verify that
5 # pg_verifybackup can verify a base backup that didn't start out in plain
6 # format.
7
8 use strict;
9 use warnings FATAL => 'all';
10 use File::Path qw(rmtree);
11 use PostgreSQL::Test::Cluster;
12 use PostgreSQL::Test::Utils;
13 use Test::More;
14
15 my $primary = PostgreSQL::Test::Cluster->new('primary');
16 $primary->init(allows_streaming => 1);
17 $primary->start;
18
19 # Create file with some random data and an arbitrary size, useful to check
20 # the solidity of the compression and decompression logic. The size of the
21 # file is chosen to be around 640kB. This has proven to be large enough to
22 # detect some issues related to LZ4, and low enough to not impact the runtime
23 # of the test significantly.
24 my $junk_data = $primary->safe_psql(
25 'postgres', qq(
26 SELECT string_agg(encode(sha256(i::bytea), 'hex'), '')
27 FROM generate_series(1, 10240) s(i);));
28 my $data_dir = $primary->data_dir;
29 my $junk_file = "$data_dir/junk";
30 open my $jf, '>', $junk_file
31 or die "Could not create junk file: $!";
32 print $jf $junk_data;
33 close $jf;
34
35 # Create a tablespace directory.
36 my $source_ts_path = PostgreSQL::Test::Utils::tempdir_short();
37
38 # Create a tablespace with table in it.
39 $primary->safe_psql(
40 'postgres', qq(
41 CREATE TABLESPACE regress_ts1 LOCATION '$source_ts_path';
42 SELECT oid FROM pg_tablespace WHERE spcname = 'regress_ts1';
43 CREATE TABLE regress_tbl1(i int) TABLESPACE regress_ts1;
44 INSERT INTO regress_tbl1 VALUES(generate_series(1,5));));
45 my $tsoid = $primary->safe_psql(
46 'postgres', qq(
47 SELECT oid FROM pg_tablespace WHERE spcname = 'regress_ts1'));
48
49 my $backup_path = $primary->backup_dir . '/server-backup';
50 my $extract_path = $primary->backup_dir . '/extracted-backup';
51
52 my @test_configuration = (
53 {
54 'compression_method' => 'none',
55 'backup_flags' => [],
56 'backup_archive' => [ 'base.tar', "$tsoid.tar" ],
57 'enabled' => 1
58 },
59 {
60 'compression_method' => 'gzip',
61 'backup_flags' => [ '--compress', 'server-gzip' ],
62 'backup_archive' => [ 'base.tar.gz', "$tsoid.tar.gz" ],
63 'enabled' => check_pg_config("#define HAVE_LIBZ 1")
64 },
65 {
66 'compression_method' => 'lz4',
67 'backup_flags' => [ '--compress', 'server-lz4' ],
68 'backup_archive' => [ 'base.tar.lz4', "$tsoid.tar.lz4" ],
69 'enabled' => check_pg_config("#define USE_LZ4 1")
70 },
71 {
72 'compression_method' => 'lz4',
73 'backup_flags' => [ '--compress', 'server-lz4:5' ],
74 'backup_archive' => [ 'base.tar.lz4', "$tsoid.tar.lz4" ],
75 'enabled' => check_pg_config("#define USE_LZ4 1")
76 },
77 {
78 'compression_method' => 'zstd',
79 'backup_flags' => [ '--compress', 'server-zstd' ],
80 'backup_archive' => [ 'base.tar.zst', "$tsoid.tar.zst" ],
81 'enabled' => check_pg_config("#define USE_ZSTD 1")
82 },
83 {
84 'compression_method' => 'zstd',
85 'backup_flags' => [ '--compress', 'server-zstd:level=1,long' ],
86 'backup_archive' => [ 'base.tar.zst', "$tsoid.tar.zst" ],
87 'enabled' => check_pg_config("#define USE_ZSTD 1")
88 });
89
90 for my $tc (@test_configuration)
91 {
92 my $method = $tc->{'compression_method'};
93
94 SKIP:
95 {
96 skip "$method compression not supported by this build", 3
97 if !$tc->{'enabled'};
98 skip "no decompressor available for $method", 3
99 if exists $tc->{'decompress_program'}
100 && (!defined $tc->{'decompress_program'}
101 || $tc->{'decompress_program'} eq '');
102
103 # Take a server-side backup.
104 $primary->command_ok(
105 [
106 'pg_basebackup', '--no-sync',
107 '--checkpoint' => 'fast',
108 '--target' => "server:$backup_path",
109 '--wal-method' => 'fetch',
110 @{ $tc->{'backup_flags'} },
111 ],
112 "server side backup, compression $method");
113
114
115 # Verify that the we got the files we expected.
116 my $backup_files = join(',',
117 sort grep { $_ ne '.' && $_ ne '..' } slurp_dir($backup_path));
118 my $expected_backup_files =
119 join(',', sort ('backup_manifest', @{ $tc->{'backup_archive'} }));
120 is($backup_files, $expected_backup_files,
121 "found expected backup files, compression $method");
122
123 # Verify tar backup.
124 $primary->command_ok(
125 [
126 'pg_verifybackup', '--no-parse-wal',
127 '--exit-on-error', $backup_path,
128 ],
129 "verify backup, compression $method");
130
131 # Cleanup.
132 rmtree($backup_path);
133 rmtree($extract_path);
134 }
135 }
136
137 done_testing();