41 #define ESTIMATED_BYTES_PER_MANIFEST_LINE 100
46 #define READ_CHUNK_SIZE (128 * 1024)
64 #define should_verify_checksum(m) \
65 (((m)->matched) && !((m)->bad) && (((m)->checksum_type) != CHECKSUM_TYPE_NONE))
71 #define SH_PREFIX manifest_files
72 #define SH_ELEMENT_TYPE manifest_file
73 #define SH_KEY_TYPE char *
74 #define SH_KEY pathname
75 #define SH_HASH_KEY(tb, key) hash_string(key)
76 #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
77 #define SH_SCOPE static inline
78 #define SH_RAW_ALLOCATOR pg_malloc0
103 manifest_files_hash *
files;
122 int manifest_version);
124 uint64 manifest_system_identifier);
126 char *pathname,
size_t size,
129 uint8 *checksum_payload);
139 char *
relpath,
char *fullpath);
141 char *
relpath,
char *fullpath);
143 uint64 manifest_system_identifier);
150 char *pg_waldump_path,
151 char *wal_directory);
154 const
char *pg_restrict
fmt,...)
161 static
void usage(
void);
179 static struct option long_options[] = {
193 char *manifest_path = NULL;
194 bool no_parse_wal =
false;
196 char *wal_directory = NULL;
197 char *pg_waldump_path = NULL;
207 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
212 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
214 puts(
"pg_verifybackup (PostgreSQL) " PG_VERSION);
240 while ((
c =
getopt_long(argc, argv,
"ei:m:nPqsw:", long_options, NULL)) != -1)
295 pg_log_error(
"too many command-line arguments (first is \"%s\")",
303 pg_fatal(
"cannot specify both %s and %s",
304 "-P/--progress",
"-q/--quiet");
313 "pg_waldump (PostgreSQL) " PG_VERSION
"\n",
323 pg_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
324 "pg_waldump",
"pg_verifybackup", full_path);
326 pg_fatal(
"program \"%s\" was found by \"%s\" but was not the same version as %s",
327 "pg_waldump", full_path,
"pg_verifybackup");
332 if (manifest_path == NULL)
333 manifest_path =
psprintf(
"%s/backup_manifest",
337 if (wal_directory == NULL)
380 if (!
context.saw_any_error && !quiet)
381 printf(
_(
"backup successfully verified\n"));
383 return context.saw_any_error ? 1 : 0;
396 manifest_files_hash *ht;
405 if ((
fd = open(manifest_path, O_RDONLY |
PG_BINARY, 0)) < 0)
417 ht = manifest_files_create(initial_size, NULL);
431 if (statbuf.
st_size <= chunk_size)
438 pg_fatal(
"could not read file \"%s\": %m", manifest_path);
440 pg_fatal(
"could not read file \"%s\": read %d of %lld",
441 manifest_path, rc, (
long long int) statbuf.
st_size);
452 int bytes_left = statbuf.
st_size;
459 while (bytes_left > 0)
461 int bytes_to_read = chunk_size;
468 if (bytes_left < chunk_size)
469 bytes_to_read = bytes_left;
470 else if (bytes_left < 2 * chunk_size)
471 bytes_to_read = bytes_left / 2;
472 rc =
read(
fd, buffer, bytes_to_read);
473 if (rc != bytes_to_read)
476 pg_fatal(
"could not read file \"%s\": %m", manifest_path);
478 pg_fatal(
"could not read file \"%s\": read %lld of %lld",
480 (
long long int) (statbuf.
st_size + rc - bytes_left),
481 (
long long int) statbuf.
st_size);
523 int manifest_version)
528 manifest->version = manifest_version;
536 uint64 manifest_system_identifier)
541 manifest->system_identifier = manifest_system_identifier;
549 char *pathname,
size_t size,
551 int checksum_length,
uint8 *checksum_payload)
554 manifest_files_hash *ht =
manifest->files;
559 m = manifest_files_insert(ht, pathname, &found);
587 range->start_lsn = start_lsn;
588 range->end_lsn = end_lsn;
593 if (
manifest->first_wal_range == NULL)
631 "could not open directory \"%s\": %m", fullpath);
663 "could not close directory \"%s\": %m", fullpath);
680 if (
stat(fullpath, &sb) != 0)
683 "could not stat file or directory \"%s\": %m",
706 "\"%s\" is not a file or directory",
716 "\"%s\" is present on disk but not in the manifest",
728 "\"%s\" has size %lld on disk but size %zu in the manifest",
737 if (
context->manifest->version != 1 &&
738 strcmp(
relpath,
"global/pg_control") == 0)
777 report_fatal_error(
"%s: manifest system identifier is %llu, but control file has %llu",
779 (
unsigned long long) manifest_system_identifier,
794 manifest_files_iterator it;
797 manifest_files_start_iterate(
manifest->files, &it);
798 while ((m = manifest_files_iterate(
manifest->files, &it)) != NULL)
801 "\"%s\" is present in the manifest but not on disk",
814 manifest_files_iterator it;
822 manifest_files_start_iterate(
manifest->files, &it);
823 while ((m = manifest_files_iterate(
manifest->files, &it)) != NULL)
852 char *fullpath,
uint8 *buffer)
858 size_t bytes_read = 0;
863 if ((
fd = open(fullpath, O_RDONLY |
PG_BINARY, 0)) < 0)
918 if (bytes_read != m->
size)
921 "file \"%s\" should contain %zu bytes, but read %zu bytes",
931 "could not finalize checksum of file \"%s\"",
939 "file \"%s\" has checksum of length %d, but expected %d",
943 "checksum mismatch for file \"%s\"",
958 while (this_wal_range != NULL)
960 char *pg_waldump_cmd;
962 pg_waldump_cmd =
psprintf(
"\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%X --end=%X/%X\n",
963 pg_waldump_path, wal_directory, this_wal_range->
tli,
967 if (system(pg_waldump_cmd) != 0)
969 "WAL parsing failed for timeline %u",
970 this_wal_range->
tli);
972 this_wal_range = this_wal_range->
next;
1023 for (cell =
context->ignore_list.head; cell != NULL; cell = cell->
next)
1026 char *v = cell->
val;
1028 while (*v !=
'\0' && *r == *v)
1031 if (*v ==
'\0' && (*r ==
'\0' || *r ==
'/'))
1052 int percent_size = 0;
1053 char totalsize_str[32];
1054 char donesize_str[32];
1072 _(
"%*s/%s kB (%d%%) verified"),
1073 (
int) strlen(totalsize_str),
1074 donesize_str, totalsize_str, percent_size);
1080 fputc((!finished && isatty(fileno(stderr))) ?
'\r' :
'\n', stderr);
1089 printf(
_(
"%s verifies a backup against the backup manifest.\n\n"),
progname);
1092 printf(
_(
" -e, --exit-on-error exit immediately on error\n"));
1093 printf(
_(
" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
1094 printf(
_(
" -m, --manifest-path=PATH use specified path for manifest\n"));
1095 printf(
_(
" -n, --no-parse-wal do not try to parse WAL files\n"));
1096 printf(
_(
" -P, --progress show progress information\n"));
1097 printf(
_(
" -q, --quiet do not print any output, except for errors\n"));
1098 printf(
_(
" -s, --skip-checksums skip checksum verification\n"));
1099 printf(
_(
" -w, --wal-directory=PATH use specified path for WAL files\n"));
1100 printf(
_(
" -V, --version output version information, then exit\n"));
1101 printf(
_(
" -?, --help show this help, then exit\n"));
1102 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1103 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
Datum now(PG_FUNCTION_ARGS)
#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)
ControlFileData * get_controlfile_by_exact_path(const char *ControlFilePath, bool *crc_ok_p)
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
void * pg_malloc0(size_t size)
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
static void const char * fmt
static void const char fflush(stdout)
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(...)
#define pg_log_debug(...)
char * pstrdup(const char *in)
void pfree(void *pointer)
void json_parse_manifest_incremental_chunk(JsonManifestParseIncrementalState *incstate, char *chunk, int size, bool is_last)
JsonManifestParseIncrementalState * json_parse_manifest_incremental_init(JsonManifestParseContext *context)
void json_parse_manifest(JsonManifestParseContext *context, char *buffer, size_t size)
void json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate)
static pg_time_t last_progress_report
#define PG_CONTROL_VERSION
PGDLLIMPORT char * optarg
static void verify_backup_checksums(verifier_context *context)
static manifest_data * parse_manifest_file(char *manifest_path)
struct verifier_context verifier_context
static void verifybackup_version_cb(JsonManifestParseContext *context, int manifest_version)
static void verifybackup_per_file_cb(JsonManifestParseContext *context, char *pathname, size_t size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
static void verifybackup_system_identifier(JsonManifestParseContext *context, uint64 manifest_system_identifier)
int main(int argc, char **argv)
static void verifybackup_per_wal_range_cb(JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
struct manifest_wal_range manifest_wal_range
static void report_extra_backup_files(verifier_context *context)
#define ESTIMATED_BYTES_PER_MANIFEST_LINE
static bool skip_checksums
static void progress_report(bool finished)
struct manifest_file manifest_file
static void pg_attribute_noreturn()
static bool should_ignore_relpath(verifier_context *context, char *relpath)
#define should_verify_checksum(m)
static bool show_progress
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 parse_required_wal(verifier_context *context, char *pg_waldump_path, char *wal_directory)
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 void verify_file_checksum(verifier_context *context, manifest_file *m, char *fullpath, uint8 *buffer)
static void verify_control_file(const char *controlpath, uint64 manifest_system_identifier)
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
struct manifest_data manifest_data
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(rlocator, forknum)
void simple_string_list_append(SimpleStringList *list, const char *val)
static pg_noinline void Size size
uint32 pg_control_version
char val[FLEXIBLE_ARRAY_MEMBER]
struct SimpleStringListCell * next
manifest_wal_range * last_wal_range
manifest_wal_range * first_wal_range
manifest_files_hash * files
pg_checksum_type checksum_type
struct manifest_wal_range * next
struct manifest_wal_range * prev
SimpleStringList ignore_list
#define LSN_FORMAT_ARGS(lsn)