Merge commit '3ef1fa126d9c9b9ba3b29deab7f67218cdf7ce10'

Conflicts:
	.gitignore
	Makefile
	README.rst
	check_dir.c
	config.c
	config.h
	dbutils.h
	repmgr.c
	repmgr.conf
	repmgr.h
	repmgrd.c
This commit is contained in:
Greg Smith
2011-02-15 00:06:01 -05:00
15 changed files with 541 additions and 233 deletions

401
repmgr.c
View File

@@ -34,20 +34,22 @@
#include "log.h"
#include "config.h"
#include "check_dir.h"
#include "strutil.h"
#define RECOVERY_FILE "recovery.conf"
#define RECOVERY_DONE_FILE "recovery.done"
#define NO_ACTION 0 /* Not a real action, just to initialize */
#define MASTER_REGISTER 1
#define NO_ACTION 0 /* Not a real action, just to initialize */
#define MASTER_REGISTER 1
#define STANDBY_REGISTER 2
#define STANDBY_CLONE 3
#define STANDBY_PROMOTE 4
#define STANDBY_FOLLOW 5
static void help(const char *progname);
static bool create_recovery_file(const char *data_dir);
static int copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_path, bool is_directory);
static bool create_recovery_file(const char *data_dir, char *master_conninfo);
static int copy_remote_files(char *host, char *remote_user, char *remote_path,
char *local_path, bool is_directory);
static bool check_parameters_for_action(const int action);
static void do_master_register(void);
@@ -111,7 +113,8 @@ main(int argc, char **argv)
}
while ((c = getopt_long(argc, argv, "d:h:p:U:D:f:R:w:F:v", long_options, &optindex)) != -1)
while ((c = getopt_long(argc, argv, "d:h:p:U:D:f:R:w:F:v", long_options,
&optindex)) != -1)
{
switch (c)
{
@@ -164,7 +167,8 @@ main(int argc, char **argv)
if (optind < argc)
{
server_mode = argv[optind++];
if (strcasecmp(server_mode, "STANDBY") != 0 && strcasecmp(server_mode, "MASTER") != 0)
if (strcasecmp(server_mode, "STANDBY") != 0 &&
strcasecmp(server_mode, "MASTER") != 0)
{
usage();
exit(ERR_BAD_CONFIG);
@@ -240,36 +244,18 @@ main(int argc, char **argv)
}
/*
* Read the configuration file: repmgr.conf
* Read the configuration file: repmgr.conf, but only if we're not doing a
* STANDBY CLONE action: it is not necessary to have the configuration file
* in that case.
*/
parse_config(runtime_options.config_file, &options);
if (options.node == -1)
if (action != STANDBY_CLONE)
{
fprintf(stderr, "Node information is missing. "
"Check the configuration file.\n");
exit(ERR_BAD_CONFIG);
if (runtime_options.verbose)
printf(_("Opening configuration file: %s\n"), runtime_options.config_file);
parse_config(runtime_options.config_file, &options);
}
keywords[2] = "user";
values[2] = runtime_options.username;
keywords[3] = "dbname";
values[3] = runtime_options.dbname;
keywords[4] = "application_name";
values[4] = (char *) progname;
keywords[5] = NULL;
values[5] = NULL;
if (!runtime_options.config_file[0])
strncpy(runtime_options.config_file, DEFAULT_CONFIG_FILE, MAXLEN);
/*
* Read the configuration file: repmgr.conf
*/
if (runtime_options.verbose)
printf(_("Opening configuration file: %s\n"), runtime_options.config_file);
parse_config(runtime_options.config_file, &options);
if (need_a_node)
{
@@ -317,11 +303,12 @@ main(int argc, char **argv)
static void
do_master_register(void)
{
PGconn *conn;
PGconn *conn;
PGresult *res;
char sqlquery[QUERY_STR_LEN];
bool schema_exists = false;
char schema_quoted[MAXLEN];
char master_version[MAXVERSIONSTR];
conn = establishDBConnection(options.conninfo, true);
@@ -343,8 +330,22 @@ do_master_register(void)
exit(ERR_BAD_CONFIG);
}
/* Assemble a quoted schema name
* XXX This is not currently used due to a merge conflict, but
* probably should be */
if (false)
{
char *identifier = PQescapeIdentifier(conn, repmgr_schema,
strlen(repmgr_schema));
maxlen_snprintf(schema_quoted, "%s", identifier);
PQfreemem(identifier);
}
/* Check if there is a schema for this cluster */
snprintf(sqlquery, QUERY_STR_LEN, "SELECT 1 FROM pg_namespace WHERE nspname = '%s'", repmgr_schema);
sqlquery_snprintf(sqlquery,
"SELECT 1 FROM pg_namespace "
"WHERE nspname = '%s'", repmgr_schema);
log_debug("master register: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -373,7 +374,7 @@ do_master_register(void)
log_info("master register: creating database objects inside the %s schema", repmgr_schema);
/* ok, create the schema */
snprintf(sqlquery, QUERY_STR_LEN, "CREATE SCHEMA %s", repmgr_schema);
sqlquery_snprintf(sqlquery, "CREATE SCHEMA %s", repmgr_schema);
log_debug("master register: %s\n", sqlquery);
if (!PQexec(conn, sqlquery))
{
@@ -384,7 +385,7 @@ do_master_register(void)
}
/* ... the tables */
snprintf(sqlquery, QUERY_STR_LEN, "CREATE TABLE %s.repl_nodes ( "
sqlquery_snprintf(sqlquery, "CREATE TABLE %s.repl_nodes ( "
" id integer primary key, "
" cluster text not null, "
" conninfo text not null)", repmgr_schema);
@@ -397,7 +398,7 @@ do_master_register(void)
exit(ERR_BAD_CONFIG);
}
snprintf(sqlquery, QUERY_STR_LEN, "CREATE TABLE %s.repl_monitor ( "
sqlquery_snprintf(sqlquery, "CREATE TABLE %s.repl_monitor ( "
" primary_node INTEGER NOT NULL, "
" standby_node INTEGER NOT NULL, "
" last_monitor_time TIMESTAMP WITH TIME ZONE NOT NULL, "
@@ -415,7 +416,7 @@ do_master_register(void)
}
/* and the view */
snprintf(sqlquery, QUERY_STR_LEN, "CREATE VIEW %s.repl_status AS "
sqlquery_snprintf(sqlquery, "CREATE VIEW %s.repl_status AS "
" WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node "
" ORDER BY last_monitor_time desc) "
" FROM %s.repl_monitor) "
@@ -436,10 +437,11 @@ do_master_register(void)
else
{
PGconn *master_conn;
int id;
int id;
/* Ensure there isn't any other master already registered */
master_conn = getMasterConnection(conn, options.node, options.cluster_name, &id);
master_conn = getMasterConnection(conn, options.node,
options.cluster_name, &id,NULL);
if (master_conn != NULL)
{
PQfinish(master_conn);
@@ -451,7 +453,7 @@ do_master_register(void)
/* Now register the master */
if (runtime_options.force)
{
snprintf(sqlquery, QUERY_STR_LEN, "DELETE FROM %s.repl_nodes "
sqlquery_snprintf(sqlquery, "DELETE FROM %s.repl_nodes "
" WHERE id = %d",
repmgr_schema, options.node);
log_debug("master register: %s\n", sqlquery);
@@ -465,7 +467,7 @@ do_master_register(void)
}
}
snprintf(sqlquery, QUERY_STR_LEN, "INSERT INTO %s.repl_nodes "
sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes "
"VALUES (%d, '%s', '%s')",
repmgr_schema, options.node, options.cluster_name, options.conninfo);
log_debug("master register: %s\n", sqlquery);
@@ -488,18 +490,21 @@ do_master_register(void)
static void
do_standby_register(void)
{
PGconn *conn;
PGconn *conn;
PGconn *master_conn;
int master_id;
PGresult *res;
char sqlquery[QUERY_STR_LEN];
char schema_quoted[MAXLEN];
char master_version[MAXVERSIONSTR];
char standby_version[MAXVERSIONSTR];
conn = establishDBConnection(options.conninfo, true);
/* XXX: A lot of copied code from do_master_register! Refactor */
/* should be v9 or better */
pg_version(conn, standby_version);
if (strcmp(standby_version, "") == 0)
@@ -517,8 +522,20 @@ do_standby_register(void)
exit(ERR_BAD_CONFIG);
}
/* Assemble a quoted schema name
* XXX This is not currently used due to a merge conflict, but
* probably should be */
if (false)
{
char *identifier = PQescapeIdentifier(conn, repmgr_schema,
strlen(repmgr_schema));
maxlen_snprintf(schema_quoted, "%s", identifier);
PQfreemem(identifier);
}
/* Check if there is a schema for this cluster */
snprintf(sqlquery, QUERY_STR_LEN, "SELECT 1 FROM pg_namespace WHERE nspname = '%s'", repmgr_schema);
sqlquery_snprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = '%s'", repmgr_schema);
log_debug("standby register: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -529,8 +546,9 @@ do_standby_register(void)
exit(ERR_BAD_CONFIG);
}
if (PQntuples(res) == 0) /* schema doesn't exist */
if (PQntuples(res) == 0)
{
/* schema doesn't exist */
log_err("Schema %s doesn't exists.\n", repmgr_schema);
PQclear(res);
PQfinish(conn);
@@ -539,7 +557,8 @@ do_standby_register(void)
PQclear(res);
/* check if there is a master in this cluster */
master_conn = getMasterConnection(conn, options.node, options.cluster_name, &master_id);
master_conn = getMasterConnection(conn, options.node, options.cluster_name,
&master_id, NULL);
if (!master_conn)
{
log_err(_("A master must be defined before configuring a slave\n"));
@@ -570,7 +589,7 @@ do_standby_register(void)
/* Now register the standby */
if (runtime_options.force)
{
snprintf(sqlquery, QUERY_STR_LEN, "DELETE FROM %s.repl_nodes "
sqlquery_snprintf(sqlquery, "DELETE FROM %s.repl_nodes "
" WHERE id = %d",
repmgr_schema, options.node);
log_debug("standby register: %s\n", sqlquery);
@@ -585,7 +604,7 @@ do_standby_register(void)
}
}
snprintf(sqlquery, QUERY_STR_LEN, "INSERT INTO %s.repl_nodes "
sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes "
"VALUES (%d, '%s', '%s')",
repmgr_schema, options.node, options.cluster_name, options.conninfo);
log_debug("standby register: %s\n", sqlquery);
@@ -608,9 +627,9 @@ do_standby_register(void)
static void
do_standby_clone(void)
{
PGconn *conn;
PGconn *conn;
PGresult *res;
char sqlquery[QUERY_STR_LEN];
char sqlquery[QUERY_STR_LEN];
int r = 0;
int i;
@@ -624,8 +643,8 @@ do_standby_clone(void)
char local_control_file[MAXFILENAME];
char tblspc_dir[MAXFILENAME];
const char *first_wal_segment = NULL;
const char *last_wal_segment = NULL;
char *first_wal_segment = NULL;
const char *last_wal_segment = NULL;
char master_version[MAXVERSIONSTR];
@@ -745,8 +764,14 @@ do_standby_clone(void)
log_info(_("Succesfully connected to primary. Current installation size is %s\n"), get_cluster_size(conn));
/* Check if the tablespace locations exists and that we can write to them */
snprintf(sqlquery, QUERY_STR_LEN, "select spclocation from pg_tablespace where spcname not in ('pg_default', 'pg_global')");
/*
* Check if the tablespace locations exists and that we can write to
* them.
*/
sqlquery_snprintf(sqlquery,
"SELECT spclocation "
" FROM pg_tablespace "
"WHERE spcname NOT IN ('pg_default', 'pg_global')");
log_debug("standby clone: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
@@ -798,9 +823,10 @@ do_standby_clone(void)
/* Present and not empty */
if (!runtime_options.force)
{
fprintf(stderr,
_("%s: directory \"%s\" exists but is not empty\n"),
progname, tblspc_dir);
fprintf(
stderr,
_("%s: directory \"%s\" exists but is not empty\n"),
progname, tblspc_dir);
PQclear(res);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
@@ -818,9 +844,10 @@ do_standby_clone(void)
log_notice("Starting backup...\n");
/* Get the data directory full path and the configuration files location */
snprintf(sqlquery, QUERY_STR_LEN, "SELECT name, setting "
" FROM pg_settings "
" WHERE name IN ('data_directory', 'config_file', 'hba_file', 'ident_file')");
sqlquery_snprintf(sqlquery,
"SELECT name, setting "
" FROM pg_settings "
" WHERE name IN ('data_directory', 'config_file', 'hba_file', 'ident_file')");
log_debug("standby clone: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -849,9 +876,11 @@ do_standby_clone(void)
* inform the master we will start a backup and get the first XLog filename
* so we can say to the user we need those files
*/
snprintf(sqlquery, QUERY_STR_LEN, "SELECT pg_xlogfile_name(pg_start_backup('repmgr_standby_clone_%ld'))", time(NULL));
sqlquery_snprintf(
sqlquery,
"SELECT pg_xlogfile_name(pg_start_backup('repmgr_standby_clone_%ld'))",
time(NULL));
log_debug("standby clone: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
@@ -860,25 +889,36 @@ do_standby_clone(void)
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
first_wal_segment = PQgetvalue(res, 0, 0);
if (runtime_options.verbose)
{
char *first_wal_seg_pq = PQgetvalue(res, 0, 0);
size_t buf_sz = strlen(first_wal_seg_pq);
first_wal_segment = malloc(buf_sz + 1);
xsnprintf(first_wal_segment, buf_sz + 1, "%s", first_wal_seg_pq);
}
PQclear(res);
/*
* 1) first move global/pg_control
*
* 2) then move data_directory ommiting the files we have already moved and pg_xlog
* content
* 2) then move data_directory ommiting the files we have already moved and
* pg_xlog content
*
* 3) finally We need to backup configuration files (that could be on other directories, debian
* like systems likes to do that), so look at config_file, hba_file and ident_file but we
* can omit external_pid_file ;)
* 3) finally We need to backup configuration files (that could be on other
* directories, debian like systems likes to do that), so look at
* config_file, hba_file and ident_file but we can omit
* external_pid_file ;)
*
* On error we need to return but before that execute pg_stop_backup()
*/
/* need to create the global sub directory */
snprintf(master_control_file, MAXFILENAME, "%s/global/pg_control", master_data_directory);
snprintf(local_control_file, MAXFILENAME, "%s/global", runtime_options.dest_dir);
maxlen_snprintf(master_control_file, "%s/global/pg_control",
master_data_directory);
maxlen_snprintf(local_control_file, "%s/global", runtime_options.dest_dir);
if (!create_directory(local_control_file))
{
log_err(_("%s: couldn't create directory %s ...\n"),
@@ -887,7 +927,8 @@ do_standby_clone(void)
}
log_info("standby clone: master control file '%s'\n", master_control_file);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_control_file, local_control_file, false);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user,
master_control_file, local_control_file, false);
if (r != 0)
{
log_warning("standby clone: failed copying master control file '%s'\n", master_control_file);
@@ -895,7 +936,8 @@ do_standby_clone(void)
}
log_info("standby clone: master data directory '%s'\n", master_data_directory);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_data_directory, runtime_options.dest_dir, true);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user,
master_data_directory, runtime_options.dest_dir, true);
if (r != 0)
{
log_warning("standby clone: failed copying master data directory '%s'\n", master_data_directory);
@@ -903,10 +945,14 @@ do_standby_clone(void)
}
/*
* Copy tablespace locations, i'm doing this separately because i couldn't find and appropiate
* rsync option but besides we could someday make all these rsync happen concurrently
* Copy tablespace locations, i'm doing this separately because i couldn't
* find and appropiate rsync option but besides we could someday make all
* these rsync happen concurrently
*/
snprintf(sqlquery, QUERY_STR_LEN, "select spclocation from pg_tablespace where spcname not in ('pg_default', 'pg_global')");
sqlquery_snprintf(sqlquery,
"SELECT spclocation "
" FROM pg_tablespace "
" WHERE spcname NOT IN ('pg_default', 'pg_global')");
log_debug("standby clone: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -919,7 +965,8 @@ do_standby_clone(void)
{
strncpy(tblspc_dir, PQgetvalue(res, i, 0), MAXFILENAME);
log_info("standby clone: master tablespace '%s'\n", tblspc_dir);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user, tblspc_dir, tblspc_dir, true);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user,
tblspc_dir, tblspc_dir, true);
if (r != 0)
{
log_warning("standby clone: failed copying tablespace directory '%s'\n", tblspc_dir);
@@ -928,7 +975,8 @@ do_standby_clone(void)
}
log_info("standby clone: master config file '%s'\n", master_config_file);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_config_file, runtime_options.dest_dir, false);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user,
master_config_file, runtime_options.dest_dir, false);
if (r != 0)
{
log_warning("standby clone: failed copying master config file '%s'\n", master_config_file);
@@ -944,7 +992,8 @@ do_standby_clone(void)
}
log_info("standby clone: master ident file '%s'\n", master_ident_file);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_ident_file, runtime_options.dest_dir, false);
r = copy_remote_files(runtime_options.host, runtime_options.remote_user,
master_ident_file, runtime_options.dest_dir, false);
if (r != 0)
{
log_warning("standby clone: failed copying master ident file '%s'\n", master_ident_file);
@@ -963,7 +1012,7 @@ stop_backup:
log_notice("Finishing backup...\n");
snprintf(sqlquery, QUERY_STR_LEN, "SELECT pg_xlogfile_name(pg_stop_backup())");
sqlquery_snprintf(sqlquery, "SELECT pg_xlogfile_name(pg_stop_backup())");
log_debug("standby clone: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
@@ -976,16 +1025,35 @@ stop_backup:
}
last_wal_segment = PQgetvalue(res, 0, 0);
if (runtime_options.verbose)
{
printf(
_("%s requires primary to keep WAL files %s until at least %s\n"),
progname, first_wal_segment, last_wal_segment);
/*
* Only free the first_wal_segment since it was copied out of the
* pqresult.
*/
free(first_wal_segment);
first_wal_segment = NULL;
}
PQclear(res);
PQfinish(conn);
/* If the rsync failed then exit */
if (r != 0)
exit(ERR_BAD_RSYNC);
if (runtime_options.verbose)
printf(_("%s requires primary to keep WAL files %s until at least %s\n"),
progname, first_wal_segment, last_wal_segment);
log_info(_("%s requires primary to keep WAL files %s until at least %s\n"),
progname, first_wal_segment, last_wal_segment);
/* we need to create the pg_xlog sub directory too, i'm reusing a variable here */
snprintf(local_control_file, MAXFILENAME, "%s/pg_xlog", runtime_options.dest_dir);
/*
* We need to create the pg_xlog sub directory too, I'm reusing a variable
* here.
*/
maxlen_snprintf(local_control_file, "%s/pg_xlog", runtime_options.dest_dir);
if (!create_directory(local_control_file))
{
log_err(_("%s: couldn't create directory %s, you will need to do it manually...\n"),
@@ -994,7 +1062,7 @@ stop_backup:
}
/* Finally, write the recovery.conf file */
create_recovery_file(runtime_options.dest_dir);
create_recovery_file(runtime_options.dest_dir, NULL);
PQclear(res);
PQfinish(conn);
@@ -1011,10 +1079,10 @@ stop_backup:
static void
do_standby_promote(void)
{
PGconn *conn;
PGconn *conn;
PGresult *res;
char sqlquery[QUERY_STR_LEN];
char script[QUERY_STR_LEN];
char script[MAXLEN];
PGconn *old_master_conn;
int old_master_id;
@@ -1046,7 +1114,8 @@ do_standby_promote(void)
}
/* we also need to check if there isn't any master already */
old_master_conn = getMasterConnection(conn, options.node, options.cluster_name, &old_master_id);
old_master_conn = getMasterConnection(conn, options.node, options.cluster_name,
&old_master_id, NULL);
if (old_master_conn != NULL)
{
PQfinish(old_master_conn);
@@ -1058,7 +1127,7 @@ do_standby_promote(void)
printf(_("\n%s: Promoting standby...\n"), progname);
/* Get the data directory full path and the last subdirectory */
snprintf(sqlquery, QUERY_STR_LEN, "SELECT setting "
sqlquery_snprintf(sqlquery, "SELECT setting "
" FROM pg_settings WHERE name = 'data_directory'");
log_debug("standby promote: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
@@ -1073,8 +1142,8 @@ do_standby_promote(void)
PQclear(res);
PQfinish(conn);
snprintf(recovery_file_path, MAXFILENAME, "%s/%s", data_dir, RECOVERY_FILE);
snprintf(recovery_done_path, MAXFILENAME, "%s/%s", data_dir, RECOVERY_DONE_FILE);
maxlen_snprintf(recovery_file_path, "%s/%s", data_dir, RECOVERY_FILE);
maxlen_snprintf(recovery_done_path, "%s/%s", data_dir, RECOVERY_DONE_FILE);
rename(recovery_file_path, recovery_done_path);
/*
@@ -1083,7 +1152,7 @@ do_standby_promote(void)
* find an active server rather than one starting up. This may
* hang for up the default timeout (60 seconds).
*/
snprintf(script, QUERY_STR_LEN, "pg_ctl -D %s -w -m fast restart", data_dir);
maxlen_snprintf(script, "pg_ctl -D %s -w -m fast restart", data_dir);
r = system(script);
if (r != 0)
{
@@ -1102,7 +1171,6 @@ do_standby_promote(void)
log_err("\n%s: STANDBY PROMOTE successful. You should REINDEX any hash indexes you have.\n", progname);
}
PQfinish(conn);
return;
}
@@ -1110,11 +1178,11 @@ do_standby_promote(void)
static void
do_standby_follow(void)
{
PGconn *conn;
PGconn *conn;
PGresult *res;
char sqlquery[QUERY_STR_LEN];
char script[QUERY_STR_LEN];
char script[MAXLEN];
char master_conninfo[MAXLEN];
PGconn *master_conn;
int master_id;
@@ -1145,7 +1213,8 @@ do_standby_follow(void)
}
/* we also need to check if there is any master in the cluster */
master_conn = getMasterConnection(conn, options.node, options.cluster_name, &master_id);
master_conn = getMasterConnection(conn, options.node,
options.cluster_name, &master_id,(char *) &master_conninfo);
if (master_conn == NULL)
{
PQfinish(conn);
@@ -1190,11 +1259,10 @@ do_standby_follow(void)
strncpy(runtime_options.masterport, PQport(master_conn), MAXLEN);
PQfinish(master_conn);
if (runtime_options.verbose)
printf(_("\n%s: Changing standby's master...\n"), progname);
log_info(_("%s Changing standby's master"),progname);
/* Get the data directory full path */
snprintf(sqlquery, QUERY_STR_LEN, "SELECT setting "
sqlquery_snprintf(sqlquery, "SELECT setting "
" FROM pg_settings WHERE name = 'data_directory'");
log_debug("standby follow: %s\n", sqlquery);
res = PQexec(conn, sqlquery);
@@ -1210,12 +1278,12 @@ do_standby_follow(void)
PQfinish(conn);
/* write the recovery.conf file */
if (!create_recovery_file(data_dir))
if (!create_recovery_file(data_dir,NULL))
exit(ERR_BAD_CONFIG);
/* Finally, restart the service */
/* We assume the pg_ctl script is in the PATH */
snprintf(script, QUERY_STR_LEN, "pg_ctl -D %s -m fast restart", data_dir);
maxlen_snprintf(script, "pg_ctl -D %s -m fast restart", data_dir);
r = system(script);
if (r != 0)
{
@@ -1238,44 +1306,50 @@ void help(const char *progname)
{
printf(_("\n%s: Replicator manager \n"), progname);
printf(_("Usage:\n"));
printf(_(" %s [OPTIONS] master {register}\n"), progname);
printf(_(" %s [OPTIONS] standby {register|clone|promote|follow}\n"), progname);
printf(_(" %s [OPTIONS] master {register}\n"), progname);
printf(_(" %s [OPTIONS] standby {register|clone|promote|follow}\n"),
progname);
printf(_("\nGeneral options:\n"));
printf(_(" --help show this help, then exit\n"));
printf(_(" --version output version information, then exit\n"));
printf(_(" --verbose output verbose activity information\n"));
printf(_(" --help show this help, then exit\n"));
printf(_(" --version output version information, then exit\n"));
printf(_(" --verbose output verbose activity information\n"));
printf(_("\nConnection options:\n"));
printf(_(" -d, --dbname=DBNAME database to connect to\n"));
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
printf(_(" -p, --port=PORT database server port\n"));
printf(_(" -U, --username=USERNAME database user name to connect as\n"));
printf(_(" -d, --dbname=DBNAME database to connect to\n"));
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
printf(_(" -p, --port=PORT database server port\n"));
printf(_(" -U, --username=USERNAME database user name to connect as\n"));
printf(_("\nConfiguration options:\n"));
printf(_(" -D, --data-dir=DIR local directory where the files will be copied to\n"));
printf(_(" -f, --config_file=PATH path to the configuration file\n"));
printf(_(" -R, --remote-user=USERNAME database server username for rsync\n"));
printf(_(" -w, --wal-keep-segments=VALUE minimum value for the GUC wal_keep_segments (default: 5000)\n"));
printf(_(" -F, --force force potentially dangerous operations to happen\n"));
printf(_(" -D, --data-dir=DIR local directory where the files will be copied to\n"));
printf(_(" -f, --config_file=PATH path to the configuration file\n"));
printf(_(" -R, --remote-user=USERNAME database server username for rsync\n"));
printf(_(" -w, --wal-keep-segments=VALUE minimum value for the GUC wal_keep_segments (default: 5000)\n"));
printf(_(" -F, --force force potentially dangerous operations to happen\n"));
printf(_("\n%s performs some tasks like clone a node, promote it "), progname);
printf(_("or making follow another node and then exits.\n"));
printf(_("COMMANDS:\n"));
printf(_(" master register - registers the master in a cluster\n"));
printf(_(" standby register - registers a standby in a cluster\n"));
printf(_(" standby clone [node] - allows creation of a new standby\n"));
printf(_(" standby promote - allows manual promotion of a specific standby into a "));
printf(_(" master register - registers the master in a cluster\n"));
printf(_(" standby register - registers a standby in a cluster\n"));
printf(_(" standby clone [node] - allows creation of a new standby\n"));
printf(_(" standby promote - allows manual promotion of a specific standby into a "));
printf(_("new master in the event of a failover\n"));
printf(_(" standby follow - allows the standby to re-point itself to a new master\n"));
printf(_(" standby follow - allows the standby to re-point itself to a new master\n"));
}
/*
* Creates a recovery file for a standby.
*
* Writes master_conninfo to recovery.conf if is non-NULL
*/
static bool
create_recovery_file(const char *data_dir)
create_recovery_file(const char *data_dir, char *master_conninfo)
{
FILE *recovery_file;
char recovery_file_path[MAXFILENAME];
char recovery_file_path[MAXLEN];
char line[MAXLEN];
snprintf(recovery_file_path, MAXFILENAME, "%s/%s", data_dir, RECOVERY_FILE);
maxlen_snprintf(recovery_file_path, "%s/%s", data_dir, RECOVERY_FILE);
recovery_file = fopen(recovery_file_path, "w");
if (recovery_file == NULL)
@@ -1284,7 +1358,7 @@ create_recovery_file(const char *data_dir)
return false;
}
snprintf(line, MAXLEN, "standby_mode = 'on'\n");
maxlen_snprintf(line, "standby_mode = 'on'\n");
if (fputs(line, recovery_file) == EOF)
{
log_err("recovery file could not be written, it could be necessary to create it manually\n");
@@ -1292,7 +1366,34 @@ create_recovery_file(const char *data_dir)
return false;
}
snprintf(line, MAXLEN, "primary_conninfo = 'host=%s port=%s'\n", runtime_options.host, runtime_options.masterport);
maxlen_snprintf(line, "primary_conninfo = 'host=%s port=%s'\n", runtime_options.host, runtime_options.masterport);
/*
* Template a password into the connection string in recovery.conf.
* Sometimes this is passed by the user explicitly, and otherwise we try to
* get it into the environment
*
* XXX: This is pretty dirty, at least push this up to the caller rather
* than hitting environment variables at this level.
*/
if (master_conninfo == NULL)
{
char *password = getenv("PGPASSWORD");
if (password == NULL)
{
fprintf(stderr,
_("%s: Panic! PGPASSWORD not set, how can we get here?\n"),
progname);
exit(ERR_BAD_PASSWORD);
}
maxlen_snprintf(line,
"primary_conninfo = 'host=%s port=%s password=%s'\n",
runtime_options.host, runtime_options.masterport,
password);
}
if (fputs(line, recovery_file) == EOF)
{
log_err("recovery file could not be written, it could be necessary to create it manually\n");
@@ -1308,39 +1409,42 @@ create_recovery_file(const char *data_dir)
static int
copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_path, bool is_directory)
copy_remote_files(char *host, char *remote_user, char *remote_path,
char *local_path, bool is_directory)
{
char script[QUERY_STR_LEN];
char rsync_flags[QUERY_STR_LEN];
char host_string[QUERY_STR_LEN];
int r;
char script[MAXLEN];
char rsync_flags[MAXLEN];
char host_string[MAXLEN];
int r;
if (strnlen(options.rsync_options, QUERY_STR_LEN) == 0)
snprintf(rsync_flags, QUERY_STR_LEN, "--archive --checksum --compress --progress --rsh=ssh");
if (strnlen(options.rsync_options, MAXLEN) == 0)
maxlen_snprintf(
rsync_flags, "%s",
"--archive --checksum --compress --progress --rsh=ssh");
else
strncpy(rsync_flags, options.rsync_options, QUERY_STR_LEN);
maxlen_snprintf(rsync_flags, "%s", options.rsync_options);
if (runtime_options.force)
strcat(rsync_flags, " --delete");
if (remote_user == NULL)
{
snprintf(host_string, QUERY_STR_LEN, "%s",host);
maxlen_snprintf(host_string, "%s", host);
}
else
{
snprintf(host_string, QUERY_STR_LEN, "%s@%s",remote_user,host);
maxlen_snprintf(host_string,"%s@%s",remote_user,host);
}
if (is_directory)
{
strcat(rsync_flags, " --exclude=pg_xlog* --exclude=pg_control --exclude=*.pid");
snprintf(script, QUERY_STR_LEN, "rsync %s %s:%s/* %s",
maxlen_snprintf(script, "rsync %s %s:%s/* %s",
rsync_flags, host_string, remote_path, local_path);
}
else
{
snprintf(script, QUERY_STR_LEN, "rsync %s %s:%s %s/.",
maxlen_snprintf(script, "rsync %s %s:%s %s/.",
rsync_flags, host_string, remote_path, local_path);
}
@@ -1450,13 +1554,26 @@ check_parameters_for_action(const int action)
break;
case STANDBY_CLONE:
/*
* To clone a master into a standby we need connection parameters
* repmgr.conf is useless because we don't have a server running
* in the standby
* Issue a friendly notice that the configuration file is not
* necessary nor read at all in when performing a STANDBY CLONE
* action.
*/
if (runtime_options.config_file[0])
{
log_notice("Only command line parameters for the connection to the master are used when issuing a STANDBY CLONE command.\n");
log_notice("Only command line parameters for the connection "
"to the master are used when issuing a STANDBY CLONE command. "
"The passed configuration file is neither required nor used\n");
}
/*
* To clone a master into a standby we need connection parameters
* repmgr.conf is useless because we don't have a server running in
* the standby; warn the user, but keep going.
*/
if (runtime_options.host == NULL)
{
log_notice("You need to use connection parameters to "
"the master when issuing a STANDBY CLONE command.");
ok = false;
}
need_a_node = false;
break;