2013-01-24 09:35:48 +03:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* restore.c: restore DB cluster and archived WAL.
|
|
|
|
*
|
2013-09-09 12:00:13 +03:00
|
|
|
* Copyright (c) 2009-2013, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
|
2013-01-24 09:35:48 +03:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2014-01-27 06:02:56 +03:00
|
|
|
#include "pg_arman.h"
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "catalog/pg_control.h"
|
|
|
|
|
|
|
|
static void backup_online_files(bool re_recovery);
|
|
|
|
static void restore_database(pgBackup *backup);
|
|
|
|
static void create_recovery_conf(const char *target_time,
|
|
|
|
const char *target_xid,
|
|
|
|
const char *target_inclusive,
|
|
|
|
TimeLineID target_tli);
|
|
|
|
static pgRecoveryTarget *checkIfCreateRecoveryConf(const char *target_time,
|
|
|
|
const char *target_xid,
|
|
|
|
const char *target_inclusive);
|
|
|
|
static parray * readTimeLineHistory(TimeLineID targetTLI);
|
|
|
|
static bool satisfy_timeline(const parray *timelines, const pgBackup *backup);
|
2013-12-12 16:45:57 +03:00
|
|
|
static bool satisfy_recovery_target(const pgBackup *backup,
|
|
|
|
const pgRecoveryTarget *rt);
|
|
|
|
static TimeLineID get_fullbackup_timeline(parray *backups,
|
|
|
|
const pgRecoveryTarget *rt);
|
2016-01-14 09:36:39 +02:00
|
|
|
static void print_backup_lsn(const pgBackup *backup);
|
2013-12-12 16:45:57 +03:00
|
|
|
static void search_next_wal(const char *path,
|
|
|
|
XLogRecPtr *need_lsn,
|
|
|
|
parray *timelines);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
do_restore(const char *target_time,
|
|
|
|
const char *target_xid,
|
|
|
|
const char *target_inclusive,
|
2014-01-24 16:36:31 +03:00
|
|
|
TimeLineID target_tli)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int base_index; /* index of base (full) backup */
|
|
|
|
int last_restored_index; /* index of last restored database backup */
|
|
|
|
int ret;
|
|
|
|
TimeLineID cur_tli;
|
|
|
|
TimeLineID backup_tli;
|
|
|
|
parray *backups;
|
|
|
|
pgBackup *base_backup = NULL;
|
|
|
|
parray *files;
|
|
|
|
parray *timelines;
|
|
|
|
pgRecoveryTarget *rt = NULL;
|
2013-12-12 16:45:57 +03:00
|
|
|
XLogRecPtr need_lsn;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* PGDATA and ARCLOG_PATH are always required */
|
|
|
|
if (pgdata == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"required parameter not specified: PGDATA (-D, --pgdata)");
|
2013-01-24 09:35:48 +03:00
|
|
|
if (arclog_path == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"required parameter not specified: ARCLOG_PATH (-A, --arclog-path)");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "========================================");
|
|
|
|
elog(LOG, "restore start");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* get exclusive lock of backup catalog */
|
|
|
|
ret = catalog_lock();
|
|
|
|
if (ret == -1)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot lock backup catalog.");
|
2013-01-24 09:35:48 +03:00
|
|
|
else if (ret == 1)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"another pg_arman is running, stop restore.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* confirm the PostgreSQL server is not running */
|
|
|
|
if (is_pg_running())
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "PostgreSQL server is running");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
rt = checkIfCreateRecoveryConf(target_time, target_xid, target_inclusive);
|
2016-01-14 09:36:39 +02:00
|
|
|
if (rt == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot create recovery.conf. specified args are invalid.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* get list of backups. (index == 0) is the last backup */
|
|
|
|
backups = catalog_get_backup_list(NULL);
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!backups)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot process any more.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
cur_tli = get_current_timeline();
|
|
|
|
backup_tli = get_fullbackup_timeline(backups, rt);
|
|
|
|
|
|
|
|
/* determine target timeline */
|
|
|
|
if (target_tli == 0)
|
|
|
|
target_tli = cur_tli != 0 ? cur_tli : backup_tli;
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "current timeline ID = %u", cur_tli);
|
|
|
|
elog(LOG, "latest full backup timeline ID = %u", backup_tli);
|
|
|
|
elog(LOG, "target timeline ID = %u", target_tli);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2014-01-24 14:37:13 +03:00
|
|
|
/* backup online WAL */
|
2013-01-24 09:35:48 +03:00
|
|
|
backup_online_files(cur_tli != 0 && cur_tli != backup_tli);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear restore destination, but don't remove $PGDATA.
|
|
|
|
* To remove symbolic link, get file list with "omit_symlink = false".
|
|
|
|
*/
|
|
|
|
if (!check)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "----------------------------------------");
|
|
|
|
elog(LOG, "clearing restore destination");
|
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
files = parray_new();
|
|
|
|
dir_list_file(files, pgdata, NULL, false, false);
|
|
|
|
parray_qsort(files, pgFileComparePathDesc); /* delete from leaf */
|
|
|
|
|
|
|
|
for (i = 0; i < parray_num(files); i++)
|
|
|
|
{
|
|
|
|
pgFile *file = (pgFile *) parray_get(files, i);
|
|
|
|
pgFileDelete(file);
|
|
|
|
}
|
|
|
|
parray_walk(files, pgFileFree);
|
|
|
|
parray_free(files);
|
|
|
|
}
|
|
|
|
|
2014-01-24 16:36:31 +03:00
|
|
|
/* Read timeline history files from archives */
|
2013-01-24 09:35:48 +03:00
|
|
|
timelines = readTimeLineHistory(target_tli);
|
|
|
|
|
|
|
|
/* find last full backup which can be used as base backup. */
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "searching recent full backup");
|
2013-01-24 09:35:48 +03:00
|
|
|
for (i = 0; i < parray_num(backups); i++)
|
|
|
|
{
|
|
|
|
base_backup = (pgBackup *) parray_get(backups, i);
|
|
|
|
|
|
|
|
if (base_backup->backup_mode < BACKUP_MODE_FULL ||
|
|
|
|
base_backup->status != BACKUP_STATUS_OK)
|
|
|
|
continue;
|
|
|
|
|
2013-12-15 18:30:49 +03:00
|
|
|
if (satisfy_timeline(timelines, base_backup) &&
|
|
|
|
satisfy_recovery_target(base_backup, rt))
|
2013-01-24 09:35:48 +03:00
|
|
|
goto base_backup_found;
|
|
|
|
}
|
2016-01-14 09:36:39 +02:00
|
|
|
/* no full backup found, cannot restore */
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "no full backup found, cannot restore.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
base_backup_found:
|
|
|
|
base_index = i;
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
print_backup_lsn(base_backup);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* restore base backup */
|
|
|
|
restore_database(base_backup);
|
|
|
|
|
|
|
|
last_restored_index = base_index;
|
|
|
|
|
2014-01-30 09:58:55 +03:00
|
|
|
/* restore following differential backup */
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "searching differential backup...");
|
2016-01-15 16:47:38 +02:00
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
for (i = base_index - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
pgBackup *backup = (pgBackup *) parray_get(backups, i);
|
|
|
|
|
|
|
|
/* don't use incomplete nor different timeline backup */
|
|
|
|
if (backup->status != BACKUP_STATUS_OK ||
|
2016-01-15 16:47:38 +02:00
|
|
|
backup->tli != base_backup->tli)
|
2013-01-24 09:35:48 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* use database backup only */
|
2016-02-27 20:07:55 +02:00
|
|
|
if (backup->backup_mode != BACKUP_MODE_DIFF_PAGE &&
|
|
|
|
backup->backup_mode != BACKUP_MODE_DIFF_PTRACK)
|
2013-01-24 09:35:48 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* is the backup is necessary for restore to target timeline ? */
|
2013-12-15 17:50:04 +03:00
|
|
|
if (!satisfy_timeline(timelines, backup) ||
|
|
|
|
!satisfy_recovery_target(backup, rt))
|
2013-01-24 09:35:48 +03:00
|
|
|
continue;
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
print_backup_lsn(backup);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
restore_database(backup);
|
|
|
|
last_restored_index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = last_restored_index; i >= 0; i--)
|
|
|
|
{
|
|
|
|
char xlogpath[MAXPGPATH];
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "searching archived WAL...");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2013-12-12 16:45:57 +03:00
|
|
|
search_next_wal(arclog_path, &need_lsn, timelines);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "searching online WAL...");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
join_path_components(xlogpath, pgdata, PG_XLOG_DIR);
|
2013-12-12 16:45:57 +03:00
|
|
|
search_next_wal(xlogpath, &need_lsn, timelines);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "all necessary files are found");
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create recovery.conf */
|
|
|
|
create_recovery_conf(target_time, target_xid, target_inclusive, target_tli);
|
|
|
|
|
|
|
|
/* release catalog lock */
|
|
|
|
catalog_unlock();
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
parray_walk(backups, pgBackupFree);
|
|
|
|
parray_free(backups);
|
|
|
|
|
|
|
|
/* print restore complete message */
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "all restore completed");
|
|
|
|
elog(LOG, "========================================");
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
if (!check)
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(INFO, "restore complete. Recovery starts automatically when the PostgreSQL server is started.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
return 0;
|
2013-12-09 21:21:07 +03:00
|
|
|
}
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate and restore backup.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
restore_database(pgBackup *backup)
|
|
|
|
{
|
|
|
|
char timestamp[100];
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char list_path[MAXPGPATH];
|
|
|
|
int ret;
|
|
|
|
parray *files;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* confirm block size compatibility */
|
|
|
|
if (backup->block_size != BLCKSZ)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"BLCKSZ(%d) is not compatible(%d expected)",
|
2013-01-24 09:35:48 +03:00
|
|
|
backup->block_size, BLCKSZ);
|
|
|
|
if (backup->wal_block_size != XLOG_BLCKSZ)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"XLOG_BLCKSZ(%d) is not compatible(%d expected)",
|
2013-01-24 09:35:48 +03:00
|
|
|
backup->wal_block_size, XLOG_BLCKSZ);
|
|
|
|
|
|
|
|
time2iso(timestamp, lengthof(timestamp), backup->start_time);
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "----------------------------------------");
|
|
|
|
elog(LOG, "restoring database from backup %s", timestamp);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate backup files with its size, because load of CRC calculation is
|
|
|
|
* not right.
|
|
|
|
*/
|
2013-12-15 18:30:49 +03:00
|
|
|
pgBackupValidate(backup, true, false);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* make direcotries and symbolic links */
|
|
|
|
pgBackupGetPath(backup, path, lengthof(path), MKDIRS_SH_FILE);
|
|
|
|
if (!check)
|
|
|
|
{
|
|
|
|
char pwd[MAXPGPATH];
|
|
|
|
|
|
|
|
/* keep orginal directory */
|
|
|
|
if (getcwd(pwd, sizeof(pwd)) == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot get current working directory: %s",
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
/* create pgdata directory */
|
|
|
|
dir_create_dir(pgdata, DIR_PERMISSION);
|
|
|
|
|
|
|
|
/* change directory to pgdata */
|
|
|
|
if (chdir(pgdata))
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot change directory: %s",
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
/* Execute mkdirs.sh */
|
|
|
|
ret = system(path);
|
|
|
|
if (ret != 0)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot execute mkdirs.sh: %s",
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
/* go back to original directory */
|
|
|
|
if (chdir(pwd))
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot change directory: %s",
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get list of files which need to be restored.
|
|
|
|
*/
|
|
|
|
pgBackupGetPath(backup, path, lengthof(path), DATABASE_DIR);
|
|
|
|
pgBackupGetPath(backup, list_path, lengthof(list_path), DATABASE_FILE_LIST);
|
|
|
|
files = dir_read_file_list(path, list_path);
|
|
|
|
for (i = parray_num(files) - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
pgFile *file = (pgFile *) parray_get(files, i);
|
|
|
|
|
|
|
|
/* remove files which are not backed up */
|
|
|
|
if (file->write_size == BYTES_INVALID)
|
|
|
|
pgFileFree(parray_remove(files, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore files into $PGDATA */
|
|
|
|
for (i = 0; i < parray_num(files); i++)
|
|
|
|
{
|
|
|
|
char from_root[MAXPGPATH];
|
|
|
|
pgFile *file = (pgFile *) parray_get(files, i);
|
|
|
|
|
|
|
|
pgBackupGetPath(backup, from_root, lengthof(from_root), DATABASE_DIR);
|
|
|
|
|
|
|
|
/* check for interrupt */
|
|
|
|
if (interrupted)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "interrupted during restore database");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* print progress */
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
|
|
|
elog(LOG, "(%d/%lu) %s ", i + 1, (unsigned long) parray_num(files),
|
2013-01-24 09:35:48 +03:00
|
|
|
file->path + strlen(from_root) + 1);
|
|
|
|
|
|
|
|
/* directories are created with mkdirs.sh */
|
|
|
|
if (S_ISDIR(file->mode))
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
|
|
|
elog(LOG, "directory, skip");
|
2013-01-24 09:35:48 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not backed up */
|
|
|
|
if (file->write_size == BYTES_INVALID)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
|
|
|
elog(LOG, "not backed up, skip");
|
2013-01-24 09:35:48 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore file */
|
|
|
|
if (!check)
|
2016-01-14 08:19:38 +02:00
|
|
|
restore_data_file(from_root, pgdata, file);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* print size of restored file */
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
|
|
|
elog(LOG, "restored %lu\n", (unsigned long) file->write_size);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete files which are not in file list. */
|
|
|
|
if (!check)
|
|
|
|
{
|
|
|
|
parray *files_now;
|
|
|
|
|
|
|
|
parray_walk(files, pgFileFree);
|
|
|
|
parray_free(files);
|
|
|
|
|
|
|
|
/* re-read file list to change base path to $PGDATA */
|
|
|
|
files = dir_read_file_list(pgdata, list_path);
|
|
|
|
parray_qsort(files, pgFileComparePathDesc);
|
|
|
|
|
|
|
|
/* get list of files restored to pgdata */
|
|
|
|
files_now = parray_new();
|
|
|
|
dir_list_file(files_now, pgdata, pgdata_exclude, true, false);
|
|
|
|
/* to delete from leaf, sort in reversed order */
|
|
|
|
parray_qsort(files_now, pgFileComparePathDesc);
|
|
|
|
|
|
|
|
for (i = 0; i < parray_num(files_now); i++)
|
|
|
|
{
|
|
|
|
pgFile *file = (pgFile *) parray_get(files_now, i);
|
|
|
|
|
|
|
|
/* If the file is not in the file list, delete it */
|
|
|
|
if (parray_bsearch(files, file, pgFileComparePathDesc) == NULL)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "deleted %s", file->path + strlen(pgdata) + 1);
|
2013-01-24 09:35:48 +03:00
|
|
|
pgFileDelete(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parray_walk(files_now, pgFileFree);
|
|
|
|
parray_free(files_now);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove postmaster.pid */
|
|
|
|
snprintf(path, lengthof(path), "%s/postmaster.pid", pgdata);
|
|
|
|
if (remove(path) == -1 && errno != ENOENT)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot remove postmaster.pid: %s",
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
parray_walk(files, pgFileFree);
|
|
|
|
parray_free(files);
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
|
|
|
elog(LOG, "restore backup completed");
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_recovery_conf(const char *target_time,
|
|
|
|
const char *target_xid,
|
|
|
|
const char *target_inclusive,
|
|
|
|
TimeLineID target_tli)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
FILE *fp;
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "----------------------------------------");
|
|
|
|
elog(LOG, "creating recovery.conf");
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!check)
|
|
|
|
{
|
|
|
|
snprintf(path, lengthof(path), "%s/recovery.conf", pgdata);
|
|
|
|
fp = fopen(path, "wt");
|
|
|
|
if (fp == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot open recovery.conf \"%s\": %s", path,
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
|
2014-01-27 06:02:56 +03:00
|
|
|
fprintf(fp, "# recovery.conf generated by pg_arman %s\n",
|
2013-01-24 09:35:48 +03:00
|
|
|
PROGRAM_VERSION);
|
|
|
|
fprintf(fp, "restore_command = 'cp %s/%%f %%p'\n", arclog_path);
|
|
|
|
if (target_time)
|
|
|
|
fprintf(fp, "recovery_target_time = '%s'\n", target_time);
|
|
|
|
if (target_xid)
|
|
|
|
fprintf(fp, "recovery_target_xid = '%s'\n", target_xid);
|
|
|
|
if (target_inclusive)
|
|
|
|
fprintf(fp, "recovery_target_inclusive = '%s'\n", target_inclusive);
|
|
|
|
fprintf(fp, "recovery_target_timeline = '%u'\n", target_tli);
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
backup_online_files(bool re_recovery)
|
|
|
|
{
|
|
|
|
char work_path[MAXPGPATH];
|
|
|
|
char pg_xlog_path[MAXPGPATH];
|
|
|
|
bool files_exist;
|
|
|
|
parray *files;
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!check)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "----------------------------------------");
|
|
|
|
elog(LOG, "backup online WAL start");
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get list of files in $BACKUP_PATH/backup/pg_xlog */
|
|
|
|
files = parray_new();
|
|
|
|
snprintf(work_path, lengthof(work_path), "%s/%s/%s", backup_path,
|
|
|
|
RESTORE_WORK_DIR, PG_XLOG_DIR);
|
|
|
|
dir_list_file(files, work_path, NULL, true, false);
|
|
|
|
|
|
|
|
files_exist = parray_num(files) > 0;
|
|
|
|
|
|
|
|
parray_walk(files, pgFileFree);
|
|
|
|
parray_free(files);
|
|
|
|
|
|
|
|
/* If files exist in RESTORE_WORK_DIR and not re-recovery, use them. */
|
|
|
|
if (files_exist && !re_recovery)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "online WALs have been already backed up, use them");
|
2013-01-24 09:35:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* backup online WAL */
|
|
|
|
snprintf(pg_xlog_path, lengthof(pg_xlog_path), "%s/pg_xlog", pgdata);
|
|
|
|
snprintf(work_path, lengthof(work_path), "%s/%s/%s", backup_path,
|
|
|
|
RESTORE_WORK_DIR, PG_XLOG_DIR);
|
|
|
|
dir_create_dir(work_path, DIR_PERMISSION);
|
|
|
|
dir_copy_files(pg_xlog_path, work_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to read a timeline's history file.
|
|
|
|
*
|
|
|
|
* If successful, return the list of component pgTimeLine (the ancestor
|
2016-01-14 09:36:39 +02:00
|
|
|
* timelines followed by target timeline). If we cannot find the history file,
|
2013-01-24 09:35:48 +03:00
|
|
|
* assume that the timeline has no parents, and return a list of just the
|
|
|
|
* specified timeline ID.
|
|
|
|
* based on readTimeLineHistory() in xlog.c
|
|
|
|
*/
|
|
|
|
static parray *
|
|
|
|
readTimeLineHistory(TimeLineID targetTLI)
|
|
|
|
{
|
|
|
|
parray *result;
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char fline[MAXPGPATH];
|
|
|
|
FILE *fd;
|
|
|
|
pgTimeLine *timeline;
|
|
|
|
pgTimeLine *last_timeline = NULL;
|
|
|
|
|
|
|
|
result = parray_new();
|
|
|
|
|
|
|
|
/* search from arclog_path first */
|
|
|
|
snprintf(path, lengthof(path), "%s/%08X.history", arclog_path,
|
|
|
|
targetTLI);
|
|
|
|
fd = fopen(path, "rt");
|
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "could not open file \"%s\": %s", path,
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
/* search from restore work directory next */
|
|
|
|
snprintf(path, lengthof(path), "%s/%s/%s/%08X.history", backup_path,
|
|
|
|
RESTORE_WORK_DIR, PG_XLOG_DIR, targetTLI);
|
|
|
|
fd = fopen(path, "rt");
|
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "could not open file \"%s\": %s", path,
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the file...
|
|
|
|
*/
|
|
|
|
while (fd && fgets(fline, sizeof(fline), fd) != NULL)
|
|
|
|
{
|
|
|
|
char *ptr;
|
2013-12-10 11:27:51 +03:00
|
|
|
TimeLineID tli;
|
|
|
|
uint32 switchpoint_hi;
|
|
|
|
uint32 switchpoint_lo;
|
|
|
|
int nfields;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
for (ptr = fline; *ptr; ptr++)
|
|
|
|
{
|
2013-12-10 11:27:51 +03:00
|
|
|
if (!isspace((unsigned char) *ptr))
|
2013-01-24 09:35:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*ptr == '\0' || *ptr == '#')
|
|
|
|
continue;
|
|
|
|
|
2013-12-10 11:27:51 +03:00
|
|
|
/* Parse one entry... */
|
|
|
|
nfields = sscanf(fline, "%u\t%X/%X", &tli, &switchpoint_hi, &switchpoint_lo);
|
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
timeline = pgut_new(pgTimeLine);
|
|
|
|
timeline->tli = 0;
|
2013-12-09 21:21:07 +03:00
|
|
|
timeline->end = 0;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* expect a numeric timeline ID as first field of line */
|
2013-12-10 11:27:51 +03:00
|
|
|
timeline->tli = tli;
|
|
|
|
|
|
|
|
if (nfields < 1)
|
|
|
|
{
|
|
|
|
/* expect a numeric timeline ID as first field of line */
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"syntax error in history file: %s. Expected a numeric timeline ID.",
|
2013-12-10 11:27:51 +03:00
|
|
|
fline);
|
|
|
|
}
|
|
|
|
if (nfields != 3)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"syntax error in history file: %s. Expected a transaction log switchpoint location.",
|
2013-12-10 11:27:51 +03:00
|
|
|
fline);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
if (last_timeline && timeline->tli <= last_timeline->tli)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"Timeline IDs must be in increasing sequence.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* Build list with newest item first */
|
|
|
|
parray_insert(result, 0, timeline);
|
|
|
|
last_timeline = timeline;
|
|
|
|
|
2013-12-10 11:27:51 +03:00
|
|
|
/* Calculate the end lsn finally */
|
|
|
|
timeline->end = (XLogRecPtr)
|
|
|
|
((uint64) switchpoint_hi << 32) | switchpoint_lo;
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fd)
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
if (last_timeline && targetTLI <= last_timeline->tli)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"Timeline IDs must be less than child timeline's ID.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* append target timeline */
|
|
|
|
timeline = pgut_new(pgTimeLine);
|
|
|
|
timeline->tli = targetTLI;
|
2013-12-09 21:21:07 +03:00
|
|
|
/* lsn in target timeline is valid */
|
|
|
|
timeline->end = (uint32) (-1UL << 32) | -1UL;
|
2013-01-24 09:35:48 +03:00
|
|
|
parray_insert(result, 0, timeline);
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
/* dump timeline branches in verbose mode */
|
|
|
|
if (verbose)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
|
|
|
int i;
|
2016-01-14 09:36:39 +02:00
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
for (i = 0; i < parray_num(result); i++)
|
|
|
|
{
|
|
|
|
pgTimeLine *timeline = parray_get(result, i);
|
|
|
|
elog(LOG, "%s() result[%d]: %08X/%08X/%08X", __FUNCTION__, i,
|
2013-12-09 21:21:07 +03:00
|
|
|
timeline->tli,
|
|
|
|
(uint32) (timeline->end >> 32),
|
|
|
|
(uint32) timeline->end);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
satisfy_recovery_target(const pgBackup *backup, const pgRecoveryTarget *rt)
|
|
|
|
{
|
2013-12-15 18:30:49 +03:00
|
|
|
if (rt->xid_specified)
|
|
|
|
return backup->recovery_xid <= rt->recovery_target_xid;
|
2013-12-15 17:50:04 +03:00
|
|
|
|
|
|
|
if (rt->time_specified)
|
2013-12-15 18:30:49 +03:00
|
|
|
return backup->recovery_time <= rt->recovery_target_time;
|
|
|
|
|
|
|
|
return true;
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
satisfy_timeline(const parray *timelines, const pgBackup *backup)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < parray_num(timelines); i++)
|
|
|
|
{
|
|
|
|
pgTimeLine *timeline = (pgTimeLine *) parray_get(timelines, i);
|
|
|
|
if (backup->tli == timeline->tli &&
|
2013-12-09 21:21:07 +03:00
|
|
|
backup->stop_lsn < timeline->end)
|
2013-01-24 09:35:48 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get TLI of the latest full backup */
|
|
|
|
static TimeLineID
|
|
|
|
get_fullbackup_timeline(parray *backups, const pgRecoveryTarget *rt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
pgBackup *base_backup = NULL;
|
|
|
|
TimeLineID ret;
|
|
|
|
|
|
|
|
for (i = 0; i < parray_num(backups); i++)
|
|
|
|
{
|
|
|
|
base_backup = (pgBackup *) parray_get(backups, i);
|
|
|
|
|
|
|
|
if (base_backup->backup_mode >= BACKUP_MODE_FULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Validate backup files with its size, because load of CRC
|
|
|
|
* calculation is not right.
|
|
|
|
*/
|
|
|
|
if (base_backup->status == BACKUP_STATUS_DONE)
|
2013-12-15 18:30:49 +03:00
|
|
|
pgBackupValidate(base_backup, true, true);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
if (!satisfy_recovery_target(base_backup, rt))
|
2013-01-24 09:35:48 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (base_backup->status == BACKUP_STATUS_OK)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 09:36:39 +02:00
|
|
|
/* no full backup found, cannot restore */
|
2013-01-24 09:35:48 +03:00
|
|
|
if (i == parray_num(backups))
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "no full backup found, cannot restore.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
ret = base_backup->tli;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-14 09:36:39 +02:00
|
|
|
print_backup_lsn(const pgBackup *backup)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
|
|
|
char timestamp[100];
|
2016-01-14 09:36:39 +02:00
|
|
|
|
|
|
|
if (!verbose)
|
|
|
|
return;
|
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
time2iso(timestamp, lengthof(timestamp), backup->start_time);
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, " %s (%X/%08X)",
|
|
|
|
timestamp,
|
|
|
|
(uint32) (backup->stop_lsn >> 32),
|
|
|
|
(uint32) backup->stop_lsn);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-12 16:45:57 +03:00
|
|
|
search_next_wal(const char *path, XLogRecPtr *need_lsn, parray *timelines)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int count;
|
|
|
|
char xlogfname[MAXFNAMELEN];
|
|
|
|
char pre_xlogfname[MAXFNAMELEN];
|
|
|
|
char xlogpath[MAXPGPATH];
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
for (i = 0; i < parray_num(timelines); i++)
|
|
|
|
{
|
|
|
|
pgTimeLine *timeline = (pgTimeLine *) parray_get(timelines, i);
|
2016-01-13 09:28:51 +02:00
|
|
|
XLogSegNo targetSegNo;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-01-13 09:28:51 +02:00
|
|
|
XLByteToSeg(*need_lsn, targetSegNo);
|
|
|
|
XLogFileName(xlogfname, timeline->tli, targetSegNo);
|
2013-01-24 09:35:48 +03:00
|
|
|
join_path_components(xlogpath, path, xlogfname);
|
|
|
|
|
|
|
|
if (stat(xlogpath, &st) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
if (i == parray_num(timelines))
|
|
|
|
{
|
|
|
|
if (count == 1)
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "\n");
|
2013-01-24 09:35:48 +03:00
|
|
|
else if (count > 1)
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, " - %s", pre_xlogfname);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
if (count == 1)
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "%s", xlogfname);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
strcpy(pre_xlogfname, xlogfname);
|
|
|
|
|
|
|
|
/* delete old TLI */
|
|
|
|
for (j = i + 1; j < parray_num(timelines); j++)
|
|
|
|
parray_remove(timelines, i + 1);
|
|
|
|
/* XXX: should we add a linebreak when we find a timeline? */
|
|
|
|
|
2013-12-25 02:26:25 +03:00
|
|
|
/*
|
|
|
|
* Move to next xlog segment. Note that we need to increment
|
|
|
|
* by XLogSegSize to jump directly to the next WAL segment file
|
|
|
|
* and as this value is used to generate the WAL file name with
|
|
|
|
* the current timeline of backup.
|
|
|
|
*/
|
2013-12-25 02:17:01 +03:00
|
|
|
*need_lsn += XLogSegSize;
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pgRecoveryTarget *
|
|
|
|
checkIfCreateRecoveryConf(const char *target_time,
|
|
|
|
const char *target_xid,
|
|
|
|
const char *target_inclusive)
|
|
|
|
{
|
|
|
|
time_t dummy_time;
|
|
|
|
unsigned int dummy_xid;
|
|
|
|
bool dummy_bool;
|
|
|
|
pgRecoveryTarget *rt;
|
|
|
|
|
2013-12-15 17:50:04 +03:00
|
|
|
/* Initialize pgRecoveryTarget */
|
2013-01-24 09:35:48 +03:00
|
|
|
rt = pgut_new(pgRecoveryTarget);
|
|
|
|
rt->time_specified = false;
|
|
|
|
rt->xid_specified = false;
|
|
|
|
rt->recovery_target_time = 0;
|
|
|
|
rt->recovery_target_xid = 0;
|
|
|
|
rt->recovery_target_inclusive = false;
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
if (target_time)
|
|
|
|
{
|
2013-01-24 09:35:48 +03:00
|
|
|
rt->time_specified = true;
|
2016-01-14 09:36:39 +02:00
|
|
|
if (parse_time(target_time, &dummy_time))
|
2013-01-24 09:35:48 +03:00
|
|
|
rt->recovery_target_time = dummy_time;
|
|
|
|
else
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot create recovery.conf with %s", target_time);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
2016-01-14 09:36:39 +02:00
|
|
|
if (target_xid)
|
|
|
|
{
|
2013-01-24 09:35:48 +03:00
|
|
|
rt->xid_specified = true;
|
2016-01-14 09:36:39 +02:00
|
|
|
if (parse_uint32(target_xid, &dummy_xid))
|
2013-01-24 09:35:48 +03:00
|
|
|
rt->recovery_target_xid = dummy_xid;
|
|
|
|
else
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot create recovery.conf with %s", target_xid);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
2016-01-14 09:36:39 +02:00
|
|
|
if (target_inclusive)
|
|
|
|
{
|
|
|
|
if (parse_bool(target_inclusive, &dummy_bool))
|
2013-01-24 09:35:48 +03:00
|
|
|
rt->recovery_target_inclusive = dummy_bool;
|
|
|
|
else
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot create recovery.conf with %s", target_inclusive);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rt;
|
|
|
|
|
|
|
|
}
|