1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-11-24 08:52:38 +02:00
pg_probackup/validate.c

193 lines
4.8 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* 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
*
*-------------------------------------------------------------------------
*/
2016-11-16 19:34:21 +02:00
#include "pg_probackup.h"
#include <sys/stat.h>
2016-11-25 13:26:58 +02:00
#include <pthread.h>
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;
/*
* Validate each files in the backup with its size.
* TODO: change from bool to void
* TODO: understand and probably fix second argument
*/
2017-02-27 14:00:44 +02:00
bool
pgBackupValidate(pgBackup *backup,
bool size_only,
bool for_get_timeline)
{
char *backup_id_string;
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];
backup_id_string = base36enc(backup->start_time);
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)
elog(INFO, "validate: %s backup and archive log files by %s",
backup_id_string, (size_only ? "SIZE" : "CRC"));
}
if (backup->backup_mode == BACKUP_MODE_FULL ||
backup->backup_mode == BACKUP_MODE_DIFF_PAGE ||
backup->backup_mode == BACKUP_MODE_DIFF_PTRACK)
{
int i;
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);
/* 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]);
}
parray_walk(files, pgFileFree);
parray_free(files);
}
2017-02-27 14:00:44 +02:00
/* update status to OK */
if (corrupted)
backup->status = BACKUP_STATUS_CORRUPT;
else
backup->status = BACKUP_STATUS_OK;
pgBackupWriteConf(backup);
if (corrupted)
elog(WARNING, "backup %s is corrupted", backup_id_string);
else
elog(LOG, "backup %s is valid", backup_id_string);
2017-02-27 14:00:44 +02:00
return !corrupted;
}
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)
{
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++)
{
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;
if (interrupted)
elog(ERROR, "interrupted during validate");
/* skipped backup while differential backup */
/* 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)
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));
/* always validate file size */
if (stat(file->path, &st) == -1)
{
if (errno == ENOENT)
elog(WARNING, "backup file \"%s\" vanished", file->path);
else
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;
}
if (file->write_size != st.st_size)
{
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),
(unsigned long) file->write_size,
(unsigned long) st.st_size);
2016-11-25 13:26:58 +02:00
arguments->corrupted = true;
return;
}
/* validate CRC too */
2016-11-25 13:26:58 +02:00
if (!arguments->size_only)
{
pg_crc32 crc;
crc = pgFileGetCRC(file);
if (crc != file->crc)
{
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;
}
}
}
}