-
Notifications
You must be signed in to change notification settings - Fork 77
/
init.c
215 lines (184 loc) · 5.43 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*-------------------------------------------------------------------------
*
* init.c: manage backup catalog.
*
* Copyright (c) 2009-2023, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
*-------------------------------------------------------------------------
*/
#include "pg_rman.h"
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include <unistd.h>
#include <dirent.h>
static void parse_postgresql_conf(const char *path, char **log_directory,
char **archive_command);
/*
* selects function for scandir.
*/
static int selects(const struct dirent *dir)
{
return dir->d_name[0] != '.';
}
/*
* Initialize backup catalog.
*/
int
do_init(void)
{
char path[MAXPGPATH];
char *log_directory = NULL;
char *archive_command = NULL;
FILE *fp;
struct dirent **dp;
int results;
uint64 sysid;
ControlFileData *controlFile;
bool crc_ok;
if (access(backup_path, F_OK) == 0)
{
results = scandir(backup_path, &dp, selects, NULL);
if(results != 0)
ereport(ERROR,
(errcode(ERROR),
errmsg("backup catalog already exist and it's not empty")));
}
if (pgdata == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: PGDATA (-D, --pgdata)")));
/* create backup catalog root directory */
dir_create_dir(backup_path, DIR_PERMISSION);
/* create directories for backup of online files */
join_path_components(path, backup_path, RESTORE_WORK_DIR);
dir_create_dir(path, DIR_PERMISSION);
snprintf(path, lengthof(path), "%s/%s/%s", backup_path, RESTORE_WORK_DIR,
PG_XLOG_DIR);
dir_create_dir(path, DIR_PERMISSION);
snprintf(path, lengthof(path), "%s/%s/%s", backup_path, RESTORE_WORK_DIR,
SRVLOG_DIR);
dir_create_dir(path, DIR_PERMISSION);
/* create directory for timeline history files */
join_path_components(path, backup_path, TIMELINE_HISTORY_DIR);
dir_create_dir(path, DIR_PERMISSION);
/* read postgresql.conf */
if (pgdata)
{
join_path_components(path, pgdata, "postgresql.conf");
parse_postgresql_conf(path, &log_directory, &archive_command);
}
/* get system identifier of the current database.*/
controlFile = get_controlfile(pgdata, &crc_ok);
if (!crc_ok)
ereport(WARNING,
(errmsg("control file appears to be corrupt"),
errdetail("Calculated CRC checksum does not match value stored in file.")));
sysid = controlFile->system_identifier;
pg_free(controlFile);
/* register system identifier of target database. */
join_path_components(path, backup_path, SYSTEM_IDENTIFIER_FILE);
fp = fopen(path, "wt");
if (fp == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not create system identifier file: %s", strerror(errno))));
else
fprintf(fp, "SYSTEM_IDENTIFIER='" UINT64_FORMAT "'\n", sysid);
fclose(fp);
/* create pg_rman.ini */
join_path_components(path, backup_path, PG_RMAN_INI_FILE);
fp = fopen(path, "wt");
if (fp == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not create pg_rman.ini: %s", strerror(errno))));
/* set ARCLOG_PATH referred with archive_command */
if (arclog_path == NULL && archive_command && archive_command[0])
{
char *command = pgut_strdup(archive_command);
char *begin;
char *end;
char *fname;
/* example: 'cp "%p" /path/to/arclog/"%f"' */
for (begin = command; *begin;)
{
begin = begin + strspn(begin, " \n\r\t\v");
end = begin + strcspn(begin, " \n\r\t\v");
*end = '\0';
if ((fname = strstr(begin, "%f")) != NULL)
{
while (strchr(" \n\r\t\v\"'", *begin))
begin++;
fname--;
while (fname > begin && strchr(" \n\r\t\v\"'/", fname[-1]))
fname--;
*fname = '\0';
if (is_absolute_path(begin))
arclog_path = pgut_strdup(begin);
break;
}
begin = end + 1;
}
free(command);
}
if (arclog_path)
{
fprintf(fp, "ARCLOG_PATH='%s'\n", arclog_path);
elog(INFO, "ARCLOG_PATH is set to '%s'", arclog_path);
}
else if (archive_command && archive_command[0])
ereport(WARNING,
(errmsg("ARCLOG_PATH is not set yet"),
errdetail("Pg_rman failed to parse archive_command '%s'.", archive_command),
errhint("Please set ARCLOG_PATH in pg_rman.ini or environmental variable.")));
else
ereport(WARNING,
(errmsg("ARCLOG_PATH is not set yet"),
errdetail("The archive_command is not set in postgresql.conf."),
errhint("Please set ARCLOG_PATH in pg_rman.ini or environmental variable.")));
/* set SRVLOG_PATH referred with log_directory */
if (srvlog_path == NULL)
{
if (log_directory)
{
if (is_absolute_path(log_directory))
srvlog_path = pgut_strdup(log_directory);
else
{
srvlog_path = pgut_malloc(MAXPGPATH);
join_path_components(srvlog_path, pgdata, log_directory);
}
}
else if (pgdata)
{
/* default: log_directory = 'log' if PostgreSQL version is 10 or above */
srvlog_path = pgut_malloc(MAXPGPATH);
join_path_components(srvlog_path, pgdata, "log");
}
}
if (srvlog_path)
{
fprintf(fp, "SRVLOG_PATH='%s'\n", srvlog_path);
elog(INFO, "SRVLOG_PATH is set to '%s'", srvlog_path);
}
fprintf(fp, "\n");
fclose(fp);
free(archive_command);
free(log_directory);
return 0;
}
static void
parse_postgresql_conf(const char *path,
char **log_directory,
char **archive_command)
{
pgut_option options[] =
{
{ 's', 0, "log_directory" , NULL, SOURCE_ENV },
{ 's', 0, "archive_command" , NULL, SOURCE_ENV },
{ 0 }
};
options[0].var = log_directory;
options[1].var = archive_command;
pgut_readopt(path, options, DEBUG); /* ignore unknown options */
}