Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * pg_verifybackup.c
4 : * Verify a backup against a backup manifest.
5 : *
6 : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : * src/bin/pg_verifybackup/pg_verifybackup.c
10 : *
11 : *-------------------------------------------------------------------------
12 : */
13 :
14 : #include "postgres_fe.h"
15 :
16 : #include <dirent.h>
17 : #include <fcntl.h>
18 : #include <sys/stat.h>
19 : #include <time.h>
20 :
21 : #include "common/logging.h"
22 : #include "common/parse_manifest.h"
23 : #include "fe_utils/simple_list.h"
24 : #include "getopt_long.h"
25 : #include "pg_verifybackup.h"
26 : #include "pgtime.h"
27 :
28 : /*
29 : * For efficiency, we'd like our hash table containing information about the
30 : * manifest to start out with approximately the correct number of entries.
31 : * There's no way to know the exact number of entries without reading the whole
32 : * file, but we can get an estimate by dividing the file size by the estimated
33 : * number of bytes per line.
34 : *
35 : * This could be off by about a factor of two in either direction, because the
36 : * checksum algorithm has a big impact on the line lengths; e.g. a SHA512
37 : * checksum is 128 hex bytes, whereas a CRC-32C value is only 8, and there
38 : * might be no checksum at all.
39 : */
40 : #define ESTIMATED_BYTES_PER_MANIFEST_LINE 100
41 :
42 : /*
43 : * How many bytes should we try to read from a file at once?
44 : */
45 : #define READ_CHUNK_SIZE (128 * 1024)
46 :
47 : static manifest_data *parse_manifest_file(char *manifest_path);
48 : static void verifybackup_version_cb(JsonManifestParseContext *context,
49 : int manifest_version);
50 : static void verifybackup_system_identifier(JsonManifestParseContext *context,
51 : uint64 manifest_system_identifier);
52 : static void verifybackup_per_file_cb(JsonManifestParseContext *context,
53 : const char *pathname, size_t size,
54 : pg_checksum_type checksum_type,
55 : int checksum_length,
56 : uint8 *checksum_payload);
57 : static void verifybackup_per_wal_range_cb(JsonManifestParseContext *context,
58 : TimeLineID tli,
59 : XLogRecPtr start_lsn,
60 : XLogRecPtr end_lsn);
61 : static void report_manifest_error(JsonManifestParseContext *context,
62 : const char *fmt,...)
63 : pg_attribute_printf(2, 3) pg_attribute_noreturn();
64 :
65 : static void verify_backup_directory(verifier_context *context,
66 : char *relpath, char *fullpath);
67 : static void verify_backup_file(verifier_context *context,
68 : char *relpath, char *fullpath);
69 : static void verify_control_file(const char *controlpath,
70 : uint64 manifest_system_identifier);
71 : static void report_extra_backup_files(verifier_context *context);
72 : static void verify_backup_checksums(verifier_context *context);
73 : static void verify_file_checksum(verifier_context *context,
74 : manifest_file *m, char *fullpath,
75 : uint8 *buffer);
76 : static void parse_required_wal(verifier_context *context,
77 : char *pg_waldump_path,
78 : char *wal_directory);
79 :
80 : static void progress_report(bool finished);
81 : static void usage(void);
82 :
83 : static const char *progname;
84 :
85 : /* is progress reporting enabled? */
86 : static bool show_progress = false;
87 :
88 : /* Progress indicators */
89 : static uint64 total_size = 0;
90 : static uint64 done_size = 0;
91 :
92 : /*
93 : * Main entry point.
94 : */
95 : int
96 198 : main(int argc, char **argv)
97 : {
98 : static struct option long_options[] = {
99 : {"exit-on-error", no_argument, NULL, 'e'},
100 : {"ignore", required_argument, NULL, 'i'},
101 : {"manifest-path", required_argument, NULL, 'm'},
102 : {"no-parse-wal", no_argument, NULL, 'n'},
103 : {"progress", no_argument, NULL, 'P'},
104 : {"quiet", no_argument, NULL, 'q'},
105 : {"skip-checksums", no_argument, NULL, 's'},
106 : {"wal-directory", required_argument, NULL, 'w'},
107 : {NULL, 0, NULL, 0}
108 : };
109 :
110 : int c;
111 : verifier_context context;
112 198 : char *manifest_path = NULL;
113 198 : bool no_parse_wal = false;
114 198 : bool quiet = false;
115 198 : char *wal_directory = NULL;
116 198 : char *pg_waldump_path = NULL;
117 :
118 198 : pg_logging_init(argv[0]);
119 198 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_verifybackup"));
120 198 : progname = get_progname(argv[0]);
121 :
122 198 : memset(&context, 0, sizeof(context));
123 :
124 198 : if (argc > 1)
125 : {
126 196 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
127 : {
128 2 : usage();
129 2 : exit(0);
130 : }
131 194 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
132 : {
133 2 : puts("pg_verifybackup (PostgreSQL) " PG_VERSION);
134 2 : exit(0);
135 : }
136 : }
137 :
138 : /*
139 : * Skip certain files in the toplevel directory.
140 : *
141 : * Ignore the backup_manifest file, because it's not included in the
142 : * backup manifest.
143 : *
144 : * Ignore the pg_wal directory, because those files are not included in
145 : * the backup manifest either, since they are fetched separately from the
146 : * backup itself, and verified via a separate mechanism.
147 : *
148 : * Ignore postgresql.auto.conf, recovery.signal, and standby.signal,
149 : * because we expect that those files may sometimes be created or changed
150 : * as part of the backup process. For example, pg_basebackup -R will
151 : * modify postgresql.auto.conf and create standby.signal.
152 : */
153 194 : simple_string_list_append(&context.ignore_list, "backup_manifest");
154 194 : simple_string_list_append(&context.ignore_list, "pg_wal");
155 194 : simple_string_list_append(&context.ignore_list, "postgresql.auto.conf");
156 194 : simple_string_list_append(&context.ignore_list, "recovery.signal");
157 194 : simple_string_list_append(&context.ignore_list, "standby.signal");
158 :
159 280 : while ((c = getopt_long(argc, argv, "ei:m:nPqsw:", long_options, NULL)) != -1)
160 : {
161 88 : switch (c)
162 : {
163 32 : case 'e':
164 32 : context.exit_on_error = true;
165 32 : break;
166 8 : case 'i':
167 : {
168 8 : char *arg = pstrdup(optarg);
169 :
170 8 : canonicalize_path(arg);
171 8 : simple_string_list_append(&context.ignore_list, arg);
172 8 : break;
173 : }
174 16 : case 'm':
175 16 : manifest_path = pstrdup(optarg);
176 16 : canonicalize_path(manifest_path);
177 16 : break;
178 14 : case 'n':
179 14 : no_parse_wal = true;
180 14 : break;
181 4 : case 'P':
182 4 : show_progress = true;
183 4 : break;
184 6 : case 'q':
185 6 : quiet = true;
186 6 : break;
187 4 : case 's':
188 4 : context.skip_checksums = true;
189 4 : break;
190 2 : case 'w':
191 2 : wal_directory = pstrdup(optarg);
192 2 : canonicalize_path(wal_directory);
193 2 : break;
194 2 : default:
195 : /* getopt_long already emitted a complaint */
196 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
197 2 : exit(1);
198 : }
199 : }
200 :
201 : /* Get backup directory name */
202 192 : if (optind >= argc)
203 : {
204 2 : pg_log_error("no backup directory specified");
205 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
206 2 : exit(1);
207 : }
208 190 : context.backup_directory = pstrdup(argv[optind++]);
209 190 : canonicalize_path(context.backup_directory);
210 :
211 : /* Complain if any arguments remain */
212 190 : if (optind < argc)
213 : {
214 2 : pg_log_error("too many command-line arguments (first is \"%s\")",
215 : argv[optind]);
216 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
217 2 : exit(1);
218 : }
219 :
220 : /* Complain if the specified arguments conflict */
221 188 : if (show_progress && quiet)
222 2 : pg_fatal("cannot specify both %s and %s",
223 : "-P/--progress", "-q/--quiet");
224 :
225 : /* Unless --no-parse-wal was specified, we will need pg_waldump. */
226 186 : if (!no_parse_wal)
227 : {
228 : int ret;
229 :
230 172 : pg_waldump_path = pg_malloc(MAXPGPATH);
231 172 : ret = find_other_exec(argv[0], "pg_waldump",
232 : "pg_waldump (PostgreSQL) " PG_VERSION "\n",
233 : pg_waldump_path);
234 172 : if (ret < 0)
235 : {
236 : char full_path[MAXPGPATH];
237 :
238 0 : if (find_my_exec(argv[0], full_path) < 0)
239 0 : strlcpy(full_path, progname, sizeof(full_path));
240 :
241 0 : if (ret == -1)
242 0 : pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
243 : "pg_waldump", "pg_verifybackup", full_path);
244 : else
245 0 : pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
246 : "pg_waldump", full_path, "pg_verifybackup");
247 : }
248 : }
249 :
250 : /* By default, look for the manifest in the backup directory. */
251 186 : if (manifest_path == NULL)
252 170 : manifest_path = psprintf("%s/backup_manifest",
253 : context.backup_directory);
254 :
255 : /* By default, look for the WAL in the backup directory, too. */
256 186 : if (wal_directory == NULL)
257 184 : wal_directory = psprintf("%s/pg_wal", context.backup_directory);
258 :
259 : /*
260 : * Try to read the manifest. We treat any errors encountered while parsing
261 : * the manifest as fatal; there doesn't seem to be much point in trying to
262 : * verify the backup directory against a corrupted manifest.
263 : */
264 186 : context.manifest = parse_manifest_file(manifest_path);
265 :
266 : /*
267 : * Now scan the files in the backup directory. At this stage, we verify
268 : * that every file on disk is present in the manifest and that the sizes
269 : * match. We also set the "matched" flag on every manifest entry that
270 : * corresponds to a file on disk.
271 : */
272 118 : verify_backup_directory(&context, NULL, context.backup_directory);
273 :
274 : /*
275 : * The "matched" flag should now be set on every entry in the hash table.
276 : * Any entries for which the bit is not set are files mentioned in the
277 : * manifest that don't exist on disk.
278 : */
279 114 : report_extra_backup_files(&context);
280 :
281 : /*
282 : * Now do the expensive work of verifying file checksums, unless we were
283 : * told to skip it.
284 : */
285 112 : if (!context.skip_checksums)
286 108 : verify_backup_checksums(&context);
287 :
288 : /*
289 : * Try to parse the required ranges of WAL records, unless we were told
290 : * not to do so.
291 : */
292 112 : if (!no_parse_wal)
293 98 : parse_required_wal(&context, pg_waldump_path, wal_directory);
294 :
295 : /*
296 : * If everything looks OK, tell the user this, unless we were asked to
297 : * work quietly.
298 : */
299 112 : if (!context.saw_any_error && !quiet)
300 80 : printf(_("backup successfully verified\n"));
301 :
302 112 : return context.saw_any_error ? 1 : 0;
303 : }
304 :
305 : /*
306 : * Parse a manifest file and return a data structure describing the contents.
307 : */
308 : static manifest_data *
309 186 : parse_manifest_file(char *manifest_path)
310 : {
311 : int fd;
312 : struct stat statbuf;
313 : off_t estimate;
314 : uint32 initial_size;
315 : manifest_files_hash *ht;
316 : char *buffer;
317 : int rc;
318 : JsonManifestParseContext context;
319 : manifest_data *result;
320 :
321 186 : int chunk_size = READ_CHUNK_SIZE;
322 :
323 : /* Open the manifest file. */
324 186 : if ((fd = open(manifest_path, O_RDONLY | PG_BINARY, 0)) < 0)
325 6 : report_fatal_error("could not open file \"%s\": %m", manifest_path);
326 :
327 : /* Figure out how big the manifest is. */
328 180 : if (fstat(fd, &statbuf) != 0)
329 0 : report_fatal_error("could not stat file \"%s\": %m", manifest_path);
330 :
331 : /* Guess how large to make the hash table based on the manifest size. */
332 180 : estimate = statbuf.st_size / ESTIMATED_BYTES_PER_MANIFEST_LINE;
333 180 : initial_size = Min(PG_UINT32_MAX, Max(estimate, 256));
334 :
335 : /* Create the hash table. */
336 180 : ht = manifest_files_create(initial_size, NULL);
337 :
338 180 : result = pg_malloc0(sizeof(manifest_data));
339 180 : result->files = ht;
340 180 : context.private_data = result;
341 180 : context.version_cb = verifybackup_version_cb;
342 180 : context.system_identifier_cb = verifybackup_system_identifier;
343 180 : context.per_file_cb = verifybackup_per_file_cb;
344 180 : context.per_wal_range_cb = verifybackup_per_wal_range_cb;
345 180 : context.error_cb = report_manifest_error;
346 :
347 : /*
348 : * Parse the file, in chunks if necessary.
349 : */
350 180 : if (statbuf.st_size <= chunk_size)
351 : {
352 64 : buffer = pg_malloc(statbuf.st_size);
353 64 : rc = read(fd, buffer, statbuf.st_size);
354 64 : if (rc != statbuf.st_size)
355 : {
356 0 : if (rc < 0)
357 0 : pg_fatal("could not read file \"%s\": %m", manifest_path);
358 : else
359 0 : pg_fatal("could not read file \"%s\": read %d of %lld",
360 : manifest_path, rc, (long long int) statbuf.st_size);
361 : }
362 :
363 : /* Close the manifest file. */
364 64 : close(fd);
365 :
366 : /* Parse the manifest. */
367 64 : json_parse_manifest(&context, buffer, statbuf.st_size);
368 : }
369 : else
370 : {
371 116 : int bytes_left = statbuf.st_size;
372 : JsonManifestParseIncrementalState *inc_state;
373 :
374 116 : inc_state = json_parse_manifest_incremental_init(&context);
375 :
376 116 : buffer = pg_malloc(chunk_size + 1);
377 :
378 346 : while (bytes_left > 0)
379 : {
380 232 : int bytes_to_read = chunk_size;
381 :
382 : /*
383 : * Make sure that the last chunk is sufficiently large. (i.e. at
384 : * least half the chunk size) so that it will contain fully the
385 : * piece at the end with the checksum.
386 : */
387 232 : if (bytes_left < chunk_size)
388 116 : bytes_to_read = bytes_left;
389 116 : else if (bytes_left < 2 * chunk_size)
390 116 : bytes_to_read = bytes_left / 2;
391 232 : rc = read(fd, buffer, bytes_to_read);
392 232 : if (rc != bytes_to_read)
393 : {
394 0 : if (rc < 0)
395 0 : pg_fatal("could not read file \"%s\": %m", manifest_path);
396 : else
397 0 : pg_fatal("could not read file \"%s\": read %lld of %lld",
398 : manifest_path,
399 : (long long int) (statbuf.st_size + rc - bytes_left),
400 : (long long int) statbuf.st_size);
401 : }
402 232 : bytes_left -= rc;
403 232 : json_parse_manifest_incremental_chunk(inc_state, buffer, rc,
404 : bytes_left == 0);
405 : }
406 :
407 : /* Release the incremental state memory */
408 114 : json_parse_manifest_incremental_shutdown(inc_state);
409 :
410 114 : close(fd);
411 : }
412 :
413 : /* Done with the buffer. */
414 118 : pfree(buffer);
415 :
416 118 : return result;
417 : }
418 :
419 : /*
420 : * Report an error while parsing the manifest.
421 : *
422 : * We consider all such errors to be fatal errors. The manifest parser
423 : * expects this function not to return.
424 : */
425 : static void
426 60 : report_manifest_error(JsonManifestParseContext *context, const char *fmt,...)
427 : {
428 : va_list ap;
429 :
430 60 : va_start(ap, fmt);
431 60 : pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, gettext(fmt), ap);
432 60 : va_end(ap);
433 :
434 60 : exit(1);
435 : }
436 :
437 : /*
438 : * Record details extracted from the backup manifest.
439 : */
440 : static void
441 168 : verifybackup_version_cb(JsonManifestParseContext *context,
442 : int manifest_version)
443 : {
444 168 : manifest_data *manifest = context->private_data;
445 :
446 : /* Validation will be at the later stage */
447 168 : manifest->version = manifest_version;
448 168 : }
449 :
450 : /*
451 : * Record details extracted from the backup manifest.
452 : */
453 : static void
454 120 : verifybackup_system_identifier(JsonManifestParseContext *context,
455 : uint64 manifest_system_identifier)
456 : {
457 120 : manifest_data *manifest = context->private_data;
458 :
459 : /* Validation will be at the later stage */
460 120 : manifest->system_identifier = manifest_system_identifier;
461 120 : }
462 :
463 : /*
464 : * Record details extracted from the backup manifest for one file.
465 : */
466 : static void
467 116070 : verifybackup_per_file_cb(JsonManifestParseContext *context,
468 : const char *pathname, size_t size,
469 : pg_checksum_type checksum_type,
470 : int checksum_length, uint8 *checksum_payload)
471 : {
472 116070 : manifest_data *manifest = context->private_data;
473 116070 : manifest_files_hash *ht = manifest->files;
474 : manifest_file *m;
475 : bool found;
476 :
477 : /* Make a new entry in the hash table for this file. */
478 116070 : m = manifest_files_insert(ht, pathname, &found);
479 116070 : if (found)
480 2 : report_fatal_error("duplicate path name in backup manifest: \"%s\"",
481 : pathname);
482 :
483 : /* Initialize the entry. */
484 116068 : m->size = size;
485 116068 : m->checksum_type = checksum_type;
486 116068 : m->checksum_length = checksum_length;
487 116068 : m->checksum_payload = checksum_payload;
488 116068 : m->matched = false;
489 116068 : m->bad = false;
490 116068 : }
491 :
492 : /*
493 : * Record details extracted from the backup manifest for one WAL range.
494 : */
495 : static void
496 122 : verifybackup_per_wal_range_cb(JsonManifestParseContext *context,
497 : TimeLineID tli,
498 : XLogRecPtr start_lsn, XLogRecPtr end_lsn)
499 : {
500 122 : manifest_data *manifest = context->private_data;
501 : manifest_wal_range *range;
502 :
503 : /* Allocate and initialize a struct describing this WAL range. */
504 122 : range = palloc(sizeof(manifest_wal_range));
505 122 : range->tli = tli;
506 122 : range->start_lsn = start_lsn;
507 122 : range->end_lsn = end_lsn;
508 122 : range->prev = manifest->last_wal_range;
509 122 : range->next = NULL;
510 :
511 : /* Add it to the end of the list. */
512 122 : if (manifest->first_wal_range == NULL)
513 122 : manifest->first_wal_range = range;
514 : else
515 0 : manifest->last_wal_range->next = range;
516 122 : manifest->last_wal_range = range;
517 122 : }
518 :
519 : /*
520 : * Verify one directory.
521 : *
522 : * 'relpath' is NULL if we are to verify the top-level backup directory,
523 : * and otherwise the relative path to the directory that is to be verified.
524 : *
525 : * 'fullpath' is the backup directory with 'relpath' appended; i.e. the actual
526 : * filesystem path at which it can be found.
527 : */
528 : static void
529 2876 : verify_backup_directory(verifier_context *context, char *relpath,
530 : char *fullpath)
531 : {
532 : DIR *dir;
533 : struct dirent *dirent;
534 :
535 2876 : dir = opendir(fullpath);
536 2876 : if (dir == NULL)
537 : {
538 : /*
539 : * If even the toplevel backup directory cannot be found, treat this
540 : * as a fatal error.
541 : */
542 4 : if (relpath == NULL)
543 2 : report_fatal_error("could not open directory \"%s\": %m", fullpath);
544 :
545 : /*
546 : * Otherwise, treat this as a non-fatal error, but ignore any further
547 : * errors related to this path and anything beneath it.
548 : */
549 2 : report_backup_error(context,
550 : "could not open directory \"%s\": %m", fullpath);
551 2 : simple_string_list_append(&context->ignore_list, relpath);
552 :
553 2 : return;
554 : }
555 :
556 120102 : while (errno = 0, (dirent = readdir(dir)) != NULL)
557 : {
558 117234 : char *filename = dirent->d_name;
559 117234 : char *newfullpath = psprintf("%s/%s", fullpath, filename);
560 : char *newrelpath;
561 :
562 : /* Skip "." and ".." */
563 117234 : if (filename[0] == '.' && (filename[1] == '\0'
564 2868 : || strcmp(filename, "..") == 0))
565 5738 : continue;
566 :
567 111496 : if (relpath == NULL)
568 2748 : newrelpath = pstrdup(filename);
569 : else
570 108748 : newrelpath = psprintf("%s/%s", relpath, filename);
571 :
572 111496 : if (!should_ignore_relpath(context, newrelpath))
573 111166 : verify_backup_file(context, newrelpath, newfullpath);
574 :
575 111492 : pfree(newfullpath);
576 111492 : pfree(newrelpath);
577 : }
578 :
579 2868 : if (closedir(dir))
580 : {
581 0 : report_backup_error(context,
582 : "could not close directory \"%s\": %m", fullpath);
583 0 : return;
584 : }
585 : }
586 :
587 : /*
588 : * Verify one file (which might actually be a directory or a symlink).
589 : *
590 : * The arguments to this function have the same meaning as the arguments to
591 : * verify_backup_directory.
592 : */
593 : static void
594 111166 : verify_backup_file(verifier_context *context, char *relpath, char *fullpath)
595 : {
596 : struct stat sb;
597 : manifest_file *m;
598 :
599 111166 : if (stat(fullpath, &sb) != 0)
600 : {
601 6 : report_backup_error(context,
602 : "could not stat file or directory \"%s\": %m",
603 : relpath);
604 :
605 : /*
606 : * Suppress further errors related to this path name and, if it's a
607 : * directory, anything underneath it.
608 : */
609 6 : simple_string_list_append(&context->ignore_list, relpath);
610 :
611 2766 : return;
612 : }
613 :
614 : /* If it's a directory, just recurse. */
615 111160 : if (S_ISDIR(sb.st_mode))
616 : {
617 2758 : verify_backup_directory(context, relpath, fullpath);
618 2756 : return;
619 : }
620 :
621 : /* If it's not a directory, it should be a plain file. */
622 108402 : if (!S_ISREG(sb.st_mode))
623 : {
624 0 : report_backup_error(context,
625 : "\"%s\" is not a file or directory",
626 : relpath);
627 0 : return;
628 : }
629 :
630 : /* Check whether there's an entry in the manifest hash. */
631 108402 : m = manifest_files_lookup(context->manifest->files, relpath);
632 108402 : if (m == NULL)
633 : {
634 4 : report_backup_error(context,
635 : "\"%s\" is present on disk but not in the manifest",
636 : relpath);
637 4 : return;
638 : }
639 :
640 : /* Flag this entry as having been encountered in the filesystem. */
641 108398 : m->matched = true;
642 :
643 : /* Check that the size matches. */
644 108398 : if (m->size != sb.st_size)
645 : {
646 4 : report_backup_error(context,
647 : "\"%s\" has size %lld on disk but size %zu in the manifest",
648 4 : relpath, (long long int) sb.st_size, m->size);
649 4 : m->bad = true;
650 : }
651 :
652 : /*
653 : * Validate the manifest system identifier, not available in manifest
654 : * version 1.
655 : */
656 108398 : if (context->manifest->version != 1 &&
657 108398 : strcmp(relpath, "global/pg_control") == 0)
658 116 : verify_control_file(fullpath, context->manifest->system_identifier);
659 :
660 : /* Update statistics for progress report, if necessary */
661 108396 : if (show_progress && !context->skip_checksums &&
662 1928 : should_verify_checksum(m))
663 1928 : total_size += m->size;
664 :
665 : /*
666 : * We don't verify checksums at this stage. We first finish verifying that
667 : * we have the expected set of files with the expected sizes, and only
668 : * afterwards verify the checksums. That's because computing checksums may
669 : * take a while, and we'd like to report more obvious problems quickly.
670 : */
671 : }
672 :
673 : /*
674 : * Sanity check control file and validate system identifier against manifest
675 : * system identifier.
676 : */
677 : static void
678 116 : verify_control_file(const char *controlpath, uint64 manifest_system_identifier)
679 : {
680 : ControlFileData *control_file;
681 : bool crc_ok;
682 :
683 116 : pg_log_debug("reading \"%s\"", controlpath);
684 116 : control_file = get_controlfile_by_exact_path(controlpath, &crc_ok);
685 :
686 : /* Control file contents not meaningful if CRC is bad. */
687 116 : if (!crc_ok)
688 0 : report_fatal_error("%s: CRC is incorrect", controlpath);
689 :
690 : /* Can't interpret control file if not current version. */
691 116 : if (control_file->pg_control_version != PG_CONTROL_VERSION)
692 0 : report_fatal_error("%s: unexpected control file version",
693 : controlpath);
694 :
695 : /* System identifiers should match. */
696 116 : if (manifest_system_identifier != control_file->system_identifier)
697 2 : report_fatal_error("%s: manifest system identifier is %llu, but control file has %llu",
698 : controlpath,
699 : (unsigned long long) manifest_system_identifier,
700 2 : (unsigned long long) control_file->system_identifier);
701 :
702 : /* Release memory. */
703 114 : pfree(control_file);
704 114 : }
705 :
706 : /*
707 : * Scan the hash table for entries where the 'matched' flag is not set; report
708 : * that such files are present in the manifest but not on disk.
709 : */
710 : static void
711 114 : report_extra_backup_files(verifier_context *context)
712 : {
713 114 : manifest_data *manifest = context->manifest;
714 : manifest_files_iterator it;
715 : manifest_file *m;
716 :
717 114 : manifest_files_start_iterate(manifest->files, &it);
718 109200 : while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
719 109088 : if (!m->matched && !should_ignore_relpath(context, m->pathname))
720 10 : report_backup_error(context,
721 : "\"%s\" is present in the manifest but not on disk",
722 : m->pathname);
723 112 : }
724 :
725 : /*
726 : * Verify checksums for hash table entries that are otherwise unproblematic.
727 : * If we've already reported some problem related to a hash table entry, or
728 : * if it has no checksum, just skip it.
729 : */
730 : static void
731 108 : verify_backup_checksums(verifier_context *context)
732 : {
733 108 : manifest_data *manifest = context->manifest;
734 : manifest_files_iterator it;
735 : manifest_file *m;
736 : uint8 *buffer;
737 :
738 108 : progress_report(false);
739 :
740 108 : buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
741 :
742 108 : manifest_files_start_iterate(manifest->files, &it);
743 104578 : while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
744 : {
745 104470 : if (should_verify_checksum(m) &&
746 98670 : !should_ignore_relpath(context, m->pathname))
747 : {
748 : char *fullpath;
749 :
750 : /* Compute the full pathname to the target file. */
751 98670 : fullpath = psprintf("%s/%s", context->backup_directory,
752 : m->pathname);
753 :
754 : /* Do the actual checksum verification. */
755 98670 : verify_file_checksum(context, m, fullpath, buffer);
756 :
757 : /* Avoid leaking memory. */
758 98670 : pfree(fullpath);
759 : }
760 : }
761 :
762 108 : pfree(buffer);
763 :
764 108 : progress_report(true);
765 108 : }
766 :
767 : /*
768 : * Verify the checksum of a single file.
769 : */
770 : static void
771 98670 : verify_file_checksum(verifier_context *context, manifest_file *m,
772 : char *fullpath, uint8 *buffer)
773 : {
774 : pg_checksum_context checksum_ctx;
775 98670 : const char *relpath = m->pathname;
776 : int fd;
777 : int rc;
778 98670 : size_t bytes_read = 0;
779 : uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
780 : int checksumlen;
781 :
782 : /* Open the target file. */
783 98670 : if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
784 : {
785 2 : report_backup_error(context, "could not open file \"%s\": %m",
786 : relpath);
787 2 : return;
788 : }
789 :
790 : /* Initialize checksum context. */
791 98668 : if (pg_checksum_init(&checksum_ctx, m->checksum_type) < 0)
792 : {
793 0 : report_backup_error(context, "could not initialize checksum of file \"%s\"",
794 : relpath);
795 0 : close(fd);
796 0 : return;
797 : }
798 :
799 : /* Read the file chunk by chunk, updating the checksum as we go. */
800 185108 : while ((rc = read(fd, buffer, READ_CHUNK_SIZE)) > 0)
801 : {
802 86440 : bytes_read += rc;
803 86440 : if (pg_checksum_update(&checksum_ctx, buffer, rc) < 0)
804 : {
805 0 : report_backup_error(context, "could not update checksum of file \"%s\"",
806 : relpath);
807 0 : close(fd);
808 0 : return;
809 : }
810 :
811 : /* Report progress */
812 86440 : done_size += rc;
813 86440 : progress_report(false);
814 : }
815 98668 : if (rc < 0)
816 0 : report_backup_error(context, "could not read file \"%s\": %m",
817 : relpath);
818 :
819 : /* Close the file. */
820 98668 : if (close(fd) != 0)
821 : {
822 0 : report_backup_error(context, "could not close file \"%s\": %m",
823 : relpath);
824 0 : return;
825 : }
826 :
827 : /* If we didn't manage to read the whole file, bail out now. */
828 98668 : if (rc < 0)
829 0 : return;
830 :
831 : /*
832 : * Double-check that we read the expected number of bytes from the file.
833 : * Normally, a file size mismatch would be caught in verify_backup_file
834 : * and this check would never be reached, but this provides additional
835 : * safety and clarity in the event of concurrent modifications or
836 : * filesystem misbehavior.
837 : */
838 98668 : if (bytes_read != m->size)
839 : {
840 0 : report_backup_error(context,
841 : "file \"%s\" should contain %zu bytes, but read %zu bytes",
842 : relpath, m->size, bytes_read);
843 0 : return;
844 : }
845 :
846 : /* Get the final checksum. */
847 98668 : checksumlen = pg_checksum_final(&checksum_ctx, checksumbuf);
848 98668 : if (checksumlen < 0)
849 : {
850 0 : report_backup_error(context,
851 : "could not finalize checksum of file \"%s\"",
852 : relpath);
853 0 : return;
854 : }
855 :
856 : /* And check it against the manifest. */
857 98668 : if (checksumlen != m->checksum_length)
858 0 : report_backup_error(context,
859 : "file \"%s\" has checksum of length %d, but expected %d",
860 : relpath, m->checksum_length, checksumlen);
861 98668 : else if (memcmp(checksumbuf, m->checksum_payload, checksumlen) != 0)
862 6 : report_backup_error(context,
863 : "checksum mismatch for file \"%s\"",
864 : relpath);
865 : }
866 :
867 : /*
868 : * Attempt to parse the WAL files required to restore from backup using
869 : * pg_waldump.
870 : */
871 : static void
872 98 : parse_required_wal(verifier_context *context, char *pg_waldump_path,
873 : char *wal_directory)
874 : {
875 98 : manifest_data *manifest = context->manifest;
876 98 : manifest_wal_range *this_wal_range = manifest->first_wal_range;
877 :
878 196 : while (this_wal_range != NULL)
879 : {
880 : char *pg_waldump_cmd;
881 :
882 98 : pg_waldump_cmd = psprintf("\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%X --end=%X/%X\n",
883 : pg_waldump_path, wal_directory, this_wal_range->tli,
884 98 : LSN_FORMAT_ARGS(this_wal_range->start_lsn),
885 98 : LSN_FORMAT_ARGS(this_wal_range->end_lsn));
886 98 : fflush(NULL);
887 98 : if (system(pg_waldump_cmd) != 0)
888 4 : report_backup_error(context,
889 : "WAL parsing failed for timeline %u",
890 : this_wal_range->tli);
891 :
892 98 : this_wal_range = this_wal_range->next;
893 : }
894 98 : }
895 :
896 : /*
897 : * Report a problem with the backup.
898 : *
899 : * Update the context to indicate that we saw an error, and exit if the
900 : * context says we should.
901 : */
902 : void
903 38 : report_backup_error(verifier_context *context, const char *pg_restrict fmt,...)
904 : {
905 : va_list ap;
906 :
907 38 : va_start(ap, fmt);
908 38 : pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, gettext(fmt), ap);
909 38 : va_end(ap);
910 :
911 38 : context->saw_any_error = true;
912 38 : if (context->exit_on_error)
913 2 : exit(1);
914 36 : }
915 :
916 : /*
917 : * Report a fatal error and exit
918 : */
919 : void
920 12 : report_fatal_error(const char *pg_restrict fmt,...)
921 : {
922 : va_list ap;
923 :
924 12 : va_start(ap, fmt);
925 12 : pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, gettext(fmt), ap);
926 12 : va_end(ap);
927 :
928 12 : exit(1);
929 : }
930 :
931 : /*
932 : * Is the specified relative path, or some prefix of it, listed in the set
933 : * of paths to ignore?
934 : *
935 : * Note that by "prefix" we mean a parent directory; for this purpose,
936 : * "aa/bb" is not a prefix of "aa/bbb", but it is a prefix of "aa/bb/cc".
937 : */
938 : bool
939 212108 : should_ignore_relpath(verifier_context *context, const char *relpath)
940 : {
941 : SimpleStringListCell *cell;
942 :
943 1292550 : for (cell = context->ignore_list.head; cell != NULL; cell = cell->next)
944 : {
945 1082704 : const char *r = relpath;
946 1082704 : char *v = cell->val;
947 :
948 1515794 : while (*v != '\0' && *r == *v)
949 433090 : ++r, ++v;
950 :
951 1082704 : if (*v == '\0' && (*r == '\0' || *r == '/'))
952 2262 : return true;
953 : }
954 :
955 209846 : return false;
956 : }
957 :
958 : /*
959 : * Print a progress report based on the global variables.
960 : *
961 : * Progress report is written at maximum once per second, unless the finished
962 : * parameter is set to true.
963 : *
964 : * If finished is set to true, this is the last progress report. The cursor
965 : * is moved to the next line.
966 : */
967 : static void
968 86656 : progress_report(bool finished)
969 : {
970 : static pg_time_t last_progress_report = 0;
971 : pg_time_t now;
972 86656 : int percent_size = 0;
973 : char totalsize_str[32];
974 : char donesize_str[32];
975 :
976 86656 : if (!show_progress)
977 86652 : return;
978 :
979 1698 : now = time(NULL);
980 1698 : if (now == last_progress_report && !finished)
981 1694 : return; /* Max once per second */
982 :
983 4 : last_progress_report = now;
984 4 : percent_size = total_size ? (int) ((done_size * 100 / total_size)) : 0;
985 :
986 4 : snprintf(totalsize_str, sizeof(totalsize_str), UINT64_FORMAT,
987 : total_size / 1024);
988 4 : snprintf(donesize_str, sizeof(donesize_str), UINT64_FORMAT,
989 : done_size / 1024);
990 :
991 4 : fprintf(stderr,
992 4 : _("%*s/%s kB (%d%%) verified"),
993 4 : (int) strlen(totalsize_str),
994 : donesize_str, totalsize_str, percent_size);
995 :
996 : /*
997 : * Stay on the same line if reporting to a terminal and we're not done
998 : * yet.
999 : */
1000 4 : fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
1001 : }
1002 :
1003 : /*
1004 : * Print out usage information and exit.
1005 : */
1006 : static void
1007 2 : usage(void)
1008 : {
1009 2 : printf(_("%s verifies a backup against the backup manifest.\n\n"), progname);
1010 2 : printf(_("Usage:\n %s [OPTION]... BACKUPDIR\n\n"), progname);
1011 2 : printf(_("Options:\n"));
1012 2 : printf(_(" -e, --exit-on-error exit immediately on error\n"));
1013 2 : printf(_(" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
1014 2 : printf(_(" -m, --manifest-path=PATH use specified path for manifest\n"));
1015 2 : printf(_(" -n, --no-parse-wal do not try to parse WAL files\n"));
1016 2 : printf(_(" -P, --progress show progress information\n"));
1017 2 : printf(_(" -q, --quiet do not print any output, except for errors\n"));
1018 2 : printf(_(" -s, --skip-checksums skip checksum verification\n"));
1019 2 : printf(_(" -w, --wal-directory=PATH use specified path for WAL files\n"));
1020 2 : printf(_(" -V, --version output version information, then exit\n"));
1021 2 : printf(_(" -?, --help show this help, then exit\n"));
1022 2 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1023 2 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1024 2 : }
|