Skip to content

Commit 1edaabf

Browse files
committed
Format change. pg_probackup.conf contains more info now. Options change: configure subcommand is implemented
1 parent 12cc3a1 commit 1edaabf

File tree

7 files changed

+186
-46
lines changed

7 files changed

+186
-46
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
PROGRAM = pg_probackup
22
OBJS = backup.o \
33
catalog.o \
4+
configure.o \
45
data.o \
56
delete.o \
67
dir.o \

configure.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* configure.c: - manage backup catalog.
4+
*
5+
* Portions Copyright (c) 2017-2017, Postgres Professional
6+
*
7+
*-------------------------------------------------------------------------
8+
*/
9+
10+
#include "pg_probackup.h"
11+
12+
/* Set configure options */
13+
int
14+
do_configure(bool show_only)
15+
{
16+
pgBackupConfig *config = readBackupCatalogConfigFile();
17+
if (pgdata)
18+
config->pgdata = pgdata;
19+
if (pgut_dbname)
20+
config->pgdatabase = pgut_dbname;
21+
if (host)
22+
config->pghost = host;
23+
if (port)
24+
config->pgport = port;
25+
if (username)
26+
config->pguser = username;
27+
28+
if (retention_redundancy)
29+
config->retention_redundancy = retention_redundancy;
30+
if (retention_window)
31+
config->retention_window = retention_window;
32+
33+
if (show_only)
34+
writeBackupCatalogConfig(stderr, config);
35+
else
36+
writeBackupCatalogConfigFile(config);
37+
38+
return 0;
39+
}
40+
41+
void
42+
pgBackupConfigInit(pgBackupConfig *config)
43+
{
44+
config->system_identifier = 0;
45+
config->pgdata = NULL;
46+
config->pgdatabase = NULL;
47+
config->pghost = NULL;
48+
config->pgport = NULL;
49+
config->pguser = NULL;
50+
51+
config->retention_redundancy = 0;
52+
config->retention_window = 0;
53+
}
54+
55+
void
56+
writeBackupCatalogConfig(FILE *out, pgBackupConfig *config)
57+
{
58+
fprintf(out, "#Backup instance info\n");
59+
fprintf(out, "PGDATA = %s\n", config->pgdata);
60+
fprintf(out, "system-identifier = %li\n", config->system_identifier);
61+
62+
fprintf(out, "#Connection parameters:\n");
63+
if (config->pgdatabase)
64+
fprintf(out, "PGDATABASE = %s\n", config->pgdatabase);
65+
if (config->pghost)
66+
fprintf(out, "PGHOST = %s\n", config->pghost);
67+
if (config->pgport)
68+
fprintf(out, "PGPORT = %s\n", config->pgport);
69+
if (config->pguser)
70+
fprintf(out, "PGUSER = %s\n", config->pguser);
71+
fprintf(out, "#Retention parameters:\n");
72+
if (config->retention_redundancy)
73+
fprintf(out, "retention-redundancy = %u\n", config->retention_redundancy);
74+
if (config->retention_window)
75+
fprintf(out, "retention-window = %u\n", config->retention_window);
76+
77+
}
78+
79+
void
80+
writeBackupCatalogConfigFile(pgBackupConfig *config)
81+
{
82+
char path[MAXPGPATH];
83+
FILE *fp;
84+
85+
join_path_components(path, backup_path, BACKUPS_DIR);
86+
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
87+
fp = fopen(path, "wt");
88+
if (fp == NULL)
89+
elog(ERROR, "cannot create %s: %s",
90+
BACKUP_CATALOG_CONF_FILE, strerror(errno));
91+
92+
writeBackupCatalogConfig(fp, config);
93+
fclose(fp);
94+
}
95+
96+
97+
pgBackupConfig*
98+
readBackupCatalogConfigFile(void)
99+
{
100+
pgBackupConfig *config = pgut_new(pgBackupConfig);
101+
char path[MAXPGPATH];
102+
103+
pgut_option options[] =
104+
{
105+
/* configure options */
106+
{ 'U', 0, "system-identifier", &(config->system_identifier), SOURCE_FILE_STRICT },
107+
{ 's', 0, "pgdata", &(config->pgdata), SOURCE_FILE_STRICT },
108+
{ 's', 0, "pgdatabase", &(config->pgdatabase), SOURCE_FILE_STRICT },
109+
{ 's', 0, "pghost", &(config->pghost), SOURCE_FILE_STRICT },
110+
{ 's', 0, "pgport", &(config->pgport), SOURCE_FILE_STRICT },
111+
{ 's', 0, "pguser", &(config->pguser), SOURCE_FILE_STRICT },
112+
{ 'u', 0, "retention-redundancy", &(config->retention_redundancy),SOURCE_FILE_STRICT },
113+
{ 'u', 0, "retention-window", &(config->retention_window), SOURCE_FILE_STRICT },
114+
{0}
115+
};
116+
117+
join_path_components(path, backup_path, BACKUPS_DIR);
118+
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
119+
120+
pgBackupConfigInit(config);
121+
pgut_readopt(path, options, ERROR);
122+
123+
return config;
124+
125+
}

