2013-01-24 09:35:48 +03:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* validate.c: validate backup files.
|
|
|
|
*
|
2017-03-01 15:50:07 +02:00
|
|
|
* Portions Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
|
|
|
|
* Portions Copyright (c) 2015-2017, Postgres Professional
|
2013-01-24 09:35:48 +03:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-11-16 19:34:21 +02:00
|
|
|
#include "pg_probackup.h"
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2016-11-25 13:26:58 +02:00
|
|
|
#include <pthread.h>
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-11-25 13:26:58 +02:00
|
|
|
static void pgBackupValidateFiles(void *arg);
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
parray *files;
|
|
|
|
const char *root;
|
|
|
|
bool size_only;
|
|
|
|
bool corrupted;
|
|
|
|
} validate_files_args;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2017-02-16 18:44:16 +02:00
|
|
|
int
|
|
|
|
do_validate(time_t backup_id,
|
|
|
|
const char *target_time,
|
|
|
|
const char *target_xid,
|
|
|
|
const char *target_inclusive,
|
|
|
|
TimeLineID target_tli)
|
2016-12-06 15:44:18 +02:00
|
|
|
{
|
2017-02-15 19:44:06 +02:00
|
|
|
int i;
|
|
|
|
int base_index; /* index of base (full) backup */
|
2017-02-28 19:00:18 +02:00
|
|
|
int last_diff_index = -1; /* index of last differential backup */
|
2017-02-15 19:44:06 +02:00
|
|
|
parray *timelines;
|
|
|
|
parray *backups;
|
2016-12-06 15:44:18 +02:00
|
|
|
pgRecoveryTarget *rt = NULL;
|
2017-02-15 19:44:06 +02:00
|
|
|
pgBackup *base_backup = NULL;
|
2017-02-28 19:00:18 +02:00
|
|
|
pgBackup *dest_backup = NULL;
|
2017-03-24 15:58:35 +02:00
|
|
|
bool success_validate;
|
2016-12-06 15:44:18 +02:00
|
|
|
|
2017-03-06 11:46:15 +02:00
|
|
|
catalog_lock(false);
|
2016-12-06 15:44:18 +02:00
|
|
|
|
|
|
|
rt = checkIfCreateRecoveryConf(target_time, target_xid, target_inclusive);
|
|
|
|
if (rt == NULL)
|
|
|
|
elog(ERROR, "cannot create recovery.conf. specified args are invalid.");
|
|
|
|
|
|
|
|
/* get list of backups. (index == 0) is the last backup */
|
|
|
|
backups = catalog_get_backup_list(0);
|
|
|
|
if (!backups)
|
|
|
|
elog(ERROR, "cannot process any more.");
|
|
|
|
|
|
|
|
/* Read timeline history files from archives */
|
2017-02-16 18:44:16 +02:00
|
|
|
if (target_tli)
|
|
|
|
timelines = readTimeLineHistory(target_tli);
|
2016-12-06 15:44:18 +02:00
|
|
|
|
|
|
|
/* find last full backup which can be used as base backup. */
|
|
|
|
elog(LOG, "searching recent full backup");
|
|
|
|
for (i = 0; i < parray_num(backups); i++)
|
|
|
|
{
|
2017-02-28 19:00:18 +02:00
|
|
|
bool satisfied = false;
|
|
|
|
|
2016-12-06 15:44:18 +02:00
|
|
|
base_backup = (pgBackup *) parray_get(backups, i);
|
|
|
|
|
|
|
|
if (backup_id && base_backup->start_time > backup_id)
|
|
|
|
continue;
|
|
|
|
|
2017-02-28 19:00:18 +02:00
|
|
|
if (backup_id == base_backup->start_time)
|
|
|
|
{
|
|
|
|
/* Checks for target backup */
|
|
|
|
if (base_backup->status != BACKUP_STATUS_OK &&
|
|
|
|
base_backup->status != BACKUP_STATUS_CORRUPT)
|
|
|
|
elog(ERROR, "given backup %s is in %s status",
|
|
|
|
base36enc(backup_id), status2str(base_backup->status));
|
2016-12-06 15:44:18 +02:00
|
|
|
|
2017-02-28 19:00:18 +02:00
|
|
|
dest_backup = base_backup;
|
|
|
|
}
|
2016-12-06 15:44:18 +02:00
|
|
|
|
2017-02-28 19:00:18 +02:00
|
|
|
if (dest_backup != NULL &&
|
|
|
|
base_backup->backup_mode == BACKUP_MODE_FULL &&
|
|
|
|
base_backup->status != BACKUP_STATUS_OK)
|
|
|
|
elog(ERROR, "base backup %s for given backup %s is in %s status",
|
|
|
|
base36enc(base_backup->start_time),
|
|
|
|
base36enc(dest_backup->start_time),
|
|
|
|
status2str(base_backup->status));
|
|
|
|
|
|
|
|
/* Dont check error backups */
|
|
|
|
if ((base_backup->status != BACKUP_STATUS_OK &&
|
|
|
|
base_backup->status != BACKUP_STATUS_CORRUPT) ||
|
|
|
|
/* Dont check differential backups if we found latest */
|
|
|
|
(last_diff_index >= 0 && base_backup->backup_mode != BACKUP_MODE_FULL))
|
2016-12-06 15:44:18 +02:00
|
|
|
continue;
|
|
|
|
|
2017-02-16 18:44:16 +02:00
|
|
|
if (target_tli)
|
|
|
|
{
|
|
|
|
if (satisfy_timeline(timelines, base_backup) &&
|
|
|
|
satisfy_recovery_target(base_backup, rt) &&
|
2017-02-28 19:00:18 +02:00
|
|
|
(dest_backup || backup_id == 0))
|
|
|
|
satisfied = true;
|
2017-02-16 18:44:16 +02:00
|
|
|
}
|
2016-12-06 15:44:18 +02:00
|
|
|
else
|
2017-02-16 18:44:16 +02:00
|
|
|
if (satisfy_recovery_target(base_backup, rt) &&
|
2017-02-28 19:00:18 +02:00
|
|
|
(dest_backup || backup_id == 0))
|
|
|
|
satisfied = true;
|
|
|
|
|
|
|
|
/* Target backup should satisfy validate options */
|
|
|
|
if (backup_id == base_backup->start_time && !satisfied)
|
|
|
|
elog(ERROR, "backup %s does not satisfy validate options",
|
|
|
|
base36enc(base_backup->start_time));
|
2017-02-16 18:44:16 +02:00
|
|
|
|
2017-02-28 19:00:18 +02:00
|
|
|
if (satisfied)
|
|
|
|
{
|
|
|
|
if (base_backup->backup_mode != BACKUP_MODE_FULL)
|
|
|
|
last_diff_index = i;
|
|
|
|
else
|
|
|
|
goto base_backup_found;
|
|
|
|
}
|
2016-12-06 15:44:18 +02:00
|
|
|
}
|
|
|
|
/* no full backup found, cannot restore */
|
|
|
|
elog(ERROR, "no full backup found, cannot validate.");
|
|
|
|
|
|
|
|
base_backup_found:
|
|
|
|
base_index = i;
|
2017-02-28 19:00:18 +02:00
|
|
|
if (last_diff_index == -1)
|
|
|
|
last_diff_index = base_index;
|
2016-12-06 15:44:18 +02:00
|
|
|
|
2017-02-28 19:00:18 +02:00
|
|
|
Assert(last_diff_index <= base_index);
|
2016-12-06 15:44:18 +02:00
|
|
|
|
2017-02-28 19:00:18 +02:00
|
|
|
/* Validate backups from base_index to last_diff_index */
|
|
|
|
for (i = base_index; i >= last_diff_index; i--)
|
2016-12-06 15:44:18 +02:00
|
|
|
{
|
2017-02-28 19:00:18 +02:00
|
|
|
pgBackup *backup = (pgBackup *) parray_get(backups, i);
|
2016-12-06 15:44:18 +02:00
|
|
|
|
2017-02-28 19:00:18 +02:00
|
|
|
if (backup->status == BACKUP_STATUS_OK ||
|
|
|
|
backup->status == BACKUP_STATUS_CORRUPT)
|
|
|
|
success_validate = pgBackupValidate(backup, false, false) &&
|
|
|
|
success_validate;
|
2016-12-06 15:44:18 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 15:58:35 +02:00
|
|
|
/* And now we must check WALs */
|
|
|
|
dest_backup = (pgBackup *) parray_get(backups, last_diff_index);
|
|
|
|
if (!dest_backup->stream || (target_time != NULL || target_xid != NULL))
|
|
|
|
validate_wal(dest_backup,
|
2016-12-07 15:28:48 +02:00
|
|
|
arclog_path,
|
|
|
|
rt->recovery_target_time,
|
|
|
|
rt->recovery_target_xid,
|
2017-02-16 18:44:16 +02:00
|
|
|
base_backup->tli);
|
2017-02-27 14:00:44 +02:00
|
|
|
else if (success_validate)
|
|
|
|
elog(INFO, "backup validation stopped successfully");
|
2016-12-06 15:44:18 +02:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
parray_walk(backups, pgBackupFree);
|
|
|
|
parray_free(backups);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate each files in the backup with its size.
|
|
|
|
*/
|
2017-02-27 14:00:44 +02:00
|
|
|
bool
|
2013-12-15 18:30:49 +03:00
|
|
|
pgBackupValidate(pgBackup *backup,
|
|
|
|
bool size_only,
|
|
|
|
bool for_get_timeline)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2016-12-06 15:44:18 +02:00
|
|
|
char *backup_id_string;
|
2013-01-24 09:35:48 +03:00
|
|
|
char base_path[MAXPGPATH];
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
parray *files;
|
|
|
|
bool corrupted = false;
|
2016-11-25 13:26:58 +02:00
|
|
|
pthread_t validate_threads[num_threads];
|
|
|
|
validate_files_args *validate_threads_args[num_threads];
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-12-06 15:44:18 +02:00
|
|
|
backup_id_string = base36enc(backup->start_time);
|
2013-12-15 18:30:49 +03:00
|
|
|
if (!for_get_timeline)
|
|
|
|
{
|
|
|
|
if (backup->backup_mode == BACKUP_MODE_FULL ||
|
2016-02-27 20:07:55 +02:00
|
|
|
backup->backup_mode == BACKUP_MODE_DIFF_PAGE ||
|
|
|
|
backup->backup_mode == BACKUP_MODE_DIFF_PTRACK)
|
2013-12-15 18:30:49 +03:00
|
|
|
elog(INFO, "validate: %s backup and archive log files by %s",
|
2016-12-06 15:44:18 +02:00
|
|
|
backup_id_string, (size_only ? "SIZE" : "CRC"));
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
2013-12-15 18:30:49 +03:00
|
|
|
if (!check)
|
|
|
|
{
|
|
|
|
if (backup->backup_mode == BACKUP_MODE_FULL ||
|
2016-02-27 20:07:55 +02:00
|
|
|
backup->backup_mode == BACKUP_MODE_DIFF_PAGE ||
|
|
|
|
backup->backup_mode == BACKUP_MODE_DIFF_PTRACK)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2016-11-25 13:26:58 +02:00
|
|
|
int i;
|
2013-01-24 09:35:48 +03:00
|
|
|
elog(LOG, "database files...");
|
|
|
|
pgBackupGetPath(backup, base_path, lengthof(base_path), DATABASE_DIR);
|
|
|
|
pgBackupGetPath(backup, path, lengthof(path),
|
|
|
|
DATABASE_FILE_LIST);
|
|
|
|
files = dir_read_file_list(base_path, path);
|
2016-11-25 13:26:58 +02:00
|
|
|
|
|
|
|
/* setup threads */
|
|
|
|
for (i = 0; i < parray_num(files); i++)
|
|
|
|
{
|
|
|
|
pgFile *file = (pgFile *) parray_get(files, i);
|
|
|
|
__sync_lock_release(&file->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore files into $PGDATA */
|
|
|
|
for (i = 0; i < num_threads; i++)
|
|
|
|
{
|
|
|
|
validate_files_args *arg = pg_malloc(sizeof(validate_files_args));
|
|
|
|
arg->files = files;
|
|
|
|
arg->root = base_path;
|
|
|
|
arg->size_only = size_only;
|
|
|
|
arg->corrupted = false;
|
|
|
|
|
|
|
|
validate_threads_args[i] = arg;
|
|
|
|
pthread_create(&validate_threads[i], NULL, (void *(*)(void *)) pgBackupValidateFiles, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait theads */
|
|
|
|
for (i = 0; i < num_threads; i++)
|
|
|
|
{
|
|
|
|
pthread_join(validate_threads[i], NULL);
|
|
|
|
if (validate_threads_args[i]->corrupted)
|
|
|
|
corrupted = true;
|
|
|
|
pg_free(validate_threads_args[i]);
|
|
|
|
}
|
2013-01-24 09:35:48 +03:00
|
|
|
parray_walk(files, pgFileFree);
|
|
|
|
parray_free(files);
|
|
|
|
}
|
2013-12-15 18:30:49 +03:00
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
/* update status to OK */
|
|
|
|
if (corrupted)
|
|
|
|
backup->status = BACKUP_STATUS_CORRUPT;
|
|
|
|
else
|
|
|
|
backup->status = BACKUP_STATUS_OK;
|
|
|
|
pgBackupWriteIni(backup);
|
|
|
|
|
|
|
|
if (corrupted)
|
2016-12-06 15:44:18 +02:00
|
|
|
elog(WARNING, "backup %s is corrupted", backup_id_string);
|
2013-01-24 09:35:48 +03:00
|
|
|
else
|
2016-12-06 15:44:18 +02:00
|
|
|
elog(LOG, "backup %s is valid", backup_id_string);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
2017-02-27 14:00:44 +02:00
|
|
|
|
|
|
|
return !corrupted;
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_relative_path(const char *path, const char *root)
|
|
|
|
{
|
|
|
|
size_t rootlen = strlen(root);
|
|
|
|
if (strncmp(path, root, rootlen) == 0 && path[rootlen] == '/')
|
|
|
|
return path + rootlen + 1;
|
|
|
|
else
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate files in the backup with size or CRC.
|
|
|
|
*/
|
2016-11-25 13:26:58 +02:00
|
|
|
static void
|
|
|
|
pgBackupValidateFiles(void *arg)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-11-25 13:26:58 +02:00
|
|
|
validate_files_args *arguments = (validate_files_args *)arg;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < parray_num(arguments->files); i++)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2016-11-25 13:26:58 +02:00
|
|
|
pgFile *file = (pgFile *) parray_get(arguments->files, i);
|
|
|
|
if (__sync_lock_test_and_set(&file->lock, 1) != 0)
|
|
|
|
continue;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
if (interrupted)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "interrupted during validate");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2014-01-30 09:58:55 +03:00
|
|
|
/* skipped backup while differential backup */
|
2017-02-13 10:44:53 +02:00
|
|
|
/* NOTE We don't compute checksums for compressed data,
|
|
|
|
* so skip it too */
|
|
|
|
if (file->write_size == BYTES_INVALID
|
|
|
|
|| !S_ISREG(file->mode)
|
|
|
|
|| file->generation != -1)
|
2013-01-24 09:35:48 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* print progress */
|
2016-11-25 13:26:58 +02:00
|
|
|
elog(LOG, "(%d/%lu) %s", i + 1, (unsigned long) parray_num(arguments->files),
|
|
|
|
get_relative_path(file->path, arguments->root));
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* always validate file size */
|
|
|
|
if (stat(file->path, &st) == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(WARNING, "backup file \"%s\" vanished", file->path);
|
2013-01-24 09:35:48 +03:00
|
|
|
else
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot stat backup file \"%s\": %s",
|
2016-11-25 13:26:58 +02:00
|
|
|
get_relative_path(file->path, arguments->root), strerror(errno));
|
|
|
|
arguments->corrupted = true;
|
|
|
|
return;
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
if (file->write_size != st.st_size)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(WARNING, "size of backup file \"%s\" must be %lu but %lu",
|
2016-11-25 13:26:58 +02:00
|
|
|
get_relative_path(file->path, arguments->root),
|
2013-01-24 09:35:48 +03:00
|
|
|
(unsigned long) file->write_size,
|
|
|
|
(unsigned long) st.st_size);
|
2016-11-25 13:26:58 +02:00
|
|
|
arguments->corrupted = true;
|
|
|
|
return;
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* validate CRC too */
|
2016-11-25 13:26:58 +02:00
|
|
|
if (!arguments->size_only)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
|
|
|
pg_crc32 crc;
|
|
|
|
|
|
|
|
crc = pgFileGetCRC(file);
|
|
|
|
if (crc != file->crc)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(WARNING, "CRC of backup file \"%s\" must be %X but %X",
|
2016-11-25 13:26:58 +02:00
|
|
|
get_relative_path(file->path, arguments->root), file->crc, crc);
|
|
|
|
arguments->corrupted = true;
|
|
|
|
return;
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|