2012-05-18 11:54:36 +03:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2014-01-24 14:37:13 +03:00
|
|
|
* backup.c: backup DB cluster, archived WAL
|
2012-05-18 11:54:36 +03:00
|
|
|
*
|
2013-09-09 12:00:13 +03:00
|
|
|
* Copyright (c) 2009-2013, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
|
2012-05-18 11:54:36 +03:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2014-01-27 06:02:56 +03:00
|
|
|
#include "pg_arman.h"
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-04-15 14:58:58 +02:00
|
|
|
#include <string.h>
|
2012-05-18 11:54:36 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <time.h>
|
2016-02-29 19:23:48 +02:00
|
|
|
#include <pthread.h>
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
#include "libpq/pqsignal.h"
|
|
|
|
#include "pgut/pgut-port.h"
|
2016-02-27 20:07:55 +02:00
|
|
|
#include "storage/bufpage.h"
|
|
|
|
#include "datapagemap.h"
|
2016-05-26 14:56:32 +02:00
|
|
|
#include "streamutil.h"
|
|
|
|
#include "receivelog.h"
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2013-12-12 16:20:08 +03:00
|
|
|
/* wait 10 sec until WAL archive complete */
|
2016-05-26 14:56:32 +02:00
|
|
|
#define TIMEOUT_ARCHIVE 10
|
2013-12-12 16:20:08 +03:00
|
|
|
|
|
|
|
/* Server version */
|
|
|
|
static int server_version = 0;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-05-26 14:56:32 +02:00
|
|
|
static bool in_backup = false; /* TODO: more robust logic */
|
|
|
|
static int standby_message_timeout = 10 * 1000; /* 10 sec = default */
|
|
|
|
static XLogRecPtr stop_backup_lsn = InvalidXLogRecPtr;
|
|
|
|
const char *progname = "pg_arman";
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-01-15 16:47:38 +02:00
|
|
|
/* list of files contained in backup */
|
2016-09-02 19:16:00 +02:00
|
|
|
parray *backup_files_list;
|
|
|
|
static volatile uint32 total_copy_files_increment;
|
|
|
|
static uint32 total_files_num;
|
2016-09-29 16:33:21 +02:00
|
|
|
static PGconn *start_stop_connect = NULL;
|
2016-01-15 16:47:38 +02:00
|
|
|
|
2016-02-29 19:23:48 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *from_root;
|
|
|
|
const char *to_root;
|
|
|
|
parray *files;
|
|
|
|
parray *prev_files;
|
|
|
|
const XLogRecPtr *lsn;
|
|
|
|
} backup_files_args;
|
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
/*
|
|
|
|
* Backup routines
|
|
|
|
*/
|
|
|
|
static void backup_cleanup(bool fatal, void *userdata);
|
2016-02-29 19:23:48 +02:00
|
|
|
static void backup_files(void *arg);
|
2013-09-09 12:00:13 +03:00
|
|
|
static parray *do_backup_database(parray *backup_list, pgBackupOption bkupopt);
|
2012-05-18 11:54:36 +03:00
|
|
|
static void confirm_block_size(const char *name, int blcksz);
|
|
|
|
static void pg_start_backup(const char *label, bool smooth, pgBackup *backup);
|
|
|
|
static void pg_stop_backup(pgBackup *backup);
|
2013-12-26 15:13:48 +03:00
|
|
|
static bool pg_is_standby(void);
|
2016-09-29 16:33:21 +02:00
|
|
|
static void get_lsn(PGconn *conn, PGresult *res, XLogRecPtr *lsn, bool stop_backup);
|
2012-05-18 11:54:36 +03:00
|
|
|
static void get_xid(PGresult *res, uint32 *xid);
|
2016-02-27 20:07:55 +02:00
|
|
|
static void pg_ptrack_clear(void);
|
2016-05-11 18:35:14 +02:00
|
|
|
static char *pg_ptrack_get_and_clear(Oid tablespace_oid,
|
|
|
|
Oid db_oid,
|
|
|
|
Oid rel_oid,
|
|
|
|
size_t *result_size);
|
2012-05-18 11:54:36 +03:00
|
|
|
static void add_files(parray *files, const char *root, bool add_root, bool is_pgdata);
|
2014-01-13 07:04:43 +03:00
|
|
|
static void create_file_list(parray *files,
|
|
|
|
const char *root,
|
|
|
|
const char *subdir,
|
|
|
|
const char *prefix,
|
|
|
|
bool is_append);
|
2016-09-29 16:33:21 +02:00
|
|
|
static void wait_for_archive(PGconn *conn, pgBackup *backup, const char *sql, bool stop_backup);
|
2016-02-27 20:07:55 +02:00
|
|
|
static void make_pagemap_from_ptrack(parray *files);
|
2016-05-26 14:56:32 +02:00
|
|
|
static void StreamLog(void *arg);
|
|
|
|
|
|
|
|
|
|
|
|
#define disconnect_and_exit(code) \
|
|
|
|
{ \
|
|
|
|
if (conn != NULL) PQfinish(conn); \
|
|
|
|
exit(code); \
|
|
|
|
}
|
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/*
|
2013-12-15 17:41:42 +03:00
|
|
|
* Take a backup of database and return the list of files backed up.
|
2012-05-18 11:54:36 +03:00
|
|
|
*/
|
|
|
|
static parray *
|
2013-09-09 12:00:13 +03:00
|
|
|
do_backup_database(parray *backup_list, pgBackupOption bkupopt)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
parray *prev_files = NULL; /* file list of previous database backup */
|
|
|
|
FILE *fp;
|
|
|
|
char path[MAXPGPATH];
|
2016-05-26 14:56:32 +02:00
|
|
|
char dst_backup_path[MAXPGPATH];
|
2012-05-18 11:54:36 +03:00
|
|
|
char label[1024];
|
|
|
|
XLogRecPtr *lsn = NULL;
|
2013-12-15 17:41:42 +03:00
|
|
|
char prev_file_txt[MAXPGPATH]; /* path of the previous backup
|
|
|
|
* list file */
|
|
|
|
bool has_backup_label = true; /* flag if backup_label is there */
|
2016-02-29 19:23:48 +02:00
|
|
|
pthread_t backup_threads[num_threads];
|
2016-05-26 14:56:32 +02:00
|
|
|
pthread_t stream_thread;
|
2016-02-29 19:23:48 +02:00
|
|
|
backup_files_args *backup_threads_args[num_threads];
|
2013-09-09 12:00:13 +03:00
|
|
|
|
2016-09-02 18:31:49 +02:00
|
|
|
|
2013-09-09 12:00:13 +03:00
|
|
|
/* repack the options */
|
|
|
|
bool smooth_checkpoint = bkupopt.smooth_checkpoint;
|
2016-01-15 16:47:38 +02:00
|
|
|
pgBackup *prev_backup = NULL;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2013-12-26 15:13:48 +03:00
|
|
|
/* Block backup operations on a standby */
|
2016-09-29 16:33:21 +02:00
|
|
|
if (pg_is_standby() && !from_replica)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "Backup cannot run on a standby.");
|
2013-12-26 15:13:48 +03:00
|
|
|
|
2016-03-28 12:47:49 +02:00
|
|
|
elog(LOG, "database backup start");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2013-12-15 17:41:42 +03:00
|
|
|
/* Initialize size summary */
|
2014-01-09 22:11:27 +03:00
|
|
|
current.data_bytes = 0;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-01-15 16:47:38 +02:00
|
|
|
/* do some checks on the node */
|
|
|
|
sanityChecks();
|
|
|
|
|
2013-12-12 21:55:39 +03:00
|
|
|
/*
|
|
|
|
* Obtain current timeline by scanning control file, theh LSN
|
|
|
|
* obtained at output of pg_start_backup or pg_stop_backup does
|
|
|
|
* not contain this information.
|
|
|
|
*/
|
2016-03-02 19:50:33 +02:00
|
|
|
current.tli = get_current_timeline(false);
|
2013-12-12 21:55:39 +03:00
|
|
|
|
2013-12-24 23:27:25 +03:00
|
|
|
/*
|
2014-01-30 09:58:55 +03:00
|
|
|
* In differential backup mode, check if there is an already-validated
|
2013-12-24 23:27:25 +03:00
|
|
|
* full backup on current timeline.
|
|
|
|
*/
|
2016-02-27 20:07:55 +02:00
|
|
|
if (current.backup_mode == BACKUP_MODE_DIFF_PAGE ||
|
|
|
|
current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
|
2013-12-24 23:27:25 +03:00
|
|
|
{
|
|
|
|
pgBackup *prev_backup;
|
|
|
|
|
|
|
|
prev_backup = catalog_get_last_data_backup(backup_list, current.tli);
|
|
|
|
if (prev_backup == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "Valid full backup not found for "
|
2014-01-30 09:58:55 +03:00
|
|
|
"differential backup. Either create a full backup "
|
2016-01-14 09:36:39 +02:00
|
|
|
"or validate existing one.");
|
2013-12-24 23:27:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-26 14:56:32 +02:00
|
|
|
/* clear ptrack files for FULL and DIFF backup */
|
2016-09-01 17:29:13 +02:00
|
|
|
if (current.backup_mode != BACKUP_MODE_DIFF_PTRACK && !disable_ptrack_clear)
|
2016-05-11 20:49:26 +02:00
|
|
|
pg_ptrack_clear();
|
|
|
|
|
2016-05-26 14:56:32 +02:00
|
|
|
/* start stream replication */
|
|
|
|
if (stream_wal)
|
|
|
|
{
|
|
|
|
pgBackupGetPath(¤t, path, lengthof(path), DATABASE_DIR);
|
|
|
|
join_path_components(dst_backup_path, path, "pg_xlog");
|
|
|
|
dir_create_dir(dst_backup_path, DIR_PERMISSION);
|
|
|
|
pthread_create(&stream_thread, NULL, (void *(*)(void *)) StreamLog, dst_backup_path);
|
|
|
|
}
|
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
/* notify start of backup to PostgreSQL server */
|
|
|
|
time2iso(label, lengthof(label), current.start_time);
|
2014-01-27 06:02:56 +03:00
|
|
|
strncat(label, " with pg_arman", lengthof(label));
|
2012-05-18 11:54:36 +03:00
|
|
|
pg_start_backup(label, smooth_checkpoint, ¤t);
|
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
if(!from_replica)
|
2013-09-09 12:00:13 +03:00
|
|
|
{
|
2016-09-29 16:33:21 +02:00
|
|
|
/* If backup_label does not exist in $PGDATA, stop taking backup */
|
|
|
|
snprintf(path, lengthof(path), "%s/backup_label", pgdata);
|
|
|
|
make_native_path(path);
|
|
|
|
if (!fileExists(path))
|
|
|
|
has_backup_label = false;
|
|
|
|
|
|
|
|
/* Leave if no backup file */
|
|
|
|
if (!has_backup_label)
|
|
|
|
{
|
|
|
|
elog(LOG, "backup_label does not exist, stopping backup");
|
|
|
|
pg_stop_backup(NULL);
|
|
|
|
elog(ERROR, "backup_label does not exist in PGDATA.");
|
|
|
|
}
|
2013-09-09 12:00:13 +03:00
|
|
|
}
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/*
|
2013-12-15 17:41:42 +03:00
|
|
|
* List directories and symbolic links with the physical path to make
|
|
|
|
* mkdirs.sh, then sort them in order of path. Omit $PGDATA.
|
2012-05-18 11:54:36 +03:00
|
|
|
*/
|
2016-01-15 16:47:38 +02:00
|
|
|
backup_files_list = parray_new();
|
|
|
|
dir_list_file(backup_files_list, pgdata, NULL, false, false);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
if (!check)
|
|
|
|
{
|
|
|
|
pgBackupGetPath(¤t, path, lengthof(path), MKDIRS_SH_FILE);
|
|
|
|
fp = fopen(path, "wt");
|
|
|
|
if (fp == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "can't open make directory script \"%s\": %s",
|
2012-05-18 11:54:36 +03:00
|
|
|
path, strerror(errno));
|
2016-01-15 16:47:38 +02:00
|
|
|
dir_print_mkdirs_sh(fp, backup_files_list, pgdata);
|
2012-05-18 11:54:36 +03:00
|
|
|
fclose(fp);
|
|
|
|
if (chmod(path, DIR_PERMISSION) == -1)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "can't change mode of \"%s\": %s", path,
|
2012-05-18 11:54:36 +03:00
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear directory list */
|
2016-01-15 16:47:38 +02:00
|
|
|
parray_walk(backup_files_list, pgFileFree);
|
|
|
|
parray_free(backup_files_list);
|
|
|
|
backup_files_list = NULL;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/*
|
2014-01-30 09:58:55 +03:00
|
|
|
* To take differential backup, the file list of the last completed database
|
2012-05-18 11:54:36 +03:00
|
|
|
* backup is needed.
|
|
|
|
*/
|
2016-02-27 20:07:55 +02:00
|
|
|
if (current.backup_mode == BACKUP_MODE_DIFF_PAGE ||
|
|
|
|
current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
/* find last completed database backup */
|
2013-12-24 23:27:25 +03:00
|
|
|
prev_backup = catalog_get_last_data_backup(backup_list, current.tli);
|
|
|
|
pgBackupGetPath(prev_backup, prev_file_txt, lengthof(prev_file_txt),
|
|
|
|
DATABASE_FILE_LIST);
|
|
|
|
prev_files = dir_read_file_list(pgdata, prev_file_txt);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2013-12-24 23:27:25 +03:00
|
|
|
/*
|
|
|
|
* Do backup only pages having larger LSN than previous backup.
|
|
|
|
*/
|
|
|
|
lsn = &prev_backup->start_lsn;
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "backup only the page that there was of the update from LSN(%X/%08X)",
|
2013-12-24 23:27:25 +03:00
|
|
|
(uint32) (*lsn >> 32), (uint32) *lsn);
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
|
2016-01-15 16:47:38 +02:00
|
|
|
/* initialize backup list */
|
|
|
|
backup_files_list = parray_new();
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-01-15 08:26:05 +02:00
|
|
|
/* list files with the logical path. omit $PGDATA */
|
2016-01-15 16:47:38 +02:00
|
|
|
add_files(backup_files_list, pgdata, false, true);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-01-15 08:26:05 +02:00
|
|
|
/* backup files */
|
|
|
|
pgBackupGetPath(¤t, path, lengthof(path), DATABASE_DIR);
|
2016-01-15 16:47:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Build page mapping in differential mode. When using this mode, the
|
|
|
|
* list of blocks to be taken is known by scanning the WAL segments
|
|
|
|
* present in archives up to the point where start backup has begun.
|
|
|
|
* However, normally this segment is not yet available in the archives,
|
|
|
|
* leading to failures when building the page map. Hence before doing
|
|
|
|
* anything and in order to ensure that all the segments needed for the
|
|
|
|
* scan are here, for a switch of the last segment with pg_switch_xlog.
|
|
|
|
*/
|
|
|
|
if (current.backup_mode == BACKUP_MODE_DIFF_PAGE)
|
|
|
|
{
|
|
|
|
/* Enforce archiving of last segment and wait for it to be here */
|
2016-09-29 16:33:21 +02:00
|
|
|
wait_for_archive(connection, ¤t, "SELECT * FROM pg_switch_xlog()", false);
|
2016-01-15 16:47:38 +02:00
|
|
|
|
|
|
|
/* Now build the page map */
|
|
|
|
parray_qsort(backup_files_list, pgFileComparePathDesc);
|
|
|
|
elog(LOG, "extractPageMap");
|
|
|
|
elog(LOG, "current_tli:%X", current.tli);
|
|
|
|
elog(LOG, "prev_backup->start_lsn: %X/%X",
|
|
|
|
(uint32) (prev_backup->start_lsn >> 32),
|
|
|
|
(uint32) (prev_backup->start_lsn));
|
|
|
|
elog(LOG, "current.start_lsn: %X/%X",
|
|
|
|
(uint32) (current.start_lsn >> 32),
|
|
|
|
(uint32) (current.start_lsn));
|
|
|
|
extractPageMap(arclog_path, prev_backup->start_lsn, current.tli,
|
|
|
|
current.start_lsn);
|
|
|
|
}
|
|
|
|
|
2016-02-27 20:07:55 +02:00
|
|
|
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
|
|
|
|
{
|
2016-03-10 16:40:33 +02:00
|
|
|
XLogRecPtr ptrack_lsn = get_last_ptrack_lsn();
|
2016-05-12 11:51:36 +02:00
|
|
|
if (ptrack_lsn > prev_backup->stop_lsn)
|
2016-03-10 16:40:33 +02:00
|
|
|
elog(ERROR, "Wrong ptrack lsn:%lx prev:%lx current:%lx",
|
|
|
|
ptrack_lsn,
|
|
|
|
prev_backup->start_lsn,
|
|
|
|
current.start_lsn);
|
2016-02-27 20:07:55 +02:00
|
|
|
parray_qsort(backup_files_list, pgFileComparePathDesc);
|
|
|
|
make_pagemap_from_ptrack(backup_files_list);
|
|
|
|
}
|
|
|
|
|
2016-02-29 19:23:48 +02:00
|
|
|
/* sort pathname ascending */
|
|
|
|
parray_qsort(backup_files_list, pgFileComparePath);
|
|
|
|
|
|
|
|
/* make dirs before backup */
|
|
|
|
for (i = 0; i < parray_num(backup_files_list); i++)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat buf;
|
|
|
|
pgFile *file = (pgFile *) parray_get(backup_files_list, i);
|
|
|
|
|
|
|
|
ret = stat(file->path, &buf);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
|
|
|
{
|
|
|
|
/* record as skipped file in file_xxx.txt */
|
|
|
|
file->write_size = BYTES_INVALID;
|
|
|
|
elog(LOG, "skip");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elog(ERROR,
|
|
|
|
"can't stat backup mode. \"%s\": %s",
|
|
|
|
file->path, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if the entry was a directory, create it in the backup */
|
|
|
|
if (S_ISDIR(buf.st_mode))
|
|
|
|
{
|
|
|
|
char dirpath[MAXPGPATH];
|
|
|
|
if (verbose)
|
|
|
|
elog(LOG, "Make dir %s", file->path + strlen(pgdata) + 1);
|
|
|
|
join_path_components(dirpath, path, JoinPathEnd(file->path, pgdata));
|
|
|
|
if (!check)
|
|
|
|
dir_create_dir(dirpath, DIR_PERMISSION);
|
|
|
|
}
|
2016-09-02 19:16:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
total_files_num++;
|
|
|
|
}
|
2016-09-02 19:38:39 +02:00
|
|
|
|
|
|
|
__sync_lock_release(&file->lock);
|
2016-02-29 19:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num_threads < 1)
|
|
|
|
num_threads = 1;
|
|
|
|
|
2016-09-02 18:31:49 +02:00
|
|
|
/* sort by size for load balancing */
|
|
|
|
parray_qsort(backup_files_list, pgFileCompareSize);
|
|
|
|
|
|
|
|
/* init thread args with own file lists */
|
2016-02-29 19:23:48 +02:00
|
|
|
for (i = 0; i < num_threads; i++)
|
|
|
|
{
|
|
|
|
backup_files_args *arg = pg_malloc(sizeof(backup_files_args));
|
|
|
|
arg->from_root = pgdata;
|
|
|
|
arg->to_root = path;
|
2016-09-02 19:38:39 +02:00
|
|
|
arg->files = backup_files_list;
|
2016-02-29 19:23:48 +02:00
|
|
|
arg->prev_files = prev_files;
|
|
|
|
arg->lsn = lsn;
|
2016-09-02 18:31:49 +02:00
|
|
|
backup_threads_args[i] = arg;
|
|
|
|
}
|
|
|
|
|
2016-09-02 19:16:00 +02:00
|
|
|
total_copy_files_increment = 0;
|
|
|
|
|
2016-09-02 18:31:49 +02:00
|
|
|
/* Run threads */
|
|
|
|
for (i = 0; i < num_threads; i++)
|
|
|
|
{
|
2016-02-29 19:23:48 +02:00
|
|
|
if (verbose)
|
2016-09-02 18:31:49 +02:00
|
|
|
elog(WARNING, "Start thread num:%li", parray_num(backup_threads_args[i]->files));
|
|
|
|
pthread_create(&backup_threads[i], NULL, (void *(*)(void *)) backup_files, backup_threads_args[i]);
|
2016-02-29 19:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait theads */
|
|
|
|
for (i = 0; i < num_threads; i++)
|
|
|
|
{
|
|
|
|
pthread_join(backup_threads[i], NULL);
|
|
|
|
pg_free(backup_threads_args[i]);
|
|
|
|
}
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-09-02 19:16:00 +02:00
|
|
|
if (progress)
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2016-02-27 20:07:55 +02:00
|
|
|
/* Notify end of backup */
|
2016-01-15 08:26:05 +02:00
|
|
|
pg_stop_backup(¤t);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-05-26 14:56:32 +02:00
|
|
|
if (stream_wal)
|
|
|
|
{
|
|
|
|
parray *list_file;
|
|
|
|
char pg_xlog_path[MAXPGPATH];
|
|
|
|
|
|
|
|
/* We expect the completion of stream */
|
|
|
|
pthread_join(stream_thread, NULL);
|
|
|
|
|
|
|
|
/* Scan backup pg_xlog dir */
|
|
|
|
list_file = parray_new();
|
|
|
|
join_path_components(pg_xlog_path, path, "pg_xlog");
|
|
|
|
dir_list_file(list_file, pg_xlog_path, NULL, true, false);
|
|
|
|
|
|
|
|
/* Remove file path root prefix and calc meta */
|
|
|
|
for (i = 0; i < parray_num(list_file); i++)
|
|
|
|
{
|
|
|
|
pgFile *file = (pgFile *)parray_get(list_file, i);
|
|
|
|
|
|
|
|
calc_file(file);
|
|
|
|
if (strstr(file->path, path) == file->path)
|
|
|
|
{
|
|
|
|
char *ptr = file->path;
|
|
|
|
file->path = pstrdup(JoinPathEnd(ptr, path));
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parray_concat(backup_files_list, list_file);
|
|
|
|
}
|
|
|
|
|
2016-02-27 20:07:55 +02:00
|
|
|
/* Create file list */
|
2016-01-15 16:47:38 +02:00
|
|
|
create_file_list(backup_files_list, pgdata, DATABASE_FILE_LIST, NULL, false);
|
2013-12-09 21:21:07 +03:00
|
|
|
|
2016-02-27 20:07:55 +02:00
|
|
|
/* Print summary of size of backup mode files */
|
2016-01-15 16:47:38 +02:00
|
|
|
for (i = 0; i < parray_num(backup_files_list); i++)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
2016-01-15 16:47:38 +02:00
|
|
|
pgFile *file = (pgFile *) parray_get(backup_files_list, i);
|
2012-05-18 11:54:36 +03:00
|
|
|
if (!S_ISREG(file->mode))
|
|
|
|
continue;
|
2014-01-09 22:11:27 +03:00
|
|
|
/*
|
|
|
|
* Count only the amount of data. For a full backup, the total
|
2014-01-30 09:58:55 +03:00
|
|
|
* amount of data written counts while for an differential
|
2014-01-09 22:11:27 +03:00
|
|
|
* backup only the data read counts.
|
|
|
|
*/
|
2016-02-27 20:07:55 +02:00
|
|
|
if (current.backup_mode == BACKUP_MODE_DIFF_PAGE ||
|
|
|
|
current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
|
2016-03-02 15:16:50 +02:00
|
|
|
current.data_bytes += file->write_size;
|
2014-01-09 22:11:27 +03:00
|
|
|
else if (current.backup_mode == BACKUP_MODE_FULL)
|
|
|
|
current.data_bytes += file->size;
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "database backup completed(Backup: " INT64_FORMAT ")",
|
|
|
|
current.data_bytes);
|
|
|
|
elog(LOG, "========================================");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-01-15 16:47:38 +02:00
|
|
|
return backup_files_list;
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
|
2013-09-09 12:00:13 +03:00
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
int
|
2013-09-09 12:00:13 +03:00
|
|
|
do_backup(pgBackupOption bkupopt)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
parray *backup_list;
|
|
|
|
parray *files_database;
|
|
|
|
int ret;
|
|
|
|
|
2014-01-24 16:36:31 +03:00
|
|
|
/* repack the necessary options */
|
2016-02-29 19:23:48 +02:00
|
|
|
int keep_data_generations = bkupopt.keep_data_generations;
|
|
|
|
int keep_data_days = bkupopt.keep_data_days;
|
2013-09-09 12:00:13 +03:00
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
/* PGDATA and BACKUP_MODE are always required */
|
|
|
|
if (pgdata == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "Required parameter not specified: PGDATA "
|
2016-01-14 09:36:39 +02:00
|
|
|
"(-D, --pgdata)");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2013-12-15 18:30:49 +03:00
|
|
|
/* A backup mode is needed */
|
2012-05-18 11:54:36 +03:00
|
|
|
if (current.backup_mode == BACKUP_MODE_INVALID)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "Required parameter not specified: BACKUP_MODE "
|
2016-01-14 09:36:39 +02:00
|
|
|
"(-b, --backup-mode)");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2013-12-15 18:30:49 +03:00
|
|
|
/* Confirm data block size and xlog block size are compatible */
|
2013-12-12 16:20:08 +03:00
|
|
|
check_server_version();
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* setup cleanup callback function */
|
|
|
|
in_backup = true;
|
|
|
|
|
|
|
|
/* show configuration actually used */
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "========================================");
|
|
|
|
elog(LOG, "backup start");
|
|
|
|
elog(LOG, "----------------------------------------");
|
2012-05-18 11:54:36 +03:00
|
|
|
if (verbose)
|
|
|
|
pgBackupWriteConfigSection(stderr, ¤t);
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "----------------------------------------");
|
2012-05-18 11:54:36 +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");
|
2012-05-18 11:54:36 +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, skipping this backup");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* initialize backup result */
|
|
|
|
current.status = BACKUP_STATUS_RUNNING;
|
|
|
|
current.tli = 0; /* get from result of pg_start_backup() */
|
2013-12-09 21:21:07 +03:00
|
|
|
current.start_lsn = 0;
|
|
|
|
current.stop_lsn = 0;
|
2012-05-18 11:54:36 +03:00
|
|
|
current.start_time = time(NULL);
|
|
|
|
current.end_time = (time_t) 0;
|
2014-01-09 22:11:27 +03:00
|
|
|
current.data_bytes = BYTES_INVALID;
|
2012-05-18 11:54:36 +03:00
|
|
|
current.block_size = BLCKSZ;
|
|
|
|
current.wal_block_size = XLOG_BLCKSZ;
|
2013-09-09 12:00:13 +03:00
|
|
|
current.recovery_xid = 0;
|
|
|
|
current.recovery_time = (time_t) 0;
|
2016-07-07 17:38:56 +02:00
|
|
|
current.checksum_version = get_data_checksum_version(true);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* create backup directory and backup.ini */
|
|
|
|
if (!check)
|
|
|
|
{
|
|
|
|
if (pgBackupCreateDir(¤t))
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot create backup directory");
|
2012-05-18 11:54:36 +03:00
|
|
|
pgBackupWriteIni(¤t);
|
|
|
|
}
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "backup destination is initialized");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* get list of backups already taken */
|
|
|
|
backup_list = catalog_get_backup_list(NULL);
|
2014-01-17 16:46:39 +03:00
|
|
|
if (!backup_list)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot process any more");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* set the error processing function for the backup process */
|
|
|
|
pgut_atexit_push(backup_cleanup, NULL);
|
|
|
|
|
|
|
|
/* backup data */
|
2013-09-09 12:00:13 +03:00
|
|
|
files_database = do_backup_database(backup_list, bkupopt);
|
2012-05-18 11:54:36 +03:00
|
|
|
pgut_atexit_pop(backup_cleanup, NULL);
|
2013-12-09 21:21:07 +03:00
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
/* update backup status to DONE */
|
|
|
|
current.end_time = time(NULL);
|
|
|
|
current.status = BACKUP_STATUS_DONE;
|
|
|
|
if (!check)
|
|
|
|
pgBackupWriteIni(¤t);
|
|
|
|
|
2013-12-15 18:30:49 +03:00
|
|
|
/* Calculate the total data read */
|
2012-05-18 11:54:36 +03:00
|
|
|
if (verbose)
|
|
|
|
{
|
2013-12-15 18:30:49 +03:00
|
|
|
int64 total_read = 0;
|
|
|
|
|
|
|
|
/* Database data */
|
|
|
|
if (current.backup_mode == BACKUP_MODE_FULL ||
|
2016-02-27 20:07:55 +02:00
|
|
|
current.backup_mode == BACKUP_MODE_DIFF_PAGE ||
|
|
|
|
current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
|
2014-01-24 16:36:31 +03:00
|
|
|
total_read += current.data_bytes;
|
2013-12-15 18:30:49 +03:00
|
|
|
|
|
|
|
if (total_read == 0)
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "nothing to backup");
|
2012-05-18 11:54:36 +03:00
|
|
|
else
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "all backup completed(read: " INT64_FORMAT " write: "
|
|
|
|
INT64_FORMAT ")",
|
|
|
|
total_read, current.data_bytes);
|
|
|
|
elog(LOG, "========================================");
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Delete old backup files after all backup operation. */
|
|
|
|
pgBackupDelete(keep_data_generations, keep_data_days);
|
|
|
|
|
|
|
|
/* Cleanup backup mode file list */
|
|
|
|
if (files_database)
|
|
|
|
parray_walk(files_database, pgFileFree);
|
|
|
|
parray_free(files_database);
|
|
|
|
|
|
|
|
/* release catalog lock */
|
|
|
|
catalog_unlock();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get server version and confirm block sizes.
|
|
|
|
*/
|
2013-12-12 16:20:08 +03:00
|
|
|
void
|
|
|
|
check_server_version(void)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
bool my_conn;
|
|
|
|
|
2013-12-12 16:20:08 +03:00
|
|
|
/* Leave if server has already been checked */
|
2012-05-18 11:54:36 +03:00
|
|
|
if (server_version > 0)
|
2013-12-12 16:20:08 +03:00
|
|
|
return;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
my_conn = (connection == NULL);
|
|
|
|
|
|
|
|
if (my_conn)
|
|
|
|
reconnect();
|
|
|
|
|
|
|
|
/* confirm server version */
|
|
|
|
server_version = PQserverVersion(connection);
|
2016-03-02 11:11:25 +02:00
|
|
|
if (server_version < 90500)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"server version is %d.%d.%d, must be %s or higher.",
|
2013-12-12 16:20:08 +03:00
|
|
|
server_version / 10000,
|
|
|
|
(server_version / 100) % 100,
|
2016-03-02 11:11:25 +02:00
|
|
|
server_version % 100, "9.5");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
if (from_replica && server_version < 90600)
|
|
|
|
elog(ERROR,
|
|
|
|
"server version is %d.%d.%d, must be %s or higher for backup from replica.",
|
|
|
|
server_version / 10000,
|
|
|
|
(server_version / 100) % 100,
|
|
|
|
server_version % 100, "9.6");
|
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
/* confirm block_size (BLCKSZ) and wal_block_size (XLOG_BLCKSZ) */
|
|
|
|
confirm_block_size("block_size", BLCKSZ);
|
2013-12-12 16:20:08 +03:00
|
|
|
confirm_block_size("wal_block_size", XLOG_BLCKSZ);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
if (my_conn)
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
confirm_block_size(const char *name, int blcksz)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
char *endp;
|
|
|
|
int block_size;
|
|
|
|
|
|
|
|
res = execute("SELECT current_setting($1)", 1, &name);
|
|
|
|
if (PQntuples(res) != 1 || PQnfields(res) != 1)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "cannot get %s: %s",
|
2012-05-18 11:54:36 +03:00
|
|
|
name, PQerrorMessage(connection));
|
|
|
|
block_size = strtol(PQgetvalue(res, 0, 0), &endp, 10);
|
|
|
|
PQclear(res);
|
|
|
|
if ((endp && *endp) || block_size != blcksz)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"%s(%d) is not compatible(%d expected)",
|
2012-05-18 11:54:36 +03:00
|
|
|
name, block_size, blcksz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify start of backup to PostgreSQL server.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pg_start_backup(const char *label, bool smooth, pgBackup *backup)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
const char *params[2];
|
|
|
|
|
|
|
|
params[0] = label;
|
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
if (start_stop_connect == NULL)
|
|
|
|
start_stop_connect = pgut_connect(ERROR);
|
2013-12-12 16:20:08 +03:00
|
|
|
|
|
|
|
/* 2nd argument is 'fast'*/
|
|
|
|
params[1] = smooth ? "false" : "true";
|
2016-09-29 16:33:21 +02:00
|
|
|
if (from_replica)
|
|
|
|
res = pgut_execute(start_stop_connect,
|
|
|
|
"SELECT pg_start_backup($1, $2, false)",
|
|
|
|
2,
|
|
|
|
params,
|
|
|
|
ERROR);
|
|
|
|
else
|
|
|
|
res = pgut_execute(start_stop_connect,
|
|
|
|
"SELECT pg_start_backup($1, $2)",
|
|
|
|
2,
|
|
|
|
params,
|
|
|
|
ERROR);
|
2013-12-12 16:20:08 +03:00
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
if (backup != NULL)
|
2016-09-29 16:33:21 +02:00
|
|
|
get_lsn(start_stop_connect, res, &backup->start_lsn, false);
|
2012-05-18 11:54:36 +03:00
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
|
2016-02-27 20:07:55 +02:00
|
|
|
static void
|
|
|
|
pg_ptrack_clear(void)
|
|
|
|
{
|
2016-04-15 14:58:58 +02:00
|
|
|
PGresult *res_db, *res;
|
2016-05-26 14:56:32 +02:00
|
|
|
const char *old_dbname = pgut_dbname;
|
2016-04-15 14:58:58 +02:00
|
|
|
int i;
|
2016-02-27 20:07:55 +02:00
|
|
|
|
|
|
|
reconnect();
|
2016-04-15 14:58:58 +02:00
|
|
|
res_db = execute("SELECT datname FROM pg_database", 0, NULL);
|
|
|
|
disconnect();
|
|
|
|
for(i=0; i < PQntuples(res_db); i++)
|
|
|
|
{
|
2016-05-26 14:56:32 +02:00
|
|
|
pgut_dbname = PQgetvalue(res_db, i, 0);
|
|
|
|
if (!strcmp(pgut_dbname, "template0"))
|
2016-04-15 14:58:58 +02:00
|
|
|
continue;
|
|
|
|
reconnect();
|
|
|
|
res = execute("SELECT pg_ptrack_clear()", 0, NULL);
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
PQclear(res_db);
|
2016-02-27 20:07:55 +02:00
|
|
|
disconnect();
|
2016-05-26 14:56:32 +02:00
|
|
|
pgut_dbname = old_dbname;
|
2016-02-27 20:07:55 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 18:35:14 +02:00
|
|
|
static char *
|
|
|
|
pg_ptrack_get_and_clear(Oid tablespace_oid, Oid db_oid, Oid rel_oid, size_t *result_size)
|
|
|
|
{
|
|
|
|
PGresult *res_db, *res;
|
2016-05-26 14:56:32 +02:00
|
|
|
const char *old_dbname = pgut_dbname;
|
2016-05-11 18:35:14 +02:00
|
|
|
char *params[2];
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
reconnect();
|
|
|
|
params[0] = palloc(64);
|
|
|
|
params[1] = palloc(64);
|
|
|
|
sprintf(params[0], "%i", db_oid);
|
|
|
|
sprintf(params[1], "%i", rel_oid);
|
|
|
|
res_db = execute("SELECT datname FROM pg_database WHERE oid=$1", 1, (const char **)params);
|
|
|
|
disconnect();
|
2016-05-26 14:56:32 +02:00
|
|
|
pgut_dbname = pstrdup(PQgetvalue(res_db, 0, 0));
|
2016-05-11 18:35:14 +02:00
|
|
|
PQclear(res_db);
|
|
|
|
|
|
|
|
reconnect();
|
|
|
|
sprintf(params[0], "%i", tablespace_oid);
|
|
|
|
res = execute("SELECT pg_ptrack_get_and_clear($1, $2)", 2, (const char **)params);
|
|
|
|
result = (char *)PQunescapeBytea((unsigned char *)PQgetvalue(res, 0, 0), result_size);
|
|
|
|
PQclear(res);
|
|
|
|
pfree(params[0]);
|
|
|
|
pfree(params[1]);
|
|
|
|
|
2016-05-26 14:56:32 +02:00
|
|
|
pfree((char *)pgut_dbname);
|
|
|
|
pgut_dbname = old_dbname;
|
2016-05-11 18:35:14 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
static void
|
2016-09-29 16:33:21 +02:00
|
|
|
wait_for_archive(PGconn *conn, pgBackup *backup, const char *sql, bool stop_backup)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
char ready_path[MAXPGPATH];
|
2013-12-12 21:55:39 +03:00
|
|
|
char file_name[MAXFNAMELEN];
|
2012-05-18 11:54:36 +03:00
|
|
|
int try_count;
|
2013-12-12 21:55:39 +03:00
|
|
|
XLogRecPtr lsn;
|
|
|
|
TimeLineID tli;
|
2016-01-13 09:28:51 +02:00
|
|
|
XLogSegNo targetSegNo;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
if (conn == NULL)
|
|
|
|
conn = pgut_connect(ERROR);
|
2016-03-28 12:47:49 +02:00
|
|
|
|
|
|
|
/* Remove annoying NOTICE messages generated by backend */
|
2016-09-29 16:33:21 +02:00
|
|
|
res = pgut_execute(conn, "SET client_min_messages = warning;", 0, NULL, ERROR);
|
2016-03-28 12:47:49 +02:00
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/* And execute the query wanted */
|
2016-09-29 16:33:21 +02:00
|
|
|
res = pgut_execute(conn, sql, 0, NULL, ERROR);
|
2013-12-12 21:55:39 +03:00
|
|
|
|
|
|
|
/* Get LSN from execution result */
|
2016-09-29 16:33:21 +02:00
|
|
|
get_lsn(conn, res, &lsn, stop_backup);
|
2013-12-12 21:55:39 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Enforce TLI obtention if backup is not present as this code
|
|
|
|
* path can be taken as a callback at exit.
|
|
|
|
*/
|
2016-03-02 19:50:33 +02:00
|
|
|
tli = get_current_timeline(false);
|
2013-12-12 21:55:39 +03:00
|
|
|
|
|
|
|
/* Fill in fields if backup exists */
|
2012-05-18 11:54:36 +03:00
|
|
|
if (backup != NULL)
|
|
|
|
{
|
2013-12-24 22:36:07 +03:00
|
|
|
backup->tli = tli;
|
2013-12-12 21:55:39 +03:00
|
|
|
backup->stop_lsn = lsn;
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "%s(): tli=%X lsn=%X/%08X",
|
2013-12-09 21:21:07 +03:00
|
|
|
__FUNCTION__, backup->tli,
|
|
|
|
(uint32) (backup->stop_lsn >> 32),
|
|
|
|
(uint32) backup->stop_lsn);
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:55:39 +03:00
|
|
|
/* As well as WAL file name */
|
2016-01-13 09:28:51 +02:00
|
|
|
XLByteToSeg(lsn, targetSegNo);
|
2016-01-15 06:37:20 +02:00
|
|
|
XLogFileName(file_name, tli, targetSegNo);
|
2013-12-12 21:55:39 +03:00
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
snprintf(ready_path, lengthof(ready_path),
|
2013-12-12 21:55:39 +03:00
|
|
|
"%s/pg_xlog/archive_status/%s.ready", pgdata,
|
|
|
|
file_name);
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "%s() wait for %s", __FUNCTION__, ready_path);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
if (from_replica)
|
|
|
|
res = pgut_execute(conn, TXID_CURRENT_IF_SQL, 0, NULL, ERROR);
|
|
|
|
else
|
|
|
|
res = pgut_execute(conn, TXID_CURRENT_SQL, 0, NULL, ERROR);
|
2016-01-14 09:36:39 +02:00
|
|
|
if (backup != NULL)
|
|
|
|
{
|
2012-05-18 11:54:36 +03:00
|
|
|
get_xid(res, &backup->recovery_xid);
|
|
|
|
backup->recovery_time = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait until switched WAL is archived */
|
|
|
|
try_count = 0;
|
|
|
|
while (fileExists(ready_path))
|
|
|
|
{
|
|
|
|
sleep(1);
|
|
|
|
if (interrupted)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"interrupted during waiting for WAL archiving");
|
2012-05-18 11:54:36 +03:00
|
|
|
try_count++;
|
|
|
|
if (try_count > TIMEOUT_ARCHIVE)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"switched WAL could not be archived in %d seconds",
|
2012-05-18 11:54:36 +03:00
|
|
|
TIMEOUT_ARCHIVE);
|
|
|
|
}
|
|
|
|
elog(LOG, "%s() .ready deleted in %d try", __FUNCTION__, try_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify end of backup to PostgreSQL server.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pg_stop_backup(pgBackup *backup)
|
|
|
|
{
|
2016-05-26 14:56:32 +02:00
|
|
|
if (stream_wal)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
TimeLineID tli;
|
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
//reconnect();
|
|
|
|
if (start_stop_connect == NULL)
|
|
|
|
start_stop_connect = pgut_connect(ERROR);
|
2016-05-26 14:56:32 +02:00
|
|
|
|
|
|
|
/* Remove annoying NOTICE messages generated by backend */
|
2016-09-29 16:33:21 +02:00
|
|
|
res = pgut_execute(start_stop_connect, "SET client_min_messages = warning;", 0, NULL, ERROR);
|
2016-05-26 14:56:32 +02:00
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/* And execute the query wanted */
|
2016-09-29 16:33:21 +02:00
|
|
|
if (from_replica)
|
|
|
|
res = pgut_execute(start_stop_connect,"SELECT * FROM pg_stop_backup(false)", 0, NULL, ERROR);
|
|
|
|
else
|
|
|
|
res = pgut_execute(start_stop_connect,"SELECT * FROM pg_stop_backup()", 0, NULL, ERROR);
|
2016-05-26 14:56:32 +02:00
|
|
|
|
|
|
|
/* Get LSN from execution result */
|
2016-09-29 16:33:21 +02:00
|
|
|
get_lsn(start_stop_connect, res, &stop_backup_lsn, true);
|
2016-05-26 14:56:32 +02:00
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enforce TLI obtention if backup is not present as this code
|
|
|
|
* path can be taken as a callback at exit.
|
|
|
|
*/
|
|
|
|
tli = get_current_timeline(false);
|
|
|
|
|
|
|
|
/* Fill in fields if backup exists */
|
|
|
|
if (backup != NULL)
|
|
|
|
{
|
|
|
|
backup->tli = tli;
|
|
|
|
backup->stop_lsn = stop_backup_lsn;
|
|
|
|
elog(LOG, "%s(): tli=%X lsn=%X/%08X",
|
|
|
|
__FUNCTION__, backup->tli,
|
|
|
|
(uint32) (backup->stop_lsn >> 32),
|
|
|
|
(uint32) backup->stop_lsn);
|
|
|
|
}
|
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
if (from_replica)
|
|
|
|
res = pgut_execute(start_stop_connect, TXID_CURRENT_IF_SQL, 0, NULL, ERROR);
|
|
|
|
else
|
|
|
|
res = pgut_execute(start_stop_connect, TXID_CURRENT_SQL, 0, NULL, ERROR);
|
2016-05-26 14:56:32 +02:00
|
|
|
if (backup != NULL)
|
|
|
|
{
|
|
|
|
get_xid(res, &backup->recovery_xid);
|
|
|
|
backup->recovery_time = time(NULL);
|
|
|
|
}
|
|
|
|
PQclear(res);
|
2016-09-29 16:33:21 +02:00
|
|
|
//disconnect();
|
|
|
|
pgut_disconnect(start_stop_connect);
|
2016-05-26 14:56:32 +02:00
|
|
|
}
|
|
|
|
else
|
2016-09-29 16:33:21 +02:00
|
|
|
{
|
|
|
|
if (from_replica)
|
|
|
|
wait_for_archive(start_stop_connect,
|
|
|
|
backup,
|
|
|
|
"SELECT * FROM pg_stop_backup(false)",
|
|
|
|
true);
|
|
|
|
else
|
|
|
|
wait_for_archive(start_stop_connect,
|
|
|
|
backup,
|
|
|
|
"SELECT * FROM pg_stop_backup()",
|
|
|
|
true);
|
|
|
|
pgut_disconnect(start_stop_connect);
|
|
|
|
}
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-26 15:13:48 +03:00
|
|
|
/*
|
|
|
|
* Check if node is a standby by looking at the presence of
|
|
|
|
* recovery.conf.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
pg_is_standby(void)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
snprintf(path, lengthof(path), "%s/recovery.conf", pgdata);
|
|
|
|
make_native_path(path);
|
|
|
|
return fileExists(path);
|
|
|
|
}
|
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
/*
|
2013-12-12 21:55:39 +03:00
|
|
|
* Get LSN from result of pg_start_backup() or pg_stop_backup().
|
2012-05-18 11:54:36 +03:00
|
|
|
*/
|
|
|
|
static void
|
2016-09-29 16:33:21 +02:00
|
|
|
get_lsn(PGconn *conn, PGresult *res, XLogRecPtr *lsn, bool stop_backup)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
2013-12-12 21:55:39 +03:00
|
|
|
uint32 xlogid;
|
|
|
|
uint32 xrecoff;
|
|
|
|
|
2016-09-29 16:33:21 +02:00
|
|
|
if (res == NULL || PQntuples(res) != 1 || (PQnfields(res) != 1 && PQnfields(res) != 3))
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"result of backup command is invalid: %s",
|
2016-09-29 16:33:21 +02:00
|
|
|
PQerrorMessage(conn));
|
2012-05-18 11:54:36 +03:00
|
|
|
|
2013-12-12 21:55:39 +03:00
|
|
|
/*
|
|
|
|
* Extract timeline and LSN from results of pg_stop_backup()
|
|
|
|
* and friends.
|
|
|
|
*/
|
|
|
|
|
2016-05-26 14:56:32 +02:00
|
|
|
XLogDataFromLSN(PQgetvalue(res, 0, 0), &xlogid, &xrecoff);
|
2013-12-12 21:55:39 +03:00
|
|
|
/* Calculate LSN */
|
|
|
|
*lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
|
2016-09-29 16:33:21 +02:00
|
|
|
|
|
|
|
if (stop_backup && from_replica && PQnfields(res) == 3)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char path_backup_label[MAXPGPATH];
|
|
|
|
char path_tablespace_map[MAXPGPATH];
|
|
|
|
FILE *fp;
|
2016-09-29 18:44:59 +02:00
|
|
|
pgFile *file;
|
2016-09-29 16:33:21 +02:00
|
|
|
|
|
|
|
pgBackupGetPath(¤t, path, lengthof(path), DATABASE_DIR);
|
|
|
|
snprintf(path_backup_label, lengthof(path_backup_label), "%s/backup_label", path);
|
|
|
|
snprintf(path_tablespace_map, lengthof(path_tablespace_map), "%s/tablespace_map", path);
|
|
|
|
|
|
|
|
fp = fopen(path_backup_label, "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
elog(ERROR, "can't open backup label file \"%s\": %s",
|
|
|
|
path_backup_label, strerror(errno));
|
|
|
|
|
|
|
|
fwrite(PQgetvalue(res, 0, 1), 1, strlen(PQgetvalue(res, 0, 1)), fp);
|
|
|
|
fclose(fp);
|
2016-09-29 18:44:59 +02:00
|
|
|
file = pgFileNew(path_backup_label, true);
|
|
|
|
calc_file(file);
|
|
|
|
free(file->path);
|
|
|
|
file->path = strdup("backup_label");
|
|
|
|
parray_append(backup_files_list, file);
|
2016-09-29 16:33:21 +02:00
|
|
|
if (strlen(PQgetvalue(res, 0, 2)) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fp = fopen(path_tablespace_map, "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
elog(ERROR, "can't open tablespace map file \"%s\": %s",
|
|
|
|
path_tablespace_map, strerror(errno));
|
|
|
|
|
|
|
|
fwrite(PQgetvalue(res, 0, 2), 1, strlen(PQgetvalue(res, 0, 2)), fp);
|
|
|
|
fclose(fp);
|
2016-09-29 18:44:59 +02:00
|
|
|
file = pgFileNew(path_tablespace_map, true);
|
|
|
|
calc_file(file);
|
|
|
|
free(file->path);
|
|
|
|
file->path = strdup("tablespace_map");
|
|
|
|
parray_append(backup_files_list, file);
|
2016-09-29 16:33:21 +02:00
|
|
|
}
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get XID from result of txid_current() after pg_stop_backup().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
get_xid(PGresult *res, uint32 *xid)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
if (res == NULL || PQntuples(res) != 1 || PQnfields(res) != 1)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"result of txid_current() is invalid: %s",
|
2012-05-18 11:54:36 +03:00
|
|
|
PQerrorMessage(connection));
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
if (sscanf(PQgetvalue(res, 0, 0), "%u", xid) != 1)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"result of txid_current() is invalid: %s",
|
2012-05-18 11:54:36 +03:00
|
|
|
PQerrorMessage(connection));
|
|
|
|
}
|
|
|
|
elog(LOG, "%s():%s", __FUNCTION__, PQgetvalue(res, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if the path is a existing regular file.
|
|
|
|
*/
|
2013-09-09 12:00:13 +03:00
|
|
|
bool
|
2012-05-18 11:54:36 +03:00
|
|
|
fileExists(const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (stat(path, &buf) == -1 && errno == ENOENT)
|
|
|
|
return false;
|
|
|
|
else if (!S_ISREG(buf.st_mode))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify end of backup to server when "backup_label" is in the root directory
|
|
|
|
* of the DB cluster.
|
|
|
|
* Also update backup status to ERROR when the backup is not finished.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
backup_cleanup(bool fatal, void *userdata)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
|
|
|
|
if (!in_backup)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If backup_label exist in $PGDATA, notify stop of backup to PostgreSQL */
|
|
|
|
snprintf(path, lengthof(path), "%s/backup_label", pgdata);
|
|
|
|
make_native_path(path);
|
|
|
|
if (fileExists(path))
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "backup_label exists, stop backup");
|
2012-05-18 11:54:36 +03:00
|
|
|
pg_stop_backup(NULL); /* don't care stop_lsn on error case */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update status of backup.ini to ERROR.
|
|
|
|
* end_time != 0 means backup finished
|
|
|
|
*/
|
|
|
|
if (current.status == BACKUP_STATUS_RUNNING && current.end_time == 0)
|
|
|
|
{
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "backup is running, update its status to ERROR");
|
2012-05-18 11:54:36 +03:00
|
|
|
current.end_time = time(NULL);
|
|
|
|
current.status = BACKUP_STATUS_ERROR;
|
|
|
|
pgBackupWriteIni(¤t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 09:58:55 +03:00
|
|
|
/*
|
|
|
|
* Take differential backup at page level.
|
|
|
|
*/
|
2012-05-18 11:54:36 +03:00
|
|
|
static void
|
2016-02-29 19:23:48 +02:00
|
|
|
backup_files(void *arg)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct timeval tv;
|
|
|
|
|
2016-02-29 19:23:48 +02:00
|
|
|
backup_files_args *arguments = (backup_files_args *) arg;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
|
|
/* backup a file or create a directory */
|
2016-09-02 18:31:49 +02:00
|
|
|
for (i = 0; i < parray_num(arguments->files); i++)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat buf;
|
|
|
|
|
2016-02-29 19:23:48 +02:00
|
|
|
pgFile *file = (pgFile *) parray_get(arguments->files, i);
|
2016-09-02 19:38:39 +02:00
|
|
|
if (__sync_lock_test_and_set(&file->lock, 1) != 0)
|
|
|
|
continue;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* If current time is rewinded, abort this backup. */
|
2016-01-14 09:36:39 +02:00
|
|
|
if (tv.tv_sec < file->mtime)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"current time may be rewound. Please retry with full backup mode.");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* check for interrupt */
|
|
|
|
if (interrupted)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "interrupted during backup");
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* print progress in verbose mode */
|
|
|
|
if (verbose)
|
2016-02-29 19:23:48 +02:00
|
|
|
elog(LOG, "(%d/%lu) %s", i + 1, (unsigned long) parray_num(arguments->files),
|
|
|
|
file->path + strlen(arguments->from_root) + 1);
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* stat file to get file type, size and modify timestamp */
|
|
|
|
ret = stat(file->path, &buf);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
|
|
|
{
|
|
|
|
/* record as skipped file in file_xxx.txt */
|
|
|
|
file->write_size = BYTES_INVALID;
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "skip");
|
2012-05-18 11:54:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"can't stat backup mode. \"%s\": %s",
|
2012-05-18 11:54:36 +03:00
|
|
|
file->path, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 19:23:48 +02:00
|
|
|
/* skip dir because make before */
|
2012-05-18 11:54:36 +03:00
|
|
|
if (S_ISDIR(buf.st_mode))
|
|
|
|
{
|
2016-02-29 19:23:48 +02:00
|
|
|
continue;
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
else if (S_ISREG(buf.st_mode))
|
|
|
|
{
|
|
|
|
/* skip files which have not been modified since last backup */
|
2016-02-29 19:23:48 +02:00
|
|
|
if (arguments->prev_files)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
pgFile *prev_file = NULL;
|
2016-02-29 19:23:48 +02:00
|
|
|
pgFile **p = (pgFile **) parray_bsearch(arguments->prev_files, file, pgFileComparePath);
|
|
|
|
if (p)
|
|
|
|
prev_file = *p;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
if (prev_file && prev_file->mtime == file->mtime)
|
|
|
|
{
|
|
|
|
/* record as skipped file in file_xxx.txt */
|
|
|
|
file->write_size = BYTES_INVALID;
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "skip");
|
2012-05-18 11:54:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We will wait until the next second of mtime so that backup
|
|
|
|
* file should contain all modifications at the clock of mtime.
|
|
|
|
* timer resolution of ext3 file system is one second.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tv.tv_sec == file->mtime)
|
|
|
|
{
|
|
|
|
/* update time and recheck */
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
while (tv.tv_sec <= file->mtime)
|
|
|
|
{
|
|
|
|
usleep(1000000 - tv.tv_usec);
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the file into backup */
|
|
|
|
if (!(file->is_datafile
|
2016-02-29 19:23:48 +02:00
|
|
|
? backup_data_file(arguments->from_root, arguments->to_root, file, arguments->lsn)
|
|
|
|
: copy_file(arguments->from_root, arguments->to_root, file)))
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
/* record as skipped file in file_xxx.txt */
|
|
|
|
file->write_size = BYTES_INVALID;
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "skip");
|
2012-05-18 11:54:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "copied %lu", (unsigned long) file->write_size);
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
else
|
2016-01-14 09:36:39 +02:00
|
|
|
elog(LOG, "unexpected file type %d", buf.st_mode);
|
2016-09-02 19:16:00 +02:00
|
|
|
if (progress)
|
|
|
|
fprintf(stderr, "\rProgress %i/%u", total_copy_files_increment, total_files_num-1);
|
|
|
|
__sync_fetch_and_add(&total_copy_files_increment, 1);
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append files to the backup list array.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
add_files(parray *files, const char *root, bool add_root, bool is_pgdata)
|
|
|
|
{
|
|
|
|
parray *list_file;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
list_file = parray_new();
|
|
|
|
|
|
|
|
/* list files with the logical path. omit $PGDATA */
|
|
|
|
dir_list_file(list_file, root, pgdata_exclude, true, add_root);
|
|
|
|
|
|
|
|
/* mark files that are possible datafile as 'datafile' */
|
|
|
|
for (i = 0; i < parray_num(list_file); i++)
|
|
|
|
{
|
|
|
|
pgFile *file = (pgFile *) parray_get(list_file, i);
|
|
|
|
char *relative;
|
|
|
|
char *fname;
|
2016-02-27 20:07:55 +02:00
|
|
|
int path_len;
|
2012-05-18 11:54:36 +03:00
|
|
|
|
|
|
|
/* data file must be a regular file */
|
|
|
|
if (!S_ISREG(file->mode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* data files are under "base", "global", or "pg_tblspc" */
|
|
|
|
relative = file->path + strlen(root) + 1;
|
|
|
|
if (is_pgdata &&
|
|
|
|
!path_is_prefix_of_path("base", relative) &&
|
2016-05-11 18:35:14 +02:00
|
|
|
/*!path_is_prefix_of_path("global", relative) &&*/
|
2012-05-18 11:54:36 +03:00
|
|
|
!path_is_prefix_of_path("pg_tblspc", relative))
|
|
|
|
continue;
|
|
|
|
|
2016-02-27 20:07:55 +02:00
|
|
|
path_len = strlen(file->path);
|
|
|
|
if (path_len > 6 && strncmp(file->path+(path_len-6), "ptrack", 6) == 0)
|
|
|
|
{
|
|
|
|
pgFile *search_file;
|
2016-05-01 21:05:18 +02:00
|
|
|
pgFile **pre_search_file;
|
|
|
|
int segno = 0;
|
|
|
|
while(true) {
|
|
|
|
pgFile tmp_file;
|
|
|
|
tmp_file.path = pg_strdup(file->path);
|
|
|
|
/* I hope segno not more than 999999 */
|
|
|
|
if (segno > 0)
|
|
|
|
sprintf(tmp_file.path+path_len-7, ".%d", segno);
|
|
|
|
else
|
|
|
|
tmp_file.path[path_len-7] = '\0';
|
|
|
|
pre_search_file = (pgFile **) parray_bsearch(list_file, &tmp_file, pgFileComparePath);
|
|
|
|
if (pre_search_file != NULL)
|
|
|
|
{
|
|
|
|
search_file = *pre_search_file;
|
|
|
|
search_file->ptrack_path = pg_strdup(file->path);
|
|
|
|
search_file->segno = segno;
|
|
|
|
} else {
|
|
|
|
pg_free(tmp_file.path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pg_free(tmp_file.path);
|
|
|
|
segno++;
|
2016-02-27 20:07:55 +02:00
|
|
|
}
|
2016-05-01 21:05:18 +02:00
|
|
|
|
2016-02-27 20:07:55 +02:00
|
|
|
pgFileFree(file);
|
|
|
|
parray_remove(list_file, i);
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-18 11:54:36 +03:00
|
|
|
/* name of data file start with digit */
|
|
|
|
fname = last_dir_separator(relative);
|
|
|
|
if (fname == NULL)
|
|
|
|
fname = relative;
|
|
|
|
else
|
|
|
|
fname++;
|
|
|
|
if (!isdigit(fname[0]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
file->is_datafile = true;
|
2016-09-06 15:30:54 +02:00
|
|
|
{
|
|
|
|
int find_dot;
|
2016-09-09 18:01:36 +02:00
|
|
|
int check_digit;
|
2016-09-06 15:30:54 +02:00
|
|
|
char *text_segno;
|
|
|
|
for(find_dot = path_len-1; file->path[find_dot] != '.' && find_dot >= 0; find_dot--);
|
|
|
|
if (find_dot <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
text_segno = file->path + find_dot + 1;
|
2016-09-09 18:01:36 +02:00
|
|
|
for(check_digit=0; text_segno[check_digit] != '\0'; check_digit++)
|
|
|
|
if (!isdigit(text_segno[check_digit]))
|
|
|
|
{
|
|
|
|
check_digit = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_digit == -1)
|
2016-09-06 15:30:54 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
file->segno = (int) strtol(text_segno, NULL, 10);
|
|
|
|
}
|
2012-05-18 11:54:36 +03:00
|
|
|
}
|
|
|
|
parray_concat(files, list_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-01-13 07:04:43 +03:00
|
|
|
* Output the list of files to backup catalog
|
2012-05-18 11:54:36 +03:00
|
|
|
*/
|
|
|
|
static void
|
2014-01-13 07:04:43 +03:00
|
|
|
create_file_list(parray *files,
|
|
|
|
const char *root,
|
|
|
|
const char *subdir,
|
|
|
|
const char *prefix,
|
|
|
|
bool is_append)
|
2012-05-18 11:54:36 +03:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
|
|
|
|
if (!check)
|
|
|
|
{
|
|
|
|
/* output path is '$BACKUP_PATH/file_database.txt' */
|
2014-01-13 07:04:43 +03:00
|
|
|
pgBackupGetPath(¤t, path, lengthof(path), subdir);
|
2012-05-18 11:54:36 +03:00
|
|
|
fp = fopen(path, is_append ? "at" : "wt");
|
|
|
|
if (fp == NULL)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "can't open file list \"%s\": %s", path,
|
2012-05-18 11:54:36 +03:00
|
|
|
strerror(errno));
|
|
|
|
dir_print_file_list(fp, files, root, prefix);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 16:47:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A helper function to create the path of a relation file and segment.
|
|
|
|
*
|
|
|
|
* The returned path is palloc'd
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
datasegpath(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
char *segpath;
|
|
|
|
|
|
|
|
path = relpathperm(rnode, forknum);
|
|
|
|
if (segno > 0)
|
|
|
|
{
|
|
|
|
segpath = psprintf("%s.%u", path, segno);
|
|
|
|
pfree(path);
|
|
|
|
return segpath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine gets called while reading WAL segments from the WAL archive,
|
|
|
|
* for every block that have changed in the target system. It makes note of
|
|
|
|
* all the changed blocks in the pagemap of the file and adds them in the
|
|
|
|
* things to track for the backup.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
process_block_change(ForkNumber forknum, RelFileNode rnode, BlockNumber blkno)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
char *rel_path;
|
|
|
|
BlockNumber blkno_inseg;
|
|
|
|
int segno;
|
|
|
|
pgFile *file_item = NULL;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
segno = blkno / RELSEG_SIZE;
|
|
|
|
blkno_inseg = blkno % RELSEG_SIZE;
|
|
|
|
|
|
|
|
rel_path = datasegpath(rnode, forknum, segno);
|
|
|
|
path = pg_malloc(strlen(rel_path) + strlen(pgdata) + 2);
|
|
|
|
sprintf(path, "%s/%s", pgdata, rel_path);
|
|
|
|
|
|
|
|
for (j = 0; j < parray_num(backup_files_list); j++)
|
|
|
|
{
|
|
|
|
pgFile *p = (pgFile *) parray_get(backup_files_list, j);
|
|
|
|
|
|
|
|
if (strcmp(p->path, path) == 0)
|
|
|
|
{
|
|
|
|
file_item = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we don't have any record of this file in the file map, it means
|
|
|
|
* that it's a relation that did not have much activity since the last
|
|
|
|
* backup. We can safely ignore it. If it is a new relation file, the
|
|
|
|
* backup would simply copy it as-is.
|
|
|
|
*/
|
|
|
|
if (file_item)
|
|
|
|
datapagemap_add(&file_item->pagemap, blkno_inseg);
|
|
|
|
|
|
|
|
pg_free(path);
|
|
|
|
pg_free(rel_path);
|
|
|
|
}
|
2016-02-27 20:07:55 +02:00
|
|
|
|
|
|
|
void make_pagemap_from_ptrack(parray *files)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < parray_num(files); i++)
|
|
|
|
{
|
|
|
|
pgFile *p = (pgFile *) parray_get(files, i);
|
|
|
|
if (p->ptrack_path != NULL)
|
|
|
|
{
|
2016-05-11 18:35:14 +02:00
|
|
|
char *flat_memory;
|
|
|
|
char *tmp_path = p->ptrack_path;
|
|
|
|
char *tablespace;
|
|
|
|
size_t path_length = strlen(p->ptrack_path);
|
2016-05-01 21:05:18 +02:00
|
|
|
size_t flat_size = 0;
|
|
|
|
size_t start_addr;
|
2016-05-11 18:35:14 +02:00
|
|
|
Oid db_oid, rel_oid, tablespace_oid = 0;
|
|
|
|
int sep_iter, sep_count = 0;
|
|
|
|
tablespace = palloc0(64);
|
2016-05-01 21:05:18 +02:00
|
|
|
|
2016-05-11 18:35:14 +02:00
|
|
|
/* Find target path*/
|
|
|
|
for(sep_iter = (int)path_length; sep_iter >= 0; sep_iter--)
|
2016-02-27 20:07:55 +02:00
|
|
|
{
|
2016-05-11 18:35:14 +02:00
|
|
|
if (IS_DIR_SEP(tmp_path[sep_iter]))
|
|
|
|
{
|
|
|
|
sep_count++;
|
|
|
|
}
|
|
|
|
if (sep_count == 3)
|
|
|
|
{
|
|
|
|
tmp_path += sep_iter + 1;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-27 20:07:55 +02:00
|
|
|
}
|
2016-05-11 18:35:14 +02:00
|
|
|
/* For unix only now */
|
|
|
|
sscanf(tmp_path, "%[^/]/%u/%u_ptrack", tablespace, &db_oid, &rel_oid);
|
|
|
|
if (strcmp(tablespace, "base") != 0 && strcmp(tablespace, "global") != 0)
|
|
|
|
sscanf(tablespace, "%i", &tablespace_oid);
|
2016-02-27 20:07:55 +02:00
|
|
|
|
2016-05-11 18:35:14 +02:00
|
|
|
flat_memory = pg_ptrack_get_and_clear(tablespace_oid,
|
|
|
|
db_oid,
|
|
|
|
rel_oid,
|
|
|
|
&flat_size);
|
2016-02-28 01:10:27 +02:00
|
|
|
|
2016-05-01 21:05:18 +02:00
|
|
|
start_addr = (RELSEG_SIZE/8)*p->segno;
|
|
|
|
p->pagemap.bitmapsize = start_addr+RELSEG_SIZE/8 > flat_size ? flat_size - start_addr : RELSEG_SIZE/8;
|
|
|
|
p->pagemap.bitmap = pg_malloc(p->pagemap.bitmapsize);
|
|
|
|
memcpy(p->pagemap.bitmap, flat_memory+start_addr, p->pagemap.bitmapsize);
|
|
|
|
pg_free(flat_memory);
|
2016-05-11 18:35:14 +02:00
|
|
|
pg_free(tablespace);
|
2016-02-27 20:07:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 14:56:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
stop_streaming(XLogRecPtr xlogpos, uint32 timeline, bool segment_finished)
|
|
|
|
{
|
|
|
|
static uint32 prevtimeline = 0;
|
|
|
|
static XLogRecPtr prevpos = InvalidXLogRecPtr;
|
|
|
|
|
|
|
|
/* we assume that we get called once at the end of each segment */
|
|
|
|
if (verbose && segment_finished)
|
|
|
|
fprintf(stderr, _("%s: finished segment at %X/%X (timeline %u)\n"),
|
|
|
|
progname, (uint32) (xlogpos >> 32), (uint32) xlogpos,
|
|
|
|
timeline);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that we report the previous, not current, position here. After a
|
|
|
|
* timeline switch, xlogpos points to the beginning of the segment because
|
|
|
|
* that's where we always begin streaming. Reporting the end of previous
|
|
|
|
* timeline isn't totally accurate, because the next timeline can begin
|
|
|
|
* slightly before the end of the WAL that we received on the previous
|
|
|
|
* timeline, but it's close enough for reporting purposes.
|
|
|
|
*/
|
|
|
|
if (prevtimeline != 0 && prevtimeline != timeline)
|
|
|
|
fprintf(stderr, _("%s: switched to timeline %u at %X/%X\n"),
|
|
|
|
progname, timeline,
|
|
|
|
(uint32) (prevpos >> 32), (uint32) prevpos);
|
|
|
|
|
|
|
|
if (stop_backup_lsn != InvalidXLogRecPtr && xlogpos > stop_backup_lsn)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
prevtimeline = timeline;
|
|
|
|
prevpos = xlogpos;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start the log streaming
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
StreamLog(void *arg)
|
|
|
|
{
|
|
|
|
XLogRecPtr startpos;
|
|
|
|
TimeLineID starttli;
|
|
|
|
char *basedir = (char *)arg;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect in replication mode to the server
|
|
|
|
*/
|
|
|
|
if (conn == NULL)
|
|
|
|
conn = GetConnection();
|
|
|
|
if (!conn)
|
|
|
|
/* Error message already written in GetConnection() */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!CheckServerVersionForStreaming(conn))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Error message already written in CheckServerVersionForStreaming().
|
|
|
|
* There's no hope of recovering from a version mismatch, so don't
|
|
|
|
* retry.
|
|
|
|
*/
|
|
|
|
disconnect_and_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify server, obtaining start LSN position and current timeline ID
|
|
|
|
* at the same time, necessary if not valid data can be found in the
|
|
|
|
* existing output directory.
|
|
|
|
*/
|
|
|
|
if (!RunIdentifySystem(conn, NULL, &starttli, &startpos, NULL))
|
|
|
|
disconnect_and_exit(1);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Always start streaming at the beginning of a segment
|
|
|
|
*/
|
|
|
|
startpos -= startpos % XLOG_SEG_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start the replication
|
|
|
|
*/
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: starting log streaming at %X/%X (timeline %u)\n"),
|
|
|
|
progname, (uint32) (startpos >> 32), (uint32) startpos,
|
|
|
|
starttli);
|
|
|
|
|
2016-08-30 16:21:02 +02:00
|
|
|
#if PG_VERSION_NUM >= 90600
|
|
|
|
StreamCtl ctl;
|
|
|
|
ctl.startpos = startpos;
|
|
|
|
ctl.timeline = starttli;
|
|
|
|
ctl.sysidentifier = NULL;
|
|
|
|
ctl.basedir = basedir;
|
|
|
|
ctl.stream_stop = stop_streaming;
|
|
|
|
ctl.standby_message_timeout = standby_message_timeout;
|
2016-09-06 13:06:09 +02:00
|
|
|
ctl.partial_suffix = NULL;
|
2016-08-30 16:21:02 +02:00
|
|
|
ctl.synchronous = false;
|
|
|
|
ctl.mark_done = false;
|
|
|
|
ReceiveXlogStream(conn, &ctl);
|
|
|
|
#else
|
2016-05-26 14:56:32 +02:00
|
|
|
ReceiveXlogStream(conn, startpos, starttli, NULL, basedir,
|
2016-09-06 13:06:09 +02:00
|
|
|
stop_streaming, standby_message_timeout, NULL,
|
2016-05-26 14:56:32 +02:00
|
|
|
false, false);
|
2016-08-30 16:21:02 +02:00
|
|
|
#endif
|
2016-05-26 14:56:32 +02:00
|
|
|
|
|
|
|
PQfinish(conn);
|
|
|
|
conn = NULL;
|
|
|
|
}
|