init.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*-------------------------------------------------------------------------
22
*
3-
* init.c: manage backup catalog.
3+
* init.c: - initialize backup catalog.
44
*
55
* Portions Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
66
* Portions Copyright (c) 2015-2017, Postgres Professional
@@ -30,11 +30,10 @@ do_init(void)
3030
{
3131
char path[MAXPGPATH];
3232
char arclog_path_dir[MAXPGPATH];
33-
FILE *fp;
34-
uint64 id;
3533

3634
struct dirent **dp;
3735
int results;
36+
pgBackupConfig *config = pgut_new(pgBackupConfig);
3837

3938
/* PGDATA is always required */
4039
if (pgdata == NULL)
@@ -49,7 +48,7 @@ do_init(void)
4948
}
5049

5150
/* Read system_identifier from PGDATA */
52-
id = get_system_identifier();
51+
system_identifier = get_system_identifier();
5352

5453
/* create backup catalog root directory */
5554
dir_create_dir(backup_path, DIR_PERMISSION);
@@ -62,16 +61,14 @@ do_init(void)
6261
join_path_components(arclog_path_dir, backup_path, "wal");
6362
dir_create_dir(arclog_path_dir, DIR_PERMISSION);
6463

65-
/* create BACKUP_CATALOG_CONF_FILE */
66-
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
67-
fp = fopen(path, "wt");
68-
if (fp == NULL)
69-
elog(ERROR, "cannot create %s: %s",
70-
BACKUP_CATALOG_CONF_FILE, strerror(errno));
71-
72-
fprintf(fp, "system-identifier = %li\n", id);
73-
fprintf(fp, "\n");
74-
fclose(fp);
64+
/*
65+
* Wite initial config. system-identifier and pgdata are set in
66+
* init subcommand and will never be updated.
67+
*/
68+
pgBackupConfigInit(config);
69+
config->system_identifier = system_identifier;
70+
config->pgdata = pgdata;
71+
writeBackupCatalogConfigFile(config);
7572

7673
return 0;
7774
}

pg_probackup.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ static pgut_option options[] =
9090
/* other */
9191
{ 'U', 15, "system-identifier", &system_identifier, SOURCE_FILE_STRICT },
9292

93-
{ 's', 'd', "dbname" , &pgut_dbname, SOURCE_CMDLINE },
94-
{ 's', 'h', "host" , &host, SOURCE_CMDLINE },
95-
{ 's', 'p', "port" , &port, SOURCE_CMDLINE },
96-
{ 's', 'U', "username" , &username, SOURCE_CMDLINE },
93+
{ 's', 'd', "pgdatabase" , &pgut_dbname, SOURCE_CMDLINE },
94+
{ 's', 'h', "pghost" , &host, SOURCE_CMDLINE },
95+
{ 's', 'p', "pgport" , &port, SOURCE_CMDLINE },
96+
{ 's', 'U', "pguser" , &username, SOURCE_CMDLINE },
9797
{ 'b', 'q', "quiet" , &quiet, SOURCE_CMDLINE },
9898
{ 'b', 'v', "verbose" , &verbose, SOURCE_CMDLINE },
9999
{ 'B', 'w', "no-password" , &prompt_password, SOURCE_CMDLINE },
@@ -148,9 +148,13 @@ main(int argc, char *argv[])
148148
/* Parse command line arguments */
149149
i = pgut_getopt(argc, argv, options);
150150

151-
/* BACKUP_PATH is always required */
152151
if (backup_path == NULL)
153-
elog(ERROR, "required parameter not specified: BACKUP_PATH (-B, --backup-path)");
152+
{
153+
/* Try to read BACKUP_PATH from environment variable */
154+
backup_path = getenv("BACKUP_PATH");
155+
if (backup_path == NULL)
156+
elog(ERROR, "required parameter not specified: BACKUP_PATH (-B, --backup-path)");
157+
}
154158
else
155159
{
156160
char path[MAXPGPATH];
@@ -162,8 +166,16 @@ main(int argc, char *argv[])
162166
if (rc != -1 && !S_ISDIR(stat_buf.st_mode))
163167
elog(ERROR, "-B, --backup-path must be a path to directory");
164168

165-
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
166-
pgut_readopt(path, options, ERROR);
169+
/* Do not read options from file or env if we're going to set them */
170+
if (backup_subcmd != CONFIGURE)
171+
{
172+
/* Read options from configuration file */
173+
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
174+
pgut_readopt(path, options, ERROR);
175+
176+
/* Read environment variables */
177+
pgut_getopt_env(options);
178+
}
167179
}
168180

