Add option --verify-backup

This causes pg_verifybackup to be executed immediately after
pg_basebackup completes.

PostreSQL 13 and later.
This commit is contained in:
Ian Barwick
2020-09-03 10:40:17 +09:00
parent c8e52e486f
commit 9a836b3c04
6 changed files with 96 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
5.2.0 2020-??-??
repmgr: improve database connection failure error checking on the
demotion candidate during "standby switchover" (Ian)
repmgr: add option --verify-backup to "standby clone"
5.1.0 2020-04-13
repmgr: remove BDR 2.x support

View File

@@ -373,6 +373,23 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--verify-backup</option></term>
<listitem>
<para>
<!-- update link after Pg13 release -->
Verify a cloned node using the
<ulink url="https://www.postgresql.org/docs/13/app-pgverifybackup.html">pg_verifybackup</ulink>
utility (PostgreSQL 13 and later).
</para>
<para>
This option can currently only be used when cloning directly from an upstream
node.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--without-barman </option></term>
<listitem>

View File

@@ -157,6 +157,7 @@ static CheckStatus parse_db_connection(const char *db_connection);
* --replication-user (only required if no upstream record)
* --without-barman
* --replication-conf-only (--recovery-conf-only)
* --verify-backup (PostgreSQL 13 and later)
*/
void
@@ -218,6 +219,15 @@ do_standby_clone(void)
if (mode == barman)
{
/*
* Not currently possible to use --verify-backup with Barman
*/
if (runtime_options.verify_backup == true)
{
log_error(_("--verify-backup option cannot be used when cloning from Barman backups"));
exit(ERR_BAD_CONFIG);
}
/*
* Sanity-check barman connection and installation;
* this will exit with ERR_BARMAN if problems found.
@@ -323,13 +333,25 @@ do_standby_clone(void)
/*
* This connects to the source node and performs sanity checks, also
* sets "recovery_conninfo_str", "upstream_repluser", "upstream_user" and
* "upstream_node_id".
* "upstream_node_id" and creates a connection handle in "source_conn".
*
* Will error out if source connection not possible and not in
* "barman" mode.
*/
check_source_server();
if (runtime_options.verify_backup == true)
{
/*
* --verify-backup available for PostgreSQL 13 and later
*/
if (PQserverVersion(source_conn) < 130000)
{
log_error(_("--verify-backup available for PostgreSQL 13 and later"));
exit(ERR_BAD_CONFIG);
}
}
/* attempt to retrieve upstream node record */
record_status = get_node_record(source_conn,
upstream_node_id,
@@ -341,6 +363,7 @@ do_standby_clone(void)
upstream_node_id);
exit(ERR_BAD_CONFIG);
}
}
else
{
@@ -719,6 +742,49 @@ do_standby_clone(void)
exit(r);
}
/*
* Run pg_verifybackup here if requested, before any alterations are made
* to the data directory.
*/
if (mode == pg_basebackup && runtime_options.verify_backup == true)
{
PQExpBufferData command;
int r;
struct stat st;
initPQExpBuffer(&command);
make_pg_path(&command, "pg_verifybackup");
/* check command actually exists */
if (stat(command.data, &st) != 0)
{
log_error(_("unable to find expected binary \"%s\""), command.data);
log_detail("%s", strerror(errno));
exit(ERR_BAD_CONFIG);
}
appendPQExpBufferStr(&command, " ");
/* Somewhat inconsistent, but pg_verifybackup doesn't accept a -D option */
appendShellString(&command,
local_data_directory);
log_debug("executing:\n %s", command.data);
r = system(command.data);
termPQExpBuffer(&command);
if (r != 0)
{
log_error(_("unable to verify backup"));
exit(ERR_BAD_BASEBACKUP);
}
log_verbose(LOG_INFO, _("backup successfully verified"));
}
/*
* If `--copy-external-config-files` was provided, copy any configuration
@@ -8841,6 +8907,9 @@ do_standby_help(void)
printf(_(" --upstream-conninfo \"primary_conninfo\" value to write in recovery.conf\n" \
" when the intended upstream server does not yet exist\n"));
printf(_(" --upstream-node-id ID of the upstream node to replicate from (optional, defaults to primary node)\n"));
#if (PG_VERSION_NUM >= 130000)
printf(_(" --verify-backup verify a cloned node using the \"pg_verifybackup\" utility\n"));
#endif
printf(_(" --without-barman do not use Barman even if configured\n"));
printf(_(" --replication-conf-only generate replication configuration for a previously cloned instance\n"));

View File

@@ -88,6 +88,7 @@ typedef struct
char upstream_conninfo[MAXLEN];
bool without_barman;
bool replication_conf_only;
bool verify_backup;
/* "standby clone"/"standby follow" options */
int upstream_node_id;
@@ -163,7 +164,7 @@ typedef struct
UNKNOWN_NODE_ID, "", "", UNKNOWN_NODE_ID, \
/* "standby clone" options */ \
false, CONFIG_FILE_SAMEPATH, false, false, false, "", "", "", \
false, false, \
false, false, false, \
/* "standby clone"/"standby follow" options */ \
NO_UPSTREAM_NODE, \
/* "standby register" options */ \

View File

@@ -433,6 +433,10 @@ main(int argc, char **argv)
runtime_options.replication_conf_only = true;
break;
/* --verify-backup */
case OPT_VERIFY_BACKUP:
runtime_options.verify_backup = true;
break;
/*---------------------------
* "standby register" options

View File

@@ -98,6 +98,7 @@
#define OPT_REPMGRD_FORCE_UNPAUSE 1045
#define OPT_REPLICATION_CONFIG_OWNER 1046
#define OPT_DB_CONNECTION 1047
#define OPT_VERIFY_BACKUP 1048
/* These options are for internal use only */
#define OPT_CONFIG_ARCHIVE_DIR 2001
@@ -160,6 +161,7 @@ static struct option long_options[] =
{"upstream-node-id", required_argument, NULL, OPT_UPSTREAM_NODE_ID},
{"without-barman", no_argument, NULL, OPT_WITHOUT_BARMAN},
{"replication-conf-only", no_argument, NULL, OPT_REPLICATION_CONF_ONLY},
{"verify-backup", no_argument, NULL, OPT_VERIFY_BACKUP },
/* deprecate this once Pg11 and earlier are unsupported */
{"recovery-conf-only", no_argument, NULL, OPT_REPLICATION_CONF_ONLY},