forked from ossc-db/pg_rman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_rman.c
384 lines (344 loc) · 12.4 KB
/
pg_rman.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*-------------------------------------------------------------------------
*
* pg_rman.c: Backup/Recovery manager for PostgreSQL.
*
* Copyright (c) 2009-2021, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
*-------------------------------------------------------------------------
*/
#include "pg_rman.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include "common/logging.h"
const char *PROGRAM_VERSION = "1.3.11";
const char *PROGRAM_URL = "http://github.com/ossc-db/pg_rman";
const char *PROGRAM_ISSUES = "http://github.com/ossc-db/pg_rman/issues";
/* path configuration */
char *backup_path;
char *pgdata;
char *arclog_path;
char *srvlog_path;
/* common configuration */
bool verbose = false;
bool progress = false;
bool check = false;
/* directory configuration */
pgBackup current;
/* backup configuration */
static bool smooth_checkpoint;
static int keep_arclog_files = KEEP_INFINITE;
static int keep_arclog_days = KEEP_INFINITE;
static int keep_srvlog_files = KEEP_INFINITE;
static int keep_srvlog_days = KEEP_INFINITE;
static int keep_data_generations = KEEP_INFINITE;
static int keep_data_days = KEEP_INFINITE;
static char *standby_host = NULL;
static char *standby_port = NULL;
/* restore configuration */
static char *target_time;
static char *target_xid;
static char *target_inclusive;
static char *target_tli_string;
static bool is_hard_copy = false;
/* delete configuration */
static bool force;
/* show configuration */
static bool show_all = false;
static void opt_backup_mode(pgut_option *opt, const char *arg);
static void parse_range(pgBackupRange *range, const char *arg1, const char *arg2);
static pgut_option options[] =
{
/* directory options */
{ 's', 'D', "pgdata" , &pgdata , SOURCE_ENV },
{ 's', 'A', "arclog-path" , &arclog_path , SOURCE_ENV },
{ 's', 'B', "backup-path" , &backup_path , SOURCE_ENV },
{ 's', 'S', "srvlog-path" , &srvlog_path , SOURCE_ENV },
/* common options */
{ 'b', 'v', "verbose" , &verbose },
{ 'b', 'P', "progress" , &progress },
{ 'b', 'c', "check" , &check },
/* backup options */
{ 'f', 'b', "backup-mode" , opt_backup_mode , SOURCE_ENV },
{ 'b', 's', "with-serverlog" , ¤t.with_serverlog , SOURCE_ENV },
{ 'b', 'Z', "compress-data" , ¤t.compress_data , SOURCE_ENV },
{ 'b', 'C', "smooth-checkpoint" , &smooth_checkpoint , SOURCE_ENV },
{ 'b', 'F', "full-backup-on-error" , ¤t.full_backup_on_error , SOURCE_ENV },
{ 's', 12, "standby-host" , &standby_host , SOURCE_ENV },
{ 's', 13, "standby-port" , &standby_port , SOURCE_ENV },
/* delete options */
{ 'b', 'f', "force" , &force , SOURCE_ENV },
/* options with only long name (keep-xxx) */
{ 'i', 1, "keep-data-generations" , &keep_data_generations, SOURCE_ENV },
{ 'i', 2, "keep-data-days" , &keep_data_days , SOURCE_ENV },
{ 'i', 3, "keep-arclog-files" , &keep_arclog_files , SOURCE_ENV },
{ 'i', 4, "keep-arclog-days" , &keep_arclog_days , SOURCE_ENV },
{ 'i', 5, "keep-srvlog-files" , &keep_srvlog_files , SOURCE_ENV },
{ 'i', 6, "keep-srvlog-days" , &keep_srvlog_days , SOURCE_ENV },
/* restore options */
{ 's', 7, "recovery-target-time" , &target_time , SOURCE_ENV },
{ 's', 8, "recovery-target-xid" , &target_xid , SOURCE_ENV },
{ 's', 9, "recovery-target-inclusive" , &target_inclusive , SOURCE_ENV },
{ 's', 10, "recovery-target-timeline" , &target_tli_string, SOURCE_ENV },
{ 'b', 11, "hard-copy" , &is_hard_copy , SOURCE_ENV },
/* catalog options */
{ 'b', 'a', "show-all" , &show_all },
{ 0 }
};
/*
* Entry point of pg_rman command.
*/
int
main(int argc, char *argv[])
{
const char *cmd = NULL;
const char *range1 = NULL;
const char *range2 = NULL;
bool show_detail = false;
pgBackupRange range;
int i;
/* do not buffer progress messages */
setvbuf(stdout, 0, _IONBF, 0); /* TODO: remove this */
/* initialize configuration */
catalog_init_config(¤t);
/* overwrite configuration with command line arguments */
i = pgut_getopt(argc, argv, options);
pg_logging_init(PROGRAM_NAME);
/* BACKUP_PATH is always required */
if (backup_path == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: BACKUP_PATH (-B, --backup-path)")));
for (; i < argc; i++)
{
if (cmd == NULL)
cmd = argv[i];
else if (pg_strcasecmp(argv[i], "detail") == 0 &&
pg_strcasecmp(cmd, "show") == 0)
show_detail = true;
else if (range1 == NULL)
range1 = argv[i];
else if (range2 == NULL)
range2 = argv[i];
else
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("too many arguments")));
}
/* command argument (backup/restore/show/...) is required. */
if (cmd == NULL)
{
help(false);
return HELP;
}
/* get object range argument if any */
if (range1 && range2)
parse_range(&range, range1, range2);
else if (range1)
parse_range(&range, range1, "");
else
range.begin = range.end = 0;
/* Read default configuration from file. */
if (backup_path)
{
char path[MAXPGPATH];
/* Check if backup_path is directory. */
struct stat stat_buf;
int rc = stat(backup_path, &stat_buf);
/* If rc == -1, there is no file or directory. So it's OK. */
if(rc != -1 && !S_ISDIR(stat_buf.st_mode))
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("-B, --backup-path must be a path to directory")));
join_path_components(path, backup_path, PG_RMAN_INI_FILE);
pgut_readopt(path, options, ERROR_ARGS);
}
/* path must be absolute */
if (backup_path != NULL && !is_absolute_path(backup_path))
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("-B, --backup-path must be an absolute path")));
if (pgdata != NULL && !is_absolute_path(pgdata))
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("-D, --pgdata must be an absolute path")));
if (arclog_path != NULL && !is_absolute_path(arclog_path))
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("-A, --arclog-path must be an absolute path")));
if (srvlog_path != NULL && !is_absolute_path(srvlog_path))
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("-S, --srvlog-path must be an absolute path")));
/* setup exclusion list for file search */
for (i = 0; pgdata_exclude[i]; i++) /* find first empty slot */
;
if (arclog_path)
pgdata_exclude[i++] = arclog_path;
if (srvlog_path)
pgdata_exclude[i++] = srvlog_path;
/* do actual operation */
if (pg_strcasecmp(cmd, "init") == 0)
return do_init();
else if (pg_strcasecmp(cmd, "backup") == 0)
{
pgBackupOption bkupopt;
bkupopt.smooth_checkpoint = smooth_checkpoint;
bkupopt.keep_arclog_files = keep_arclog_files;
bkupopt.keep_arclog_days = keep_arclog_days;
bkupopt.keep_srvlog_files = keep_srvlog_files;
bkupopt.keep_srvlog_days = keep_srvlog_days;
bkupopt.keep_data_generations = keep_data_generations;
bkupopt.keep_data_days = keep_data_days;
bkupopt.standby_host = standby_host;
bkupopt.standby_port = standby_port;
return do_backup(bkupopt);
}
else if (pg_strcasecmp(cmd, "restore") == 0)
return do_restore(target_time, target_xid,
target_inclusive, target_tli_string, is_hard_copy);
else if (pg_strcasecmp(cmd, "show") == 0)
return do_show(&range, show_detail, show_all);
else if (pg_strcasecmp(cmd, "validate") == 0)
return do_validate(&range);
else if (pg_strcasecmp(cmd, "delete") == 0)
return do_delete(&range, force);
else if (pg_strcasecmp(cmd, "purge") == 0)
return do_purge();
else
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("invalid command \"%s\"", cmd)));
return 0;
}
void
pgut_help(bool details)
{
printf(_("%s manage backup/recovery of PostgreSQL database.\n\n"), PROGRAM_NAME);
printf(_("Usage:\n"));
printf(_(" %s OPTION init\n"), PROGRAM_NAME);
printf(_(" %s OPTION backup\n"), PROGRAM_NAME);
printf(_(" %s OPTION restore\n"), PROGRAM_NAME);
printf(_(" %s OPTION show [DATE]\n"), PROGRAM_NAME);
printf(_(" %s OPTION show detail [DATE]\n"), PROGRAM_NAME);
printf(_(" %s OPTION validate [DATE]\n"), PROGRAM_NAME);
printf(_(" %s OPTION delete DATE\n"), PROGRAM_NAME);
printf(_(" %s OPTION purge\n"), PROGRAM_NAME);
if (!details)
return;
printf(_("\nCommon Options:\n"));
printf(_(" -D, --pgdata=PATH location of the database storage area\n"));
printf(_(" -A, --arclog-path=PATH location of archive WAL storage area\n"));
printf(_(" -S, --srvlog-path=PATH location of server log storage area\n"));
printf(_(" -B, --backup-path=PATH location of the backup storage area\n"));
printf(_(" -c, --check show what would have been done\n"));
printf(_(" -v, --verbose show what detail messages\n"));
printf(_(" -P, --progress show progress of processed files\n"));
printf(_("\nBackup options:\n"));
printf(_(" -b, --backup-mode=MODE full, incremental, or archive\n"));
printf(_(" -s, --with-serverlog also backup server log files\n"));
printf(_(" -Z, --compress-data compress data backup with zlib\n"));
printf(_(" -C, --smooth-checkpoint do smooth checkpoint before backup\n"));
printf(_(" -F, --full-backup-on-error switch to full backup mode\n"));
printf(_(" if pg_rman cannot find validate full backup\n"));
printf(_(" on current timeline\n"));
printf(_(" NOTE: this option is only used in --backup-mode=incremental or archive.\n"));
printf(_(" --keep-data-generations=NUM keep NUM generations of full data backup\n"));
printf(_(" --keep-data-days=NUM keep enough data backup to recover to N days ago\n"));
printf(_(" --keep-arclog-files=NUM keep NUM of archived WAL\n"));
printf(_(" --keep-arclog-days=DAY keep archived WAL modified in DAY days\n"));
printf(_(" --keep-srvlog-files=NUM keep NUM of serverlogs\n"));
printf(_(" --keep-srvlog-days=DAY keep serverlog modified in DAY days\n"));
printf(_(" --standby-host=HOSTNAME standby host when taking backup from standby\n"));
printf(_(" --standby-port=PORT standby port when taking backup from standby\n"));
printf(_("\nRestore options:\n"));
printf(_(" --recovery-target-time time stamp up to which recovery will proceed\n"));
printf(_(" --recovery-target-xid transaction ID up to which recovery will proceed\n"));
printf(_(" --recovery-target-inclusive whether we stop just after the recovery target\n"));
printf(_(" --recovery-target-timeline recovering into a particular timeline\n"));
printf(_(" --hard-copy copying archivelog not symbolic link\n"));
printf(_("\nCatalog options:\n"));
printf(_(" -a, --show-all show deleted backup too\n"));
printf(_("\nDelete options:\n"));
printf(_(" -f, --force forcibly delete backup older than given DATE\n"));
}
/*
* Create range object from one or two arguments.
* All not-digit characters in the argument(s) are ignored.
* Both arg1 and arg2 must be valid pointer.
*/
static void
parse_range(pgBackupRange *range, const char *arg1, const char *arg2)
{
size_t len = strlen(arg1) + strlen(arg2) + 1;
char *tmp;
int num;
struct tm tm;
tmp = pgut_malloc(len);
tmp[0] = '\0';
if (arg1 != NULL)
remove_not_digit(tmp, len, arg1);
if (arg2 != NULL)
remove_not_digit(tmp + strlen(tmp), len - strlen(tmp), arg2);
memset(&tm, 0, sizeof(tm));
tm.tm_year = 0; /* tm_year is year - 1900 */
tm.tm_mon = 0; /* tm_mon is 0 - 11 */
tm.tm_mday = 1; /* tm_mday is 1 - 31 */
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
num = sscanf(tmp, "%04d %02d %02d %02d %02d %02d",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec);
if (num < 1)
{
if (strcmp(tmp,"") != 0)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("supplied id(%s) is invalid", tmp)));
else
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("arguments are invalid. near \"%s\"", arg1)));
}
free(tmp);
/* adjust year and month to convert to time_t */
tm.tm_year -= 1900;
if (num > 1)
tm.tm_mon -= 1;
tm.tm_isdst = -1;
if(!IsValidTime(tm))
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("supplied time(%s) is invalid", arg1)));
range->begin = mktime(&tm);
switch (num)
{
case 1:
tm.tm_year++;
break;
case 2:
tm.tm_mon++;
break;
case 3:
tm.tm_mday++;
break;
case 4:
tm.tm_hour++;
break;
case 5:
tm.tm_min++;
break;
case 6:
tm.tm_sec++;
break;
}
range->end = mktime(&tm);
range->end--;
}
static void
opt_backup_mode(pgut_option *opt, const char *arg)
{
current.backup_mode = parse_backup_mode(arg, ERROR);
}