forked from ossc-db/pg_rman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.c
278 lines (234 loc) · 6.91 KB
/
show.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
/*-------------------------------------------------------------------------
*
* show.c: show backup catalog.
*
* Copyright (c) 2009-2021, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
*-------------------------------------------------------------------------
*/
#include "pg_rman.h"
static void show_backup_list(FILE *out, parray *backup_list, bool show_all);
static void show_detail_backup_list(FILE *out, parray *backup_list, bool show_all);
static void show_backup_detail(FILE *out, pgBackup *backup);
/*
* Show backup catalog information.
* If range is { 0, 0 }, show list of all backup, otherwise show detail of the
* backup indicated by id.
*/
int
do_show(pgBackupRange *range, bool show_detail, bool show_all)
{
if (pgBackupRangeIsSingle(range))
{
pgBackup *backup;
backup = catalog_get_backup(range->begin);
if (backup == NULL)
{
char timestamp[100];
time2iso(timestamp, lengthof(timestamp), range->begin);
elog(NOTICE, _("backup taken at \"%s\" does not exist"),
timestamp);
/* This is not error case */
return 0;
}
show_backup_detail(stdout, backup);
/* cleanup */
pgBackupFree(backup);
}
else
{
parray *backup_list;
backup_list = catalog_get_backup_list(range);
if (backup_list == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not get list of backup already taken")));
if (!show_detail)
show_backup_list(stdout, backup_list, show_all);
else
show_detail_backup_list(stdout, backup_list, show_all);
/* cleanup */
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
}
return 0;
}
static void
pretty_size(int64 size, char *buf, size_t len)
{
int exp = 0;
/* minus means the size is invalid */
if (size < 0)
{
strncpy(buf, "----", len);
return;
}
/* determine postfix */
while (size > 9999)
{
++exp;
size /= 1000;
}
switch (exp)
{
case 0:
snprintf(buf, len, INT64_FORMAT "B", size);
break;
case 1:
snprintf(buf, len, INT64_FORMAT "kB", size);
break;
case 2:
snprintf(buf, len, INT64_FORMAT "MB", size);
break;
case 3:
snprintf(buf, len, INT64_FORMAT "GB", size);
break;
case 4:
snprintf(buf, len, INT64_FORMAT "TB", size);
break;
case 5:
snprintf(buf, len, INT64_FORMAT "PB", size);
break;
default:
strncpy(buf, "***", len);
break;
}
}
static TimeLineID
get_parent_tli(TimeLineID child_tli)
{
TimeLineID result = 0;
char path[MAXPGPATH];
char fline[MAXPGPATH];
FILE *fd;
/* search from timeline history dir */
snprintf(path, lengthof(path), "%s/%s/%08X.history", backup_path,
TIMELINE_HISTORY_DIR, child_tli);
fd = fopen(path, "rt");
if (fd == NULL)
{
if (errno != ENOENT)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not open file \"%s\": %s", path,
strerror(errno))));
return 0;
}
/*
* Parse the file...
*/
while (fgets(fline, sizeof(fline), fd) != NULL)
{
/* skip leading whitespace and check for # comment */
char *ptr;
char *endptr;
for (ptr = fline; *ptr; ptr++)
{
if (!IsSpace(*ptr))
break;
}
if (*ptr == '\0' || *ptr == '#')
continue;
/* expect a numeric timeline ID as first field of line */
result = (TimeLineID) strtoul(ptr, &endptr, 0);
if (endptr == ptr)
ereport(ERROR,
(errcode(ERROR_CORRUPTED),
errmsg("syntax error(timeline ID) in history file: %s", fline)));
}
fclose(fd);
/* TLI of the last line is parent TLI */
return result;
}
static void
show_backup_list(FILE *out, parray *backup_list, bool show_all)
{
int i;
/* show header */
fputs("=====================================================================\n", out);
fputs(" StartTime EndTime Mode Size TLI Status \n", out);
fputs("=====================================================================\n", out);
for (i = 0; i < parray_num(backup_list); i++)
{
pgBackup *backup;
char start_time[20];
char end_time[20];
char write_bytes_str[10];
static const char *modes[] = { "", "ARCH", "INCR", "FULL"};
backup = parray_get(backup_list, i);
/*
* Skip deleted backup, unless --show-all has been specified.
*
* Just ignore invalid backups.
*/
if ((backup->status == BACKUP_STATUS_DELETED && !show_all) ||
backup->status == BACKUP_STATUS_INVALID)
continue;
time2iso(start_time, lengthof(start_time), backup->start_time);
time2iso(end_time, lengthof(end_time), backup->end_time);
pretty_size(backup->write_bytes, write_bytes_str,
lengthof(write_bytes_str));
fprintf(out, "%-19s %-19s %-4s %6s %5d %s\n",
start_time, end_time, modes[backup->backup_mode],
write_bytes_str, backup->tli, status2str(backup->status));
}
}
static void
show_detail_backup_list(FILE *out, parray *backup_list, bool show_all)
{
int i;
/* show header */
fputs("======================================================================================================================\n", out);
fputs(" StartTime EndTime Mode Data ArcLog SrvLog Total Compressed CurTLI ParentTLI Status \n", out);
fputs("======================================================================================================================\n", out);
for (i = 0; i < parray_num(backup_list); i++)
{
static const char *modes[] = { "", "ARCH", "INCR", "FULL"};
pgBackup *backup;
char start_time[20];
char end_time[20];
char read_data_bytes_str[10] = "----";
char read_arclog_bytes_str[10] = "----";
char read_srvlog_bytes_str[10] = "----";
char write_bytes_str[10];
char *is_compressed;
TimeLineID parent_tli;
backup = parray_get(backup_list, i);
/* skip deleted backup and serverlog backup */
if ((backup->status == BACKUP_STATUS_DELETED || !HAVE_ARCLOG(backup)) &&
!show_all)
continue;
time2iso(start_time, lengthof(start_time), backup->start_time);
time2iso(end_time, lengthof(end_time), backup->end_time);
pretty_size(backup->read_data_bytes, read_data_bytes_str,
lengthof(read_data_bytes_str));
if (HAVE_ARCLOG(backup))
pretty_size(backup->read_arclog_bytes, read_arclog_bytes_str,
lengthof(read_arclog_bytes_str));
if (backup->with_serverlog)
pretty_size(backup->read_srvlog_bytes, read_srvlog_bytes_str,
lengthof(read_srvlog_bytes_str));
pretty_size(backup->write_bytes, write_bytes_str,
lengthof(write_bytes_str));
if (backup->compress_data)
is_compressed = "true";
else
is_compressed = "false";
parent_tli = get_parent_tli(backup->tli);
fprintf(out, "%-19s %-19s %-4s %6s %6s %6s %6s %5s %6d %9d %s\n",
start_time, end_time, modes[backup->backup_mode],
read_data_bytes_str,
read_arclog_bytes_str,
read_srvlog_bytes_str,
write_bytes_str,
is_compressed,
backup->tli, parent_tli,
status2str(backup->status));
}
}
static void
show_backup_detail(FILE *out, pgBackup *backup)
{
pgBackupWriteConfigSection(out, backup);
pgBackupWriteResultSection(out, backup);
}