Debian Codegraph @

src/bin/pg_verifybackup/t/003_corruption.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Verify that various forms of corruption are detected by pg_verifybackup.
5
6 use strict;
7 use warnings FATAL => 'all';
8 use Cwd;
9 use File::Path qw(rmtree);
10 use File::Copy;
11 use PostgreSQL::Test::Cluster;
12 use PostgreSQL::Test::Utils;
13 use Test::More;
14
15 my $tar = $ENV{TAR};
16 my @tar_p_flags = tar_portability_options($tar);
17
18 my $primary = PostgreSQL::Test::Cluster->new('primary');
19 $primary->init(allows_streaming => 1);
20 $primary->start;
21
22 # Include a user-defined tablespace in the hopes of detecting problems in that
23 # area.
24 my $source_ts_path = PostgreSQL::Test::Utils::tempdir_short();
25 my $source_ts_prefix = $source_ts_path;
26 $source_ts_prefix =~ s!(^[A-Z]:/[^/]*)/.*!$1!;
27
28 $primary->safe_psql('postgres', <<EOM);
29 CREATE TABLE x1 (a int);
30 INSERT INTO x1 VALUES (111);
31 CREATE TABLESPACE ts1 LOCATION '$source_ts_path';
32 CREATE TABLE x2 (a int) TABLESPACE ts1;
33 INSERT INTO x1 VALUES (222);
34 EOM
35
36 my @scenario = (
37 {
38 'name' => 'extra_file',
39 'mutilate' => \&mutilate_extra_file,
40 'fails_like' =>
41 qr/extra_file.*present (on disk|in archive "[^"]+") but not in the manifest/
42 },
43 {
44 'name' => 'extra_tablespace_file',
45 'mutilate' => \&mutilate_extra_tablespace_file,
46 'fails_like' =>
47 qr/extra_ts_file.*present (on disk|in archive "[^"]+") but not in the manifest/
48 },
49 {
50 'name' => 'missing_file',
51 'mutilate' => \&mutilate_missing_file,
52 'fails_like' =>
53 qr/pg_xact\/0000.*present in the manifest but not (on disk|in archive "[^"]+")/
54 },
55 {
56 'name' => 'missing_tablespace',
57 'mutilate' => \&mutilate_missing_tablespace,
58 'fails_like' =>
59 qr/pg_tblspc.*present in the manifest but not (on disk|in archive "[^"]+")/
60 },
61 {
62 'name' => 'append_to_file',
63 'mutilate' => \&mutilate_append_to_file,
64 'fails_like' =>
65 qr/has size \d+ (on disk|in archive "[^"]+") but size \d+ in the manifest/
66 },
67 {
68 'name' => 'truncate_file',
69 'mutilate' => \&mutilate_truncate_file,
70 'fails_like' =>
71 qr/has size 0 (on disk|in archive "[^"]+") but size \d+ in the manifest/
72 },
73 {
74 'name' => 'replace_file',
75 'mutilate' => \&mutilate_replace_file,
76 'fails_like' => qr/checksum mismatch for file/
77 },
78 {
79 'name' => 'system_identifier',
80 'mutilate' => \&mutilate_system_identifier,
81 'fails_like' =>
82 qr/manifest system identifier is .*, but control file has/
83 },
84 {
85 'name' => 'bad_manifest',
86 'mutilate' => \&mutilate_bad_manifest,
87 'fails_like' => qr/manifest checksum mismatch/
88 },
89 {
90 'name' => 'open_file_fails',
91 'mutilate' => \&mutilate_open_file_fails,
92 'fails_like' => qr/could not open file/,
93 'needs_unix_permissions' => 1
94 },
95 {
96 'name' => 'open_directory_fails',
97 'mutilate' => \&mutilate_open_directory_fails,
98 'cleanup' => \&cleanup_open_directory_fails,
99 'fails_like' => qr/could not open directory/,
100 'needs_unix_permissions' => 1
101 },
102 {
103 'name' => 'search_directory_fails',
104 'mutilate' => \&mutilate_search_directory_fails,
105 'cleanup' => \&cleanup_search_directory_fails,
106 'fails_like' => qr/could not stat file or directory/,
107 'needs_unix_permissions' => 1
108 });
109
110 for my $scenario (@scenario)
111 {
112 my $name = $scenario->{'name'};
113
114 SKIP:
115 {
116 skip "unix-style permissions not supported on Windows", 4
117 if ($scenario->{'needs_unix_permissions'}
118 && ($windows_os || $Config::Config{osname} eq 'cygwin'));
119
120 # Take a backup and check that it verifies OK.
121 my $backup_path = $primary->backup_dir . '/' . $name;
122 my $backup_ts_path = PostgreSQL::Test::Utils::tempdir_short();
123 # The tablespace map parameter confuses Msys2, which tries to mangle
124 # it. Tell it not to.
125 # See https://www.msys2.org/wiki/Porting/#filesystem-namespaces
126 local $ENV{MSYS2_ARG_CONV_EXCL} = $source_ts_prefix;
127 $primary->command_ok(
128 [
129 'pg_basebackup',
130 '--pgdata' => $backup_path,
131 '--no-sync',
132 '--checkpoint' => 'fast',
133 '--tablespace-mapping' =>
134 "${source_ts_path}=${backup_ts_path}",
135 ],
136 "base backup ok");
137 command_ok([ 'pg_verifybackup', $backup_path ],
138 "intact backup verified");
139
140 # Mutilate the backup in some way.
141 $scenario->{'mutilate'}->($backup_path);
142
143 # Now check that the backup no longer verifies.
144 command_fails_like(
145 [ 'pg_verifybackup', $backup_path ],
146 $scenario->{'fails_like'},
147 "corrupt backup fails verification: $name");
148
149 # Run cleanup hook, if provided.
150 $scenario->{'cleanup'}->($backup_path)
151 if exists $scenario->{'cleanup'};
152
153 # Turn it into a tar-format backup and see if we can still detect the
154 # same problem, unless the scenario needs UNIX permissions or we don't
155 # have a TAR program available. Note that this destructively modifies
156 # the backup directory.
157 if ( !$scenario->{'needs_unix_permissions'}
158 && defined $tar
159 && $tar ne '')
160 {
161 my $tar_backup_path = $primary->backup_dir . '/tar_' . $name;
162 mkdir($tar_backup_path) || die "mkdir $tar_backup_path: $!";
163
164 # tar and then remove each tablespace. We remove the original files
165 # so that they don't also end up in base.tar.
166 my @tsoid = grep { $_ ne '.' && $_ ne '..' }
167 slurp_dir("$backup_path/pg_tblspc");
168 my $cwd = getcwd;
169 for my $tsoid (@tsoid)
170 {
171 my $tspath = $backup_path . '/pg_tblspc/' . $tsoid;
172
173 chdir($tspath) || die "chdir: $!";
174 command_ok(
175 [
176 $tar, @tar_p_flags,
177 '-cf' => "$tar_backup_path/$tsoid.tar",
178 '.'
179 ]);
180 chdir($cwd) || die "chdir: $!";
181 rmtree($tspath);
182 }
183
184 # tar and remove pg_wal
185 chdir($backup_path . '/pg_wal') || die "chdir: $!";
186 command_ok(
187 [
188 $tar, @tar_p_flags,
189 '-cf' => "$tar_backup_path/pg_wal.tar",
190 '.'
191 ]);
192 chdir($cwd) || die "chdir: $!";
193 rmtree($backup_path . '/pg_wal');
194
195 # move the backup manifest
196 move(
197 $backup_path . '/backup_manifest',
198 $tar_backup_path . '/backup_manifest'
199 ) or die "could not copy manifest to $tar_backup_path";
200
201 # Construct base.tar with what's left.
202 chdir($backup_path) || die "chdir: $!";
203 command_ok(
204 [
205 $tar, @tar_p_flags,
206 '-cf' => "$tar_backup_path/base.tar",
207 '.'
208 ]);
209 chdir($cwd) || die "chdir: $!";
210
211 # Now check that the backup no longer verifies. We must use -n
212 # here, because pg_waldump can't yet read WAL from a tarfile.
213 command_fails_like(
214 [ 'pg_verifybackup', '--no-parse-wal', $tar_backup_path ],
215 $scenario->{'fails_like'},
216 "corrupt backup fails verification: $name");
217
218 # Use rmtree to reclaim space.
219 rmtree($tar_backup_path);
220 }
221
222 # Use rmtree to reclaim space.
223 rmtree($backup_path);
224 }
225 }
226
227 sub create_extra_file
228 {
229 my ($backup_path, $relative_path) = @_;
230 my $pathname = "$backup_path/$relative_path";
231 open(my $fh, '>', $pathname) || die "open $pathname: $!";
232 print $fh "This is an extra file.\n";
233 close($fh);
234 return;
235 }
236
237 # Add a file into the root directory of the backup.
238 sub mutilate_extra_file
239 {
240 my ($backup_path) = @_;
241 create_extra_file($backup_path, "extra_file");
242 return;
243 }
244
245 # Add a file inside the user-defined tablespace.
246 sub mutilate_extra_tablespace_file
247 {
248 my ($backup_path) = @_;
249 my ($tsoid) =
250 grep { $_ ne '.' && $_ ne '..' } slurp_dir("$backup_path/pg_tblspc");
251 my ($catvdir) = grep { $_ ne '.' && $_ ne '..' }
252 slurp_dir("$backup_path/pg_tblspc/$tsoid");
253 my ($tsdboid) = grep { $_ ne '.' && $_ ne '..' }
254 slurp_dir("$backup_path/pg_tblspc/$tsoid/$catvdir");
255 create_extra_file($backup_path,
256 "pg_tblspc/$tsoid/$catvdir/$tsdboid/extra_ts_file");
257 return;
258 }
259
260 # Remove a file.
261 sub mutilate_missing_file
262 {
263 my ($backup_path) = @_;
264 my $pathname = "$backup_path/pg_xact/0000";
265 unlink($pathname) || die "$pathname: $!";
266 return;
267 }
268
269 # Remove the symlink to the user-defined tablespace.
270 sub mutilate_missing_tablespace
271 {
272 my ($backup_path) = @_;
273 my ($tsoid) =
274 grep { $_ ne '.' && $_ ne '..' } slurp_dir("$backup_path/pg_tblspc");
275 my $pathname = "$backup_path/pg_tblspc/$tsoid";
276 if ($windows_os)
277 {
278 # rmdir works on some windows setups, unlink on others.
279 # Instead of trying to implement precise rules, just try one and then
280 # the other.
281 unless (rmdir($pathname))
282 {
283 my $err = $!;
284 unlink($pathname) || die "$pathname: rmdir: $err, unlink: $!";
285 }
286 }
287 else
288 {
289 unlink($pathname) || die "$pathname: $!";
290 }
291 return;
292 }
293
294 # Append an additional bytes to a file.
295 sub mutilate_append_to_file
296 {
297 my ($backup_path) = @_;
298 append_to_file "$backup_path/global/pg_control", 'x';
299 return;
300 }
301
302 # Truncate a file to zero length.
303 sub mutilate_truncate_file
304 {
305 my ($backup_path) = @_;
306 my $pathname = "$backup_path/pg_hba.conf";
307 open(my $fh, '>', $pathname) || die "open $pathname: $!";
308 close($fh);
309 return;
310 }
311
312 # Replace a file's contents without changing the length of the file. This is
313 # not a particularly efficient way to do this, so we pick a file that's
314 # expected to be short.
315 sub mutilate_replace_file
316 {
317 my ($backup_path) = @_;
318 my $pathname = "$backup_path/PG_VERSION";
319 my $contents = slurp_file($pathname);
320 open(my $fh, '>', $pathname) || die "open $pathname: $!";
321 print $fh 'q' x length($contents);
322 close($fh);
323 return;
324 }
325
326 # Copy manifest of other backups to demonstrate the case where the wrong
327 # manifest is referred
328 sub mutilate_system_identifier
329 {
330 my ($backup_path) = @_;
331
332 # Set up another new database instance with different system identifier and
333 # make backup
334 my $node = PostgreSQL::Test::Cluster->new('node');
335 $node->init(force_initdb => 1, allows_streaming => 1);
336 $node->start;
337 $node->backup('backup2');
338 move($node->backup_dir . '/backup2/backup_manifest',
339 $backup_path . '/backup_manifest')
340 or BAIL_OUT "could not copy manifest to $backup_path";
341 $node->teardown_node(fail_ok => 1);
342 return;
343 }
344
345 # Corrupt the backup manifest.
346 sub mutilate_bad_manifest
347 {
348 my ($backup_path) = @_;
349 append_to_file "$backup_path/backup_manifest", "\n";
350 return;
351 }
352
353 # Create a file that can't be opened. (This is skipped on Windows.)
354 sub mutilate_open_file_fails
355 {
356 my ($backup_path) = @_;
357 my $pathname = "$backup_path/PG_VERSION";
358 chmod(0, $pathname) || die "chmod $pathname: $!";
359 return;
360 }
361
362 # Create a directory that can't be opened. (This is skipped on Windows.)
363 sub mutilate_open_directory_fails
364 {
365 my ($backup_path) = @_;
366 my $pathname = "$backup_path/pg_subtrans";
367 chmod(0, $pathname) || die "chmod $pathname: $!";
368 return;
369 }
370
371 # restore permissions on the unreadable directory we created.
372 sub cleanup_open_directory_fails
373 {
374 my ($backup_path) = @_;
375 my $pathname = "$backup_path/pg_subtrans";
376 chmod(0700, $pathname) || die "chmod $pathname: $!";
377 return;
378 }
379
380 # Create a directory that can't be searched. (This is skipped on Windows.)
381 sub mutilate_search_directory_fails
382 {
383 my ($backup_path) = @_;
384 my $pathname = "$backup_path/base";
385 chmod(0400, $pathname) || die "chmod $pathname: $!";
386 return;
387 }
388
389 # rmtree can't cope with a mode 400 directory, so change back to 700.
390 sub cleanup_search_directory_fails
391 {
392 my ($backup_path) = @_;
393 my $pathname = "$backup_path/base";
394 chmod(0700, $pathname) || die "chmod $pathname: $!";
395 return;
396 }
397
398 done_testing();