PostgreSQL Source Code  git master
pg_verifybackup.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include "common/logging.h"
#include "common/parse_manifest.h"
#include "fe_utils/simple_list.h"
#include "getopt_long.h"
#include "pg_verifybackup.h"
#include "pgtime.h"
Include dependency graph for pg_verifybackup.c:

Go to the source code of this file.

Macros

#define ESTIMATED_BYTES_PER_MANIFEST_LINE   100
 
#define READ_CHUNK_SIZE   (128 * 1024)
 

Functions

static manifest_dataparse_manifest_file (char *manifest_path)
 
static void verifybackup_version_cb (JsonManifestParseContext *context, int manifest_version)
 
static void verifybackup_system_identifier (JsonManifestParseContext *context, uint64 manifest_system_identifier)
 
static void verifybackup_per_file_cb (JsonManifestParseContext *context, const char *pathname, size_t size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
 
static void verifybackup_per_wal_range_cb (JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
 
static void report_manifest_error (JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
 
static void pg_attribute_noreturn ()
 
static void verify_backup_directory (verifier_context *context, char *relpath, char *fullpath)
 
static void verify_backup_file (verifier_context *context, char *relpath, char *fullpath)
 
static void verify_control_file (const char *controlpath, uint64 manifest_system_identifier)
 
static void report_extra_backup_files (verifier_context *context)
 
static void verify_backup_checksums (verifier_context *context)
 
static void verify_file_checksum (verifier_context *context, manifest_file *m, char *fullpath, uint8 *buffer)
 
static void parse_required_wal (verifier_context *context, char *pg_waldump_path, char *wal_directory)
 
static void progress_report (bool finished)
 
static void usage (void)
 
int main (int argc, char **argv)
 
void report_backup_error (verifier_context *context, const char *pg_restrict fmt,...)
 
void report_fatal_error (const char *pg_restrict fmt,...)
 
bool should_ignore_relpath (verifier_context *context, const char *relpath)
 

Variables

static const char * progname
 
static bool show_progress = false
 
static uint64 total_size = 0
 
static uint64 done_size = 0
 

Macro Definition Documentation

◆ ESTIMATED_BYTES_PER_MANIFEST_LINE

#define ESTIMATED_BYTES_PER_MANIFEST_LINE   100

Definition at line 40 of file pg_verifybackup.c.

◆ READ_CHUNK_SIZE

#define READ_CHUNK_SIZE   (128 * 1024)

Definition at line 45 of file pg_verifybackup.c.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 96 of file pg_verifybackup.c.

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;
112  char *manifest_path = NULL;
113  bool no_parse_wal = false;
114  bool quiet = false;
115  char *wal_directory = NULL;
116  char *pg_waldump_path = NULL;
117 
118  pg_logging_init(argv[0]);
119  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_verifybackup"));
120  progname = get_progname(argv[0]);
121 
122  memset(&context, 0, sizeof(context));
123 
124  if (argc > 1)
125  {
126  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
127  {
128  usage();
129  exit(0);
130  }
131  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
132  {
133  puts("pg_verifybackup (PostgreSQL) " PG_VERSION);
134  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  simple_string_list_append(&context.ignore_list, "backup_manifest");
154  simple_string_list_append(&context.ignore_list, "pg_wal");
155  simple_string_list_append(&context.ignore_list, "postgresql.auto.conf");
156  simple_string_list_append(&context.ignore_list, "recovery.signal");
157  simple_string_list_append(&context.ignore_list, "standby.signal");
158 
159  while ((c = getopt_long(argc, argv, "ei:m:nPqsw:", long_options, NULL)) != -1)
160  {
161  switch (c)
162  {
163  case 'e':
164  context.exit_on_error = true;
165  break;
166  case 'i':
167  {
168  char *arg = pstrdup(optarg);
169 
171  simple_string_list_append(&context.ignore_list, arg);
172  break;
173  }
174  case 'm':
175  manifest_path = pstrdup(optarg);
176  canonicalize_path(manifest_path);
177  break;
178  case 'n':
179  no_parse_wal = true;
180  break;
181  case 'P':
182  show_progress = true;
183  break;
184  case 'q':
185  quiet = true;
186  break;
187  case 's':
188  context.skip_checksums = true;
189  break;
190  case 'w':
191  wal_directory = pstrdup(optarg);
192  canonicalize_path(wal_directory);
193  break;
194  default:
195  /* getopt_long already emitted a complaint */
196  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
197  exit(1);
198  }
199  }
200 
201  /* Get backup directory name */
202  if (optind >= argc)
203  {
204  pg_log_error("no backup directory specified");
205  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
206  exit(1);
207  }
208  context.backup_directory = pstrdup(argv[optind++]);
209  canonicalize_path(context.backup_directory);
210 
211  /* Complain if any arguments remain */
212  if (optind < argc)
213  {
214  pg_log_error("too many command-line arguments (first is \"%s\")",
215  argv[optind]);
216  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
217  exit(1);
218  }
219 
220  /* Complain if the specified arguments conflict */
221  if (show_progress && quiet)
222  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  if (!no_parse_wal)
227  {
228  int ret;
229 
230  pg_waldump_path = pg_malloc(MAXPGPATH);
231  ret = find_other_exec(argv[0], "pg_waldump",
232  "pg_waldump (PostgreSQL) " PG_VERSION "\n",
233  pg_waldump_path);
234  if (ret < 0)
235  {
236  char full_path[MAXPGPATH];
237 
238  if (find_my_exec(argv[0], full_path) < 0)
239  strlcpy(full_path, progname, sizeof(full_path));
240 
241  if (ret == -1)
242  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  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  if (manifest_path == NULL)
252  manifest_path = psprintf("%s/backup_manifest",
253  context.backup_directory);
254 
255  /* By default, look for the WAL in the backup directory, too. */
256  if (wal_directory == NULL)
257  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  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  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  */
280 
281  /*
282  * Now do the expensive work of verifying file checksums, unless we were
283  * told to skip it.
284  */
285  if (!context.skip_checksums)
287 
288  /*
289  * Try to parse the required ranges of WAL records, unless we were told
290  * not to do so.
291  */
292  if (!no_parse_wal)
293  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  if (!context.saw_any_error && !quiet)
300  printf(_("backup successfully verified\n"));
301 
302  return context.saw_any_error ? 1 : 0;
303 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1214
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:310
#define _(x)
Definition: elog.c:90
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * arg
#define pg_fatal(...)
#define MAXPGPATH
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
static void verify_backup_checksums(verifier_context *context)
static manifest_data * parse_manifest_file(char *manifest_path)
static void report_extra_backup_files(verifier_context *context)
static bool show_progress
static void parse_required_wal(verifier_context *context, char *pg_waldump_path, char *wal_directory)
static const char * progname
static void verify_backup_directory(verifier_context *context, char *relpath, char *fullpath)
static void usage(void)
void canonicalize_path(char *path)
Definition: path.c:265
const char * get_progname(const char *argv0)
Definition: path.c:575
#define printf(...)
Definition: port.h:244
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * c
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
tree context
Definition: radixtree.h:1835
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63

References _, arg, canonicalize_path(), context, exit(), find_my_exec(), find_other_exec(), get_progname(), getopt_long(), MAXPGPATH, no_argument, optarg, optind, parse_manifest_file(), parse_required_wal(), pg_fatal, pg_log_error, pg_log_error_hint, pg_logging_init(), pg_malloc(), PG_TEXTDOMAIN, printf, progname, psprintf(), pstrdup(), report_extra_backup_files(), required_argument, set_pglocale_pgservice(), show_progress, simple_string_list_append(), strlcpy(), usage(), verify_backup_checksums(), and verify_backup_directory().

◆ parse_manifest_file()

static manifest_data * parse_manifest_file ( char *  manifest_path)
static

Definition at line 309 of file pg_verifybackup.c.

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;
319  manifest_data *result;
320 
321  int chunk_size = READ_CHUNK_SIZE;
322 
323  /* Open the manifest file. */
324  if ((fd = open(manifest_path, O_RDONLY | PG_BINARY, 0)) < 0)
325  report_fatal_error("could not open file \"%s\": %m", manifest_path);
326 
327  /* Figure out how big the manifest is. */
328  if (fstat(fd, &statbuf) != 0)
329  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  estimate = statbuf.st_size / ESTIMATED_BYTES_PER_MANIFEST_LINE;
333  initial_size = Min(PG_UINT32_MAX, Max(estimate, 256));
334 
335  /* Create the hash table. */
336  ht = manifest_files_create(initial_size, NULL);
337 
338  result = pg_malloc0(sizeof(manifest_data));
339  result->files = ht;
340  context.private_data = result;
341  context.version_cb = verifybackup_version_cb;
342  context.system_identifier_cb = verifybackup_system_identifier;
343  context.per_file_cb = verifybackup_per_file_cb;
344  context.per_wal_range_cb = verifybackup_per_wal_range_cb;
345  context.error_cb = report_manifest_error;
346 
347  /*
348  * Parse the file, in chunks if necessary.
349  */
350  if (statbuf.st_size <= chunk_size)
351  {
352  buffer = pg_malloc(statbuf.st_size);
353  rc = read(fd, buffer, statbuf.st_size);
354  if (rc != statbuf.st_size)
355  {
356  if (rc < 0)
357  pg_fatal("could not read file \"%s\": %m", manifest_path);
358  else
359  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  close(fd);
365 
366  /* Parse the manifest. */
367  json_parse_manifest(&context, buffer, statbuf.st_size);
368  }
369  else
370  {
371  int bytes_left = statbuf.st_size;
373 
375 
376  buffer = pg_malloc(chunk_size + 1);
377 
378  while (bytes_left > 0)
379  {
380  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  if (bytes_left < chunk_size)
388  bytes_to_read = bytes_left;
389  else if (bytes_left < 2 * chunk_size)
390  bytes_to_read = bytes_left / 2;
391  rc = read(fd, buffer, bytes_to_read);
392  if (rc != bytes_to_read)
393  {
394  if (rc < 0)
395  pg_fatal("could not read file \"%s\": %m", manifest_path);
396  else
397  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  bytes_left -= rc;
403  json_parse_manifest_incremental_chunk(inc_state, buffer, rc,
404  bytes_left == 0);
405  }
406 
407  /* Release the incremental state memory */
409 
410  close(fd);
411  }
412 
413  /* Done with the buffer. */
414  pfree(buffer);
415 
416  return result;
417 }
unsigned int uint32
Definition: c.h:506
#define Min(x, y)
Definition: c.h:1004
#define PG_UINT32_MAX
Definition: c.h:590
#define Max(x, y)
Definition: c.h:998
#define PG_BINARY
Definition: c.h:1273
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define close(a)
Definition: win32.h:12
#define read(a, b, c)
Definition: win32.h:13
void pfree(void *pointer)
Definition: mcxt.c:1521
void json_parse_manifest(JsonManifestParseContext *context, const char *buffer, size_t size)
JsonManifestParseIncrementalState * json_parse_manifest_incremental_init(JsonManifestParseContext *context)
void json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate)
void json_parse_manifest_incremental_chunk(JsonManifestParseIncrementalState *incstate, const char *chunk, size_t size, bool is_last)
void report_fatal_error(const char *pg_restrict fmt,...)
static void verifybackup_version_cb(JsonManifestParseContext *context, int manifest_version)
static void verifybackup_system_identifier(JsonManifestParseContext *context, uint64 manifest_system_identifier)
static void verifybackup_per_wal_range_cb(JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
#define ESTIMATED_BYTES_PER_MANIFEST_LINE
#define READ_CHUNK_SIZE
static void report_manifest_error(JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
static void verifybackup_per_file_cb(JsonManifestParseContext *context, const char *pathname, size_t size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
static int fd(const char *x, int i)
Definition: preproc-init.c:105
manifest_files_hash * files
Definition: load_manifest.h:59
#define fstat
Definition: win32_port.h:283

References close, context, ESTIMATED_BYTES_PER_MANIFEST_LINE, fd(), manifest_data::files, fstat, json_parse_manifest(), json_parse_manifest_incremental_chunk(), json_parse_manifest_incremental_init(), json_parse_manifest_incremental_shutdown(), Max, Min, pfree(), PG_BINARY, pg_fatal, pg_malloc(), pg_malloc0(), PG_UINT32_MAX, read, READ_CHUNK_SIZE, report_fatal_error(), report_manifest_error(), stat::st_size, verifybackup_per_file_cb(), verifybackup_per_wal_range_cb(), verifybackup_system_identifier(), and verifybackup_version_cb().

Referenced by main().

◆ parse_required_wal()

static void parse_required_wal ( verifier_context context,
char *  pg_waldump_path,
char *  wal_directory 
)
static

Definition at line 872 of file pg_verifybackup.c.

874 {
875  manifest_data *manifest = context->manifest;
876  manifest_wal_range *this_wal_range = manifest->first_wal_range;
877 
878  while (this_wal_range != NULL)
879  {
880  char *pg_waldump_cmd;
881 
882  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  LSN_FORMAT_ARGS(this_wal_range->start_lsn),
885  LSN_FORMAT_ARGS(this_wal_range->end_lsn));
886  fflush(NULL);
887  if (system(pg_waldump_cmd) != 0)
889  "WAL parsing failed for timeline %u",
890  this_wal_range->tli);
891 
892  this_wal_range = this_wal_range->next;
893  }
894 }
static void const char fflush(stdout)
static bool manifest
void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...)
XLogRecPtr end_lsn
Definition: load_manifest.h:48
struct manifest_wal_range * next
Definition: load_manifest.h:49
XLogRecPtr start_lsn
Definition: load_manifest.h:47
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43

References context, manifest_wal_range::end_lsn, fflush(), LSN_FORMAT_ARGS, manifest, manifest_wal_range::next, psprintf(), report_backup_error(), manifest_wal_range::start_lsn, and manifest_wal_range::tli.

Referenced by main().

◆ pg_attribute_noreturn()

static void pg_attribute_noreturn ( )

◆ progress_report()

static void progress_report ( bool  finished)
static

Definition at line 968 of file pg_verifybackup.c.

969 {
970  static pg_time_t last_progress_report = 0;
971  pg_time_t now;
972  int percent_size = 0;
973  char totalsize_str[32];
974  char donesize_str[32];
975 
976  if (!show_progress)
977  return;
978 
979  now = time(NULL);
980  if (now == last_progress_report && !finished)
981  return; /* Max once per second */
982 
984  percent_size = total_size ? (int) ((done_size * 100 / total_size)) : 0;
985 
986  snprintf(totalsize_str, sizeof(totalsize_str), UINT64_FORMAT,
987  total_size / 1024);
988  snprintf(donesize_str, sizeof(donesize_str), UINT64_FORMAT,
989  done_size / 1024);
990 
991  fprintf(stderr,
992  _("%*s/%s kB (%d%%) verified"),
993  (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  fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
1001 }
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1608
#define UINT64_FORMAT
Definition: c.h:549
static pg_time_t last_progress_report
Definition: pg_amcheck.c:146
static uint64 done_size
static uint64 total_size
int64 pg_time_t
Definition: pgtime.h:23
#define snprintf
Definition: port.h:238
#define fprintf
Definition: port.h:242

References _, done_size, fprintf, last_progress_report, now(), show_progress, snprintf, total_size, and UINT64_FORMAT.

Referenced by verify_backup_checksums(), and verify_file_checksum().

◆ report_backup_error()

void report_backup_error ( verifier_context context,
const char *pg_restrict  fmt,
  ... 
)

Definition at line 903 of file pg_verifybackup.c.

904 {
905  va_list ap;
906 
907  va_start(ap, fmt);
909  va_end(ap);
910 
911  context->saw_any_error = true;
912  if (context->exit_on_error)
913  exit(1);
914 }
#define gettext(x)
Definition: c.h:1179
static void const char * fmt
va_end(args)
va_start(args, fmt)
void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:218
@ PG_LOG_PRIMARY
Definition: logging.h:67
@ PG_LOG_ERROR
Definition: logging.h:43

References context, exit(), fmt, gettext, PG_LOG_ERROR, pg_log_generic_v(), PG_LOG_PRIMARY, va_end(), and va_start().

Referenced by parse_required_wal(), report_extra_backup_files(), verify_backup_directory(), verify_backup_file(), and verify_file_checksum().

◆ report_extra_backup_files()

static void report_extra_backup_files ( verifier_context context)
static

Definition at line 711 of file pg_verifybackup.c.

712 {
713  manifest_data *manifest = context->manifest;
714  manifest_files_iterator it;
715  manifest_file *m;
716 
717  manifest_files_start_iterate(manifest->files, &it);
718  while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
721  "\"%s\" is present in the manifest but not on disk",
722  m->pathname);
723 }
bool should_ignore_relpath(verifier_context *context, const char *relpath)
const char * pathname
Definition: load_manifest.h:25

References context, manifest, manifest_file::matched, manifest_file::pathname, report_backup_error(), and should_ignore_relpath().

Referenced by main().

◆ report_fatal_error()

void report_fatal_error ( const char *pg_restrict  fmt,
  ... 
)

Definition at line 920 of file pg_verifybackup.c.

921 {
922  va_list ap;
923 
924  va_start(ap, fmt);
926  va_end(ap);
927 
928  exit(1);
929 }

References exit(), fmt, gettext, PG_LOG_ERROR, pg_log_generic_v(), PG_LOG_PRIMARY, va_end(), and va_start().

Referenced by parse_manifest_file(), verify_backup_directory(), verify_control_file(), and verifybackup_per_file_cb().

◆ report_manifest_error()

static void report_manifest_error ( JsonManifestParseContext context,
const char *  fmt,
  ... 
)
static

Definition at line 426 of file pg_verifybackup.c.

427 {
428  va_list ap;
429 
430  va_start(ap, fmt);
432  va_end(ap);
433 
434  exit(1);
435 }

References exit(), fmt, gettext, PG_LOG_ERROR, pg_log_generic_v(), PG_LOG_PRIMARY, va_end(), and va_start().

Referenced by parse_manifest_file().

◆ should_ignore_relpath()

bool should_ignore_relpath ( verifier_context context,
const char *  relpath 
)

Definition at line 939 of file pg_verifybackup.c.

940 {
941  SimpleStringListCell *cell;
942 
943  for (cell = context->ignore_list.head; cell != NULL; cell = cell->next)
944  {
945  const char *r = relpath;
946  char *v = cell->val;
947 
948  while (*v != '\0' && *r == *v)
949  ++r, ++v;
950 
951  if (*v == '\0' && (*r == '\0' || *r == '/'))
952  return true;
953  }
954 
955  return false;
956 }
#define relpath(rlocator, forknum)
Definition: relpath.h:102
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
struct SimpleStringListCell * next
Definition: simple_list.h:34

References context, SimpleStringListCell::next, relpath, and SimpleStringListCell::val.

Referenced by report_extra_backup_files(), verify_backup_checksums(), and verify_backup_directory().

◆ usage()

static void usage ( void  )
static

Definition at line 1007 of file pg_verifybackup.c.

1008 {
1009  printf(_("%s verifies a backup against the backup manifest.\n\n"), progname);
1010  printf(_("Usage:\n %s [OPTION]... BACKUPDIR\n\n"), progname);
1011  printf(_("Options:\n"));
1012  printf(_(" -e, --exit-on-error exit immediately on error\n"));
1013  printf(_(" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
1014  printf(_(" -m, --manifest-path=PATH use specified path for manifest\n"));
1015  printf(_(" -n, --no-parse-wal do not try to parse WAL files\n"));
1016  printf(_(" -P, --progress show progress information\n"));
1017  printf(_(" -q, --quiet do not print any output, except for errors\n"));
1018  printf(_(" -s, --skip-checksums skip checksum verification\n"));
1019  printf(_(" -w, --wal-directory=PATH use specified path for WAL files\n"));
1020  printf(_(" -V, --version output version information, then exit\n"));
1021  printf(_(" -?, --help show this help, then exit\n"));
1022  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1023  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1024 }

References _, printf, and progname.

Referenced by main().

◆ verify_backup_checksums()

static void verify_backup_checksums ( verifier_context context)
static

Definition at line 731 of file pg_verifybackup.c.

732 {
733  manifest_data *manifest = context->manifest;
734  manifest_files_iterator it;
735  manifest_file *m;
736  uint8 *buffer;
737 
738  progress_report(false);
739 
740  buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
741 
742  manifest_files_start_iterate(manifest->files, &it);
743  while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
744  {
745  if (should_verify_checksum(m) &&
747  {
748  char *fullpath;
749 
750  /* Compute the full pathname to the target file. */
751  fullpath = psprintf("%s/%s", context->backup_directory,
752  m->pathname);
753 
754  /* Do the actual checksum verification. */
755  verify_file_checksum(context, m, fullpath, buffer);
756 
757  /* Avoid leaking memory. */
758  pfree(fullpath);
759  }
760  }
761 
762  pfree(buffer);
763 
764  progress_report(true);
765 }
unsigned char uint8
Definition: c.h:504
static void progress_report(bool finished)
static void verify_file_checksum(verifier_context *context, manifest_file *m, char *fullpath, uint8 *buffer)
#define should_verify_checksum(m)

References context, manifest, manifest_file::pathname, pfree(), pg_malloc(), progress_report(), psprintf(), READ_CHUNK_SIZE, should_ignore_relpath(), should_verify_checksum, and verify_file_checksum().

Referenced by main().

◆ verify_backup_directory()

static void verify_backup_directory ( verifier_context context,
char *  relpath,
char *  fullpath 
)
static

Definition at line 529 of file pg_verifybackup.c.

531 {
532  DIR *dir;
533  struct dirent *dirent;
534 
535  dir = opendir(fullpath);
536  if (dir == NULL)
537  {
538  /*
539  * If even the toplevel backup directory cannot be found, treat this
540  * as a fatal error.
541  */
542  if (relpath == NULL)
543  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  */
550  "could not open directory \"%s\": %m", fullpath);
551  simple_string_list_append(&context->ignore_list, relpath);
552 
553  return;
554  }
555 
556  while (errno = 0, (dirent = readdir(dir)) != NULL)
557  {
558  char *filename = dirent->d_name;
559  char *newfullpath = psprintf("%s/%s", fullpath, filename);
560  char *newrelpath;
561 
562  /* Skip "." and ".." */
563  if (filename[0] == '.' && (filename[1] == '\0'
564  || strcmp(filename, "..") == 0))
565  continue;
566 
567  if (relpath == NULL)
568  newrelpath = pstrdup(filename);
569  else
570  newrelpath = psprintf("%s/%s", relpath, filename);
571 
572  if (!should_ignore_relpath(context, newrelpath))
573  verify_backup_file(context, newrelpath, newfullpath);
574 
575  pfree(newfullpath);
576  pfree(newrelpath);
577  }
578 
579  if (closedir(dir))
580  {
582  "could not close directory \"%s\": %m", fullpath);
583  return;
584  }
585 }
int closedir(DIR *)
Definition: dirent.c:127
struct dirent * readdir(DIR *)
Definition: dirent.c:78
DIR * opendir(const char *)
Definition: dirent.c:33
static char * filename
Definition: pg_dumpall.c:119
static void verify_backup_file(verifier_context *context, char *relpath, char *fullpath)
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

References closedir(), context, dirent::d_name, filename, opendir(), pfree(), psprintf(), pstrdup(), readdir(), relpath, report_backup_error(), report_fatal_error(), should_ignore_relpath(), simple_string_list_append(), and verify_backup_file().

Referenced by main(), and verify_backup_file().

◆ verify_backup_file()

static void verify_backup_file ( verifier_context context,
char *  relpath,
char *  fullpath 
)
static

Definition at line 594 of file pg_verifybackup.c.

595 {
596  struct stat sb;
597  manifest_file *m;
598 
599  if (stat(fullpath, &sb) != 0)
600  {
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  simple_string_list_append(&context->ignore_list, relpath);
610 
611  return;
612  }
613 
614  /* If it's a directory, just recurse. */
615  if (S_ISDIR(sb.st_mode))
616  {
618  return;
619  }
620 
621  /* If it's not a directory, it should be a plain file. */
622  if (!S_ISREG(sb.st_mode))
623  {
625  "\"%s\" is not a file or directory",
626  relpath);
627  return;
628  }
629 
630  /* Check whether there's an entry in the manifest hash. */
631  m = manifest_files_lookup(context->manifest->files, relpath);
632  if (m == NULL)
633  {
635  "\"%s\" is present on disk but not in the manifest",
636  relpath);
637  return;
638  }
639 
640  /* Flag this entry as having been encountered in the filesystem. */
641  m->matched = true;
642 
643  /* Check that the size matches. */
644  if (m->size != sb.st_size)
645  {
647  "\"%s\" has size %lld on disk but size %zu in the manifest",
648  relpath, (long long int) sb.st_size, m->size);
649  m->bad = true;
650  }
651 
652  /*
653  * Validate the manifest system identifier, not available in manifest
654  * version 1.
655  */
656  if (context->manifest->version != 1 &&
657  strcmp(relpath, "global/pg_control") == 0)
658  verify_control_file(fullpath, context->manifest->system_identifier);
659 
660  /* Update statistics for progress report, if necessary */
661  if (show_progress && !context->skip_checksums &&
663  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 }
static void verify_control_file(const char *controlpath, uint64 manifest_system_identifier)
#define stat
Definition: win32_port.h:284
#define S_ISDIR(m)
Definition: win32_port.h:325
#define S_ISREG(m)
Definition: win32_port.h:328

References context, relpath, report_backup_error(), S_ISDIR, S_ISREG, should_verify_checksum, show_progress, simple_string_list_append(), stat::st_mode, stat::st_size, stat, total_size, verify_backup_directory(), and verify_control_file().

Referenced by verify_backup_directory().

◆ verify_control_file()

static void verify_control_file ( const char *  controlpath,
uint64  manifest_system_identifier 
)
static

Definition at line 678 of file pg_verifybackup.c.

679 {
680  ControlFileData *control_file;
681  bool crc_ok;
682 
683  pg_log_debug("reading \"%s\"", controlpath);
684  control_file = get_controlfile_by_exact_path(controlpath, &crc_ok);
685 
686  /* Control file contents not meaningful if CRC is bad. */
687  if (!crc_ok)
688  report_fatal_error("%s: CRC is incorrect", controlpath);
689 
690  /* Can't interpret control file if not current version. */
691  if (control_file->pg_control_version != PG_CONTROL_VERSION)
692  report_fatal_error("%s: unexpected control file version",
693  controlpath);
694 
695  /* System identifiers should match. */
696  if (manifest_system_identifier != control_file->system_identifier)
697  report_fatal_error("%s: manifest system identifier is %llu, but control file has %llu",
698  controlpath,
699  (unsigned long long) manifest_system_identifier,
700  (unsigned long long) control_file->system_identifier);
701 
702  /* Release memory. */
703  pfree(control_file);
704 }
ControlFileData * get_controlfile_by_exact_path(const char *ControlFilePath, bool *crc_ok_p)
#define pg_log_debug(...)
Definition: logging.h:133
#define PG_CONTROL_VERSION
Definition: pg_control.h:25
uint32 pg_control_version
Definition: pg_control.h:125
uint64 system_identifier
Definition: pg_control.h:110

References get_controlfile_by_exact_path(), pfree(), PG_CONTROL_VERSION, ControlFileData::pg_control_version, pg_log_debug, report_fatal_error(), and ControlFileData::system_identifier.

Referenced by verify_backup_file().

◆ verify_file_checksum()

static void verify_file_checksum ( verifier_context context,
manifest_file m,
char *  fullpath,
uint8 buffer 
)
static

Definition at line 771 of file pg_verifybackup.c.

773 {
774  pg_checksum_context checksum_ctx;
775  const char *relpath = m->pathname;
776  int fd;
777  int rc;
778  size_t bytes_read = 0;
779  uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
780  int checksumlen;
781 
782  /* Open the target file. */
783  if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
784  {
785  report_backup_error(context, "could not open file \"%s\": %m",
786  relpath);
787  return;
788  }
789 
790  /* Initialize checksum context. */
791  if (pg_checksum_init(&checksum_ctx, m->checksum_type) < 0)
792  {
793  report_backup_error(context, "could not initialize checksum of file \"%s\"",
794  relpath);
795  close(fd);
796  return;
797  }
798 
799  /* Read the file chunk by chunk, updating the checksum as we go. */
800  while ((rc = read(fd, buffer, READ_CHUNK_SIZE)) > 0)
801  {
802  bytes_read += rc;
803  if (pg_checksum_update(&checksum_ctx, buffer, rc) < 0)
804  {
805  report_backup_error(context, "could not update checksum of file \"%s\"",
806  relpath);
807  close(fd);
808  return;
809  }
810 
811  /* Report progress */
812  done_size += rc;
813  progress_report(false);
814  }
815  if (rc < 0)
816  report_backup_error(context, "could not read file \"%s\": %m",
817  relpath);
818 
819  /* Close the file. */
820  if (close(fd) != 0)
821  {
822  report_backup_error(context, "could not close file \"%s\": %m",
823  relpath);
824  return;
825  }
826 
827  /* If we didn't manage to read the whole file, bail out now. */
828  if (rc < 0)
829  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  if (bytes_read != m->size)
839  {
841  "file \"%s\" should contain %zu bytes, but read %zu bytes",
842  relpath, m->size, bytes_read);
843  return;
844  }
845 
846  /* Get the final checksum. */
847  checksumlen = pg_checksum_final(&checksum_ctx, checksumbuf);
848  if (checksumlen < 0)
849  {
851  "could not finalize checksum of file \"%s\"",
852  relpath);
853  return;
854  }
855 
856  /* And check it against the manifest. */
857  if (checksumlen != m->checksum_length)
859  "file \"%s\" has checksum of length %d, but expected %d",
860  relpath, m->checksum_length, checksumlen);
861  else if (memcmp(checksumbuf, m->checksum_payload, checksumlen) != 0)
863  "checksum mismatch for file \"%s\"",
864  relpath);
865 }
int pg_checksum_final(pg_checksum_context *context, uint8 *output)
int pg_checksum_update(pg_checksum_context *context, const uint8 *input, size_t len)
int pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
#define PG_CHECKSUM_MAX_LENGTH
uint8 * checksum_payload
Definition: load_manifest.h:29
pg_checksum_type checksum_type
Definition: load_manifest.h:27

References manifest_file::checksum_length, manifest_file::checksum_payload, manifest_file::checksum_type, close, context, done_size, fd(), manifest_file::pathname, PG_BINARY, pg_checksum_final(), pg_checksum_init(), PG_CHECKSUM_MAX_LENGTH, pg_checksum_update(), progress_report(), read, READ_CHUNK_SIZE, relpath, report_backup_error(), and manifest_file::size.

Referenced by verify_backup_checksums().

◆ verifybackup_per_file_cb()

static void verifybackup_per_file_cb ( JsonManifestParseContext context,
const char *  pathname,
size_t  size,
pg_checksum_type  checksum_type,
int  checksum_length,
uint8 checksum_payload 
)
static

Definition at line 467 of file pg_verifybackup.c.

471 {
472  manifest_data *manifest = context->private_data;
473  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  m = manifest_files_insert(ht, pathname, &found);
479  if (found)
480  report_fatal_error("duplicate path name in backup manifest: \"%s\"",
481  pathname);
482 
483  /* Initialize the entry. */
484  m->size = size;
485  m->checksum_type = checksum_type;
486  m->checksum_length = checksum_length;
487  m->checksum_payload = checksum_payload;
488  m->matched = false;
489  m->bad = false;
490 }
static pg_noinline void Size size
Definition: slab.c:607

References manifest_file::bad, manifest_file::checksum_length, manifest_file::checksum_payload, manifest_file::checksum_type, context, manifest, manifest_file::matched, report_fatal_error(), size, and manifest_file::size.

Referenced by parse_manifest_file().

◆ verifybackup_per_wal_range_cb()

static void verifybackup_per_wal_range_cb ( JsonManifestParseContext context,
TimeLineID  tli,
XLogRecPtr  start_lsn,
XLogRecPtr  end_lsn 
)
static

Definition at line 496 of file pg_verifybackup.c.

499 {
500  manifest_data *manifest = context->private_data;
502 
503  /* Allocate and initialize a struct describing this WAL range. */
504  range = palloc(sizeof(manifest_wal_range));
505  range->tli = tli;
506  range->start_lsn = start_lsn;
507  range->end_lsn = end_lsn;
508  range->prev = manifest->last_wal_range;
509  range->next = NULL;
510 
511  /* Add it to the end of the list. */
512  if (manifest->first_wal_range == NULL)
513  manifest->first_wal_range = range;
514  else
515  manifest->last_wal_range->next = range;
516  manifest->last_wal_range = range;
517 }
void * palloc(Size size)
Definition: mcxt.c:1317
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412

References context, manifest, palloc(), and range().

Referenced by parse_manifest_file().

◆ verifybackup_system_identifier()

static void verifybackup_system_identifier ( JsonManifestParseContext context,
uint64  manifest_system_identifier 
)
static

Definition at line 454 of file pg_verifybackup.c.

456 {
457  manifest_data *manifest = context->private_data;
458 
459  /* Validation will be at the later stage */
460  manifest->system_identifier = manifest_system_identifier;
461 }

References context, and manifest.

Referenced by parse_manifest_file().

◆ verifybackup_version_cb()

static void verifybackup_version_cb ( JsonManifestParseContext context,
int  manifest_version 
)
static

Definition at line 441 of file pg_verifybackup.c.

443 {
444  manifest_data *manifest = context->private_data;
445 
446  /* Validation will be at the later stage */
447  manifest->version = manifest_version;
448 }

References context, and manifest.

Referenced by parse_manifest_file().

Variable Documentation

◆ done_size

uint64 done_size = 0
static

Definition at line 90 of file pg_verifybackup.c.

Referenced by progress_report(), and verify_file_checksum().

◆ progname

const char* progname
static

Definition at line 83 of file pg_verifybackup.c.

Referenced by main(), and usage().

◆ show_progress

bool show_progress = false
static

Definition at line 86 of file pg_verifybackup.c.

Referenced by main(), progress_report(), and verify_backup_file().

◆ total_size

uint64 total_size = 0
static

Definition at line 89 of file pg_verifybackup.c.

Referenced by progress_report(), and verify_backup_file().