169181
if (backup_id_string_param != NULL)
@@ -225,19 +237,13 @@ main(int argc, char *argv[])
225237
case DELETE:
226238
return do_delete(current.backup_id);
227239
case CONFIGURE:
228-
elog(ERROR, "not implemented yet");
240+
/* TODO fixit */
241+
if (argc == 4)
242+
return do_configure(true);
243+
else
244+
return do_configure(false);
229245
}
230246

231-
// if (pg_strcasecmp(cmd, "retention") == 0)
232-
// {
233-
// if (subcmd == NULL)
234-
// elog(ERROR, "you must specify retention command");
235-
// else if (pg_strcasecmp(subcmd, "show") == 0)
236-
// return do_retention_show();
237-
// else if (pg_strcasecmp(subcmd, "purge") == 0)
238-
// return do_retention_purge();
239-
// }
240-
241247
return 0;
242248
}
243249

pg_probackup.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ typedef enum ProbackupSubcmd
114114
#define INVALID_BACKUP_ID 0
115115
#define BYTES_INVALID (-1)
116116

117+
typedef struct pgBackupConfig
118+
{
119+
uint64 system_identifier;
120+
char *pgdata;
121+
const char *pgdatabase;
122+
const char *pghost;
123+
const char *pgport;
124+
const char *pguser;
125+
126+
uint32 retention_redundancy;
127+
uint32 retention_window;
128+
} pgBackupConfig;
129+
117130
/* Information about single backup stored in backup.conf */
118131
typedef struct pgBackup
119132
{
@@ -262,6 +275,13 @@ extern void opt_tablespace_map(pgut_option *opt, const char *arg);
262275
/* in init.c */
263276
extern int do_init(void);
264277

278+
/* in configure.c */
279+
extern int do_configure(bool show_only);
280+
extern void pgBackupConfigInit(pgBackupConfig *config);
281+
extern void writeBackupCatalogConfig(FILE *out, pgBackupConfig *config);
282+
extern void writeBackupCatalogConfigFile(pgBackupConfig *config);
283+
extern pgBackupConfig* readBackupCatalogConfigFile(void);
284+
265285
/* in show.c */
266286
extern int do_show(time_t requested_backup_id);
267287

pgut/pgut.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,8 @@ longopts_to_optstring(const struct option opts[])
505505
return result;
506506
}
507507

508-
/*
509-
* Read options from environmental variables.
510-
* Do not overwrite in option was already set via command line option.
511-
*/
512-
static void
513-
option_from_env(pgut_option options[])
508+
void
509+
pgut_getopt_env(pgut_option options[])
514510
{
515511
size_t i;
516512

@@ -523,9 +519,6 @@ option_from_env(pgut_option options[])
523519
if (opt->source > SOURCE_ENV || opt->allowed < SOURCE_ENV)
524520
continue;
525521

526-
if (strcmp(opt->lname, "backup-path") == 0)
527-
value = getenv("BACKUP_PATH");
528-
529522
if (strcmp(opt->lname, "pgdata") == 0)
530523
value = getenv("PGDATA");
531524
if (strcmp(opt->lname, "port") == 0)
@@ -534,7 +527,7 @@ option_from_env(pgut_option options[])
534527
value = getenv("PGHOST");
535528
if (strcmp(opt->lname, "username") == 0)
536529
value = getenv("PGUSER");
537-
if (strcmp(opt->lname, "dbname") == 0)
530+
if (strcmp(opt->lname, "pgdatabase") == 0)
538531
{
539532
value = getenv("PGDATABASE");
540533
if (value == NULL)
@@ -575,9 +568,6 @@ pgut_getopt(int argc, char **argv, pgut_option options[])
575568
assign_option(opt, optarg, SOURCE_CMDLINE);
576569
}
577570

578-
/* Read environment variables */
579-
option_from_env(options);
580-
581571
init_cancel_handler();
582572
atexit(on_cleanup);
583573

pgut/pgut.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ extern bool interrupted;
9292
extern void help(bool details);
9393
extern int pgut_getopt(int argc, char **argv, pgut_option options[]);
9494
extern void pgut_readopt(const char *path, pgut_option options[], int elevel);
95+
extern void pgut_getopt_env(pgut_option options[]);
9596
extern void pgut_atexit_push(pgut_atexit_callback callback, void *userdata);
9697
extern void pgut_atexit_pop(pgut_atexit_callback callback, void *userdata);
9798

0 commit comments

Comments
 (0)