41 #define ESTIMATED_BYTES_PER_MANIFEST_LINE 100
46 #define READ_CHUNK_SIZE (128 * 1024)
60 int manifest_version);
62 uint64 manifest_system_identifier);
64 const char *pathname, uint64
size,
67 uint8 *checksum_payload);
83 uint64 manifest_system_identifier);
94 char *pg_waldump_path,
102 static
void usage(
void);
119 static struct option long_options[] = {
134 char *manifest_path = NULL;
135 bool no_parse_wal =
false;
137 char *wal_directory = NULL;
138 char *pg_waldump_path = NULL;
149 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
154 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
156 puts(
"pg_verifybackup (PostgreSQL) " PG_VERSION);
182 while ((
c =
getopt_long(argc, argv,
"eF:i:m:nPqsw:", long_options, NULL)) != -1)
202 if (strcmp(
optarg,
"p") == 0 || strcmp(
optarg,
"plain") == 0)
204 else if (strcmp(
optarg,
"t") == 0 || strcmp(
optarg,
"tar") == 0)
207 pg_fatal(
"invalid backup format \"%s\", must be \"plain\" or \"tar\"",
246 pg_log_error(
"too many command-line arguments (first is \"%s\")",
254 pg_fatal(
"cannot specify both %s and %s",
255 "-P/--progress",
"-q/--quiet");
264 "pg_waldump (PostgreSQL) " PG_VERSION
"\n",
274 pg_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
275 "pg_waldump",
"pg_verifybackup", full_path);
277 pg_fatal(
"program \"%s\" was found by \"%s\" but was not the same version as %s",
278 "pg_waldump", full_path,
"pg_verifybackup");
283 if (manifest_path == NULL)
284 manifest_path =
psprintf(
"%s/backup_manifest",
288 if (wal_directory == NULL)
318 if (
stat(path, &sb) == 0)
320 else if (errno != ENOENT)
337 if (!no_parse_wal &&
context.format ==
't')
340 pg_log_error_hint(
"You must use -n or --no-parse-wal when verifying a tar-format backup.");
381 if (!
context.saw_any_error && !quiet)
382 printf(
_(
"backup successfully verified\n"));
384 return context.saw_any_error ? 1 : 0;
397 manifest_files_hash *ht;
406 if ((
fd = open(manifest_path, O_RDONLY |
PG_BINARY, 0)) < 0)
418 ht = manifest_files_create(initial_size, NULL);
432 if (statbuf.
st_size <= chunk_size)
439 pg_fatal(
"could not read file \"%s\": %m", manifest_path);
441 pg_fatal(
"could not read file \"%s\": read %d of %lld",
442 manifest_path, rc, (
long long int) statbuf.
st_size);
453 int bytes_left = statbuf.
st_size;
460 while (bytes_left > 0)
462 int bytes_to_read = chunk_size;
469 if (bytes_left < chunk_size)
470 bytes_to_read = bytes_left;
471 else if (bytes_left < 2 * chunk_size)
472 bytes_to_read = bytes_left / 2;
473 rc =
read(
fd, buffer, bytes_to_read);
474 if (rc != bytes_to_read)
477 pg_fatal(
"could not read file \"%s\": %m", manifest_path);
479 pg_fatal(
"could not read file \"%s\": read %lld of %lld",
481 (
long long int) (statbuf.
st_size + rc - bytes_left),
482 (
long long int) statbuf.
st_size);
524 int manifest_version)
529 manifest->version = manifest_version;
537 uint64 manifest_system_identifier)
542 manifest->system_identifier = manifest_system_identifier;
550 const char *pathname, uint64
size,
552 int checksum_length,
uint8 *checksum_payload)
555 manifest_files_hash *ht =
manifest->files;
560 m = manifest_files_insert(ht, pathname, &found);
588 range->start_lsn = start_lsn;
589 range->end_lsn = end_lsn;
594 if (
manifest->first_wal_range == NULL)
616 char *fullpath,
DIR *dir)
621 if (dir == NULL && ((dir =
opendir(fullpath)) == NULL))
624 "could not open directory \"%s\": %m", fullpath);
656 "could not close directory \"%s\": %m", fullpath);
674 if (
stat(fullpath, &sb) != 0)
677 "could not stat file or directory \"%s\": %m",
700 "\"%s\" is not a file or directory",
710 "\"%s\" is present on disk but not in the manifest",
722 "\"%s\" has size %llu on disk but size %llu in the manifest",
724 (
unsigned long long) m->size);
732 if (
context->manifest->version != 1 &&
733 strcmp(
relpath,
"global/pg_control") == 0)
773 report_fatal_error(
"%s: manifest system identifier is %llu, but control file has %llu",
775 (
unsigned long long) manifest_system_identifier,
826 "could not close directory \"%s\": %m",
832 for (cell = tarfiles.
head; cell != NULL; cell = cell->
next)
893 if (
stat(fullpath, &sb) != 0)
896 "could not stat file or directory \"%s\": %m",
905 "\"%s\" is not a plain file",
919 if (strncmp(
"base",
relpath, 4) == 0)
921 else if (strncmp(
"pg_wal",
relpath, 6) == 0)
926 uint64 num = strtoul(
relpath, &suffix, 10);
932 if (suffix == NULL || num <= 0 || num >
OID_MAX)
935 "file \"%s\" is not expected in a tar format backup",
939 tblspc_oid = (
Oid) num;
943 if (strcmp(suffix,
".tar") == 0)
945 else if (strcmp(suffix,
".tgz") == 0)
947 else if (strcmp(suffix,
".tar.gz") == 0)
949 else if (strcmp(suffix,
".tar.lz4") == 0)
951 else if (strcmp(suffix,
".tar.zst") == 0)
956 "file \"%s\" is not expected in a tar format backup",
965 if (strncmp(
"pg_wal",
relpath, 6) == 0)
1002 if ((
fd = open(fullpath, O_RDONLY |
PG_BINARY, 0)) < 0)
1041 manifest_files_iterator it;
1044 manifest_files_start_iterate(
manifest->files, &it);
1045 while ((m = manifest_files_iterate(
manifest->files, &it)) != NULL)
1048 "\"%s\" is present in the manifest but not on disk",
1061 manifest_files_iterator it;
1069 manifest_files_start_iterate(
manifest->files, &it);
1070 while ((m = manifest_files_iterate(
manifest->files, &it)) != NULL)
1099 char *fullpath,
uint8 *buffer)
1105 uint64 bytes_read = 0;
1110 if ((
fd = open(fullpath, O_RDONLY |
PG_BINARY, 0)) < 0)
1165 if (bytes_read != m->
size)
1168 "file \"%s\" should contain %llu bytes, but read %llu bytes",
1170 (
unsigned long long) bytes_read);
1176 if (checksumlen < 0)
1179 "could not finalize checksum of file \"%s\"",
1187 "file \"%s\" has checksum of length %d, but expected %d",
1191 "checksum mismatch for file \"%s\"",
1201 char *wal_directory)
1206 while (this_wal_range != NULL)
1208 char *pg_waldump_cmd;
1210 pg_waldump_cmd =
psprintf(
"\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%X --end=%X/%X\n",
1211 pg_waldump_path, wal_directory, this_wal_range->
tli,
1215 if (system(pg_waldump_cmd) != 0)
1217 "WAL parsing failed for timeline %u",
1218 this_wal_range->
tli);
1220 this_wal_range = this_wal_range->
next;
1239 context->saw_any_error =
true;
1271 for (cell =
context->ignore_list.head; cell != NULL; cell = cell->
next)
1274 char *v = cell->
val;
1276 while (*v !=
'\0' && *r == *v)
1279 if (*v ==
'\0' && (*r ==
'\0' || *r ==
'/'))
1331 int percent_size = 0;
1332 char totalsize_str[32];
1333 char donesize_str[32];
1351 _(
"%*s/%s kB (%d%%) verified"),
1352 (
int) strlen(totalsize_str),
1353 donesize_str, totalsize_str, percent_size);
1359 fputc((!finished && isatty(fileno(stderr))) ?
'\r' :
'\n', stderr);
1368 printf(
_(
"%s verifies a backup against the backup manifest.\n\n"),
progname);
1371 printf(
_(
" -e, --exit-on-error exit immediately on error\n"));
1372 printf(
_(
" -F, --format=p|t backup format (plain, tar)\n"));
1373 printf(
_(
" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
1374 printf(
_(
" -m, --manifest-path=PATH use specified path for manifest\n"));
1375 printf(
_(
" -n, --no-parse-wal do not try to parse WAL files\n"));
1376 printf(
_(
" -P, --progress show progress information\n"));
1377 printf(
_(
" -q, --quiet do not print any output, except for errors\n"));
1378 printf(
_(
" -s, --skip-checksums skip checksum verification\n"));
1379 printf(
_(
" -w, --wal-directory=PATH use specified path for WAL files\n"));
1380 printf(
_(
" -V, --version output version information, then exit\n"));
1381 printf(
_(
" -?, --help show this help, then exit\n"));
1382 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1383 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
static void astreamer_free(astreamer *streamer)
static void astreamer_content(astreamer *streamer, astreamer_member *member, const char *data, int len, astreamer_archive_context context)
static void astreamer_finalize(astreamer *streamer)
astreamer * astreamer_gzip_decompressor_new(astreamer *next)
astreamer * astreamer_lz4_decompressor_new(astreamer *next)
astreamer * astreamer_tar_parser_new(astreamer *next)
astreamer * astreamer_verify_content_new(astreamer *next, verifier_context *context, char *archive_name, Oid tblspc_oid)
astreamer * astreamer_zstd_decompressor_new(astreamer *next)
Datum now(PG_FUNCTION_ARGS)
#define Assert(condition)
#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(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)
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)
static void verify_tar_file(verifier_context *context, char *relpath, char *fullpath, astreamer *streamer)
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)
bool should_ignore_relpath(verifier_context *context, const char *relpath)
int main(int argc, char **argv)
static void verifybackup_per_wal_range_cb(JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
static void verify_plain_backup_directory(verifier_context *context, char *relpath, char *fullpath, DIR *dir)
static void report_extra_backup_files(verifier_context *context)
#define ESTIMATED_BYTES_PER_MANIFEST_LINE
static void progress_report(bool finished)
static void pg_attribute_noreturn()
static astreamer * create_archive_verifier(verifier_context *context, char *archive_name, Oid tblspc_oid, pg_compress_algorithm compress_algo)
static bool show_progress
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)
void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...)
static void precheck_tar_backup_file(verifier_context *context, char *relpath, char *fullpath, SimplePtrList *tarfiles)
static const char * progname
static void verify_tar_backup(verifier_context *context, DIR *dir)
static void verify_plain_backup_file(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 verifybackup_per_file_cb(JsonManifestParseContext *context, const char *pathname, uint64 size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
#define should_verify_checksum(m)
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_ptr_list_destroy(SimplePtrList *list)
void simple_string_list_append(SimpleStringList *list, const char *val)
void simple_ptr_list_append(SimplePtrList *list, void *ptr)
static pg_noinline void Size size
uint32 pg_control_version
struct SimplePtrListCell * next
char val[FLEXIBLE_ARRAY_MEMBER]
struct SimpleStringListCell * next
manifest_files_hash * files
pg_checksum_type checksum_type
struct manifest_wal_range * next
pg_compress_algorithm compress_algorithm
#define LSN_FORMAT_ARGS(lsn)