38 #define ESTIMATED_BYTES_PER_MANIFEST_LINE 100
43 #define READ_CHUNK_SIZE 4096
66 #define SH_PREFIX manifest_files
67 #define SH_ELEMENT_TYPE manifest_file
68 #define SH_KEY_TYPE char *
69 #define SH_KEY pathname
70 #define SH_HASH_KEY(tb, key) hash_string_pointer(key)
71 #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
72 #define SH_SCOPE static inline
73 #define SH_RAW_ALLOCATOR pg_malloc0
96 manifest_files_hash *
ht;
106 manifest_files_hash *
ht;
114 manifest_files_hash **ht_p,
118 char *pathname,
size_t size,
121 uint8 *checksum_payload);
131 char *
relpath,
char *fullpath);
133 char *
relpath,
char *fullpath);
139 char *pg_waldump_path,
144 const
char *pg_restrict
fmt,...)
150 static
void usage(
void);
160 static struct option long_options[] = {
174 char *manifest_path = NULL;
175 bool no_parse_wal =
false;
177 bool skip_checksums =
false;
178 char *wal_directory = NULL;
179 char *pg_waldump_path = NULL;
185 memset(&context, 0,
sizeof(context));
189 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
194 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
196 puts(
"pg_verifybackup (PostgreSQL) " PG_VERSION);
222 while ((
c =
getopt_long(argc, argv,
"ei:m:nqsw:", long_options, NULL)) != -1)
227 context.exit_on_error =
true;
248 skip_checksums =
true;
274 pg_log_error(
"too many command-line arguments (first is \"%s\")",
287 "pg_waldump (PostgreSQL) " PG_VERSION
"\n",
297 pg_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
298 "pg_waldump",
"pg_verifybackup", full_path);
300 pg_fatal(
"program \"%s\" was found by \"%s\" but was not the same version as %s",
301 "pg_waldump", full_path,
"pg_verifybackup");
306 if (manifest_path == NULL)
307 manifest_path =
psprintf(
"%s/backup_manifest",
308 context.backup_directory);
311 if (wal_directory == NULL)
312 wal_directory =
psprintf(
"%s/pg_wal", context.backup_directory);
349 wal_directory, first_wal_range);
355 if (!context.saw_any_error && !quiet)
356 printf(
_(
"backup successfully verified\n"));
358 return context.saw_any_error ? 1 : 0;
374 manifest_files_hash *ht;
381 if ((
fd = open(manifest_path, O_RDONLY |
PG_BINARY, 0)) < 0)
393 ht = manifest_files_create(initial_size, NULL);
410 manifest_path, rc, (
long long int) statbuf.
st_size);
417 private_context.
ht = ht;
457 char *pathname,
size_t size,
459 int checksum_length,
uint8 *checksum_payload)
462 manifest_files_hash *ht = pcxt->
ht;
467 m = manifest_files_insert(ht, pathname, &found);
495 range->start_lsn = start_lsn;
496 range->end_lsn = end_lsn;
539 "could not open directory \"%s\": %m", fullpath);
571 "could not close directory \"%s\": %m", fullpath);
588 if (
stat(fullpath, &sb) != 0)
591 "could not stat file or directory \"%s\": %m",
614 "\"%s\" is not a file or directory",
620 m = manifest_files_lookup(context->
ht,
relpath);
624 "\"%s\" is present on disk but not in the manifest",
636 "\"%s\" has size %lld on disk but size %zu in the manifest",
656 manifest_files_iterator it;
659 manifest_files_start_iterate(context->
ht, &it);
660 while ((m = manifest_files_iterate(context->
ht, &it)) != NULL)
663 "\"%s\" is present in the manifest but not on disk",
675 manifest_files_iterator it;
678 manifest_files_start_iterate(context->
ht, &it);
679 while ((m = manifest_files_iterate(context->
ht, &it)) != NULL)
710 size_t bytes_read = 0;
716 if ((
fd = open(fullpath, O_RDONLY |
PG_BINARY, 0)) < 0)
767 if (bytes_read != m->
size)
770 "file \"%s\" should contain %zu bytes, but read %zu bytes",
780 "could not finalize checksum of file \"%s\"",
788 "file \"%s\" has checksum of length %d, but expected %d",
792 "checksum mismatch for file \"%s\"",
806 while (this_wal_range != NULL)
808 char *pg_waldump_cmd;
810 pg_waldump_cmd =
psprintf(
"\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%X --end=%X/%X\n",
811 pg_waldump_path, wal_directory, this_wal_range->
tli,
814 if (system(pg_waldump_cmd) != 0)
816 "WAL parsing failed for timeline %u",
817 this_wal_range->
tli);
819 this_wal_range = this_wal_range->
next;
875 while (*v !=
'\0' && *r == *v)
878 if (*v ==
'\0' && (*r ==
'\0' || *r ==
'/'))
891 unsigned char *ss = (
unsigned char *) s;
902 printf(
_(
"%s verifies a backup against the backup manifest.\n\n"),
progname);
905 printf(
_(
" -e, --exit-on-error exit immediately on error\n"));
906 printf(
_(
" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
907 printf(
_(
" -m, --manifest-path=PATH use specified path for manifest\n"));
908 printf(
_(
" -n, --no-parse-wal do not try to parse WAL files\n"));
909 printf(
_(
" -q, --quiet do not print any output, except for errors\n"));
910 printf(
_(
" -s, --skip-checksums skip checksum verification\n"));
911 printf(
_(
" -w, --wal-directory=PATH use specified path for WAL files\n"));
912 printf(
_(
" -V, --version output version information, then exit\n"));
913 printf(
_(
" -?, --help show this help, then exit\n"));
914 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
915 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
#define PG_TEXTDOMAIN(domain)
#define pg_attribute_printf(f, a)
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
int find_my_exec(const char *argv0, char *retpath)
void set_pglocale_pgservice(const char *argv0, const char *app)
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
void * pg_malloc(size_t size)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
uint32 hash_bytes(const unsigned char *k, int keylen)
static void const char * fmt
void pg_logging_init(const char *argv0)
void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap)
#define pg_log_error(...)
#define pg_log_error_hint(...)
char * pstrdup(const char *in)
void pfree(void *pointer)
void json_parse_manifest(JsonManifestParseContext *context, char *buffer, size_t size)
PGDLLIMPORT char * optarg
static void verify_backup_checksums(verifier_context *context)
static void verify_file_checksum(verifier_context *context, manifest_file *m, char *pathname)
static void record_manifest_details_for_file(JsonManifestParseContext *context, char *pathname, size_t size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
struct verifier_context verifier_context
static void parse_manifest_file(char *manifest_path, manifest_files_hash **ht_p, manifest_wal_range **first_wal_range_p)
struct parser_context parser_context
static void parse_required_wal(verifier_context *context, char *pg_waldump_path, char *wal_directory, manifest_wal_range *first_wal_range)
int main(int argc, char **argv)
struct manifest_wal_range manifest_wal_range
static void report_extra_backup_files(verifier_context *context)
#define ESTIMATED_BYTES_PER_MANIFEST_LINE
struct manifest_file manifest_file
static void pg_attribute_noreturn()
static bool should_ignore_relpath(verifier_context *context, char *relpath)
static void record_manifest_details_for_wal_range(JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
static void verify_backup_file(verifier_context *context, char *relpath, char *fullpath)
static void report_manifest_error(JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
static void static void report_fatal_error(const char *pg_restrict fmt,...) pg_attribute_printf(1
static const char * progname
static void verify_backup_directory(verifier_context *context, char *relpath, char *fullpath)
static uint32 hash_string_pointer(char *s)
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
void canonicalize_path(char *path)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
static int fd(const char *x, int i)
char * psprintf(const char *fmt,...)
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
#define relpath(rnode, forknum)
void simple_string_list_append(SimpleStringList *list, const char *val)
json_manifest_perwalrange_callback perwalrange_cb
json_manifest_perfile_callback perfile_cb
json_manifest_error_callback error_cb
char val[FLEXIBLE_ARRAY_MEMBER]
struct SimpleStringListCell * next
SimpleStringListCell * head
pg_checksum_type checksum_type
struct manifest_wal_range * next
struct manifest_wal_range * prev
manifest_wal_range * last_wal_range
manifest_wal_range * first_wal_range
SimpleStringList ignore_list
#define LSN_FORMAT_ARGS(lsn)