Skip to content

Commit 75534a0

Browse files
committed
Code cleanup. Unify restore and validate. Remove unused functions.
1 parent 9d14e06 commit 75534a0

File tree

8 files changed

+263
-510
lines changed

8 files changed

+263
-510
lines changed

backup.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
208208
(uint32) (*lsn >> 32), (uint32) *lsn);
209209

210210
current.parent_backup = prev_backup->start_time;
211-
pgBackupWriteIni(&current);
211+
pgBackupWriteConf(&current);
212212
}
213213

214214
/* initialize backup list */
@@ -466,18 +466,18 @@ do_backup(bool smooth_checkpoint)
466466
current.checksum_version = get_data_checksum_version(true);
467467
current.stream = stream_wal;
468468

469-
/* Create backup directory and backup.ini */
469+
/* Create backup directory and BACKUP_CONF_FILE */
470470

471471
if (pgBackupCreateDir(&current))
472472
elog(ERROR, "cannot create backup directory");
473-
pgBackupWriteIni(&current);
473+
pgBackupWriteConf(&current);
474474

475475
elog(LOG, "backup destination is initialized");
476476

477477
/* get list of backups already taken */
478-
backup_list = catalog_get_backup_list(0);
479-
if (!backup_list)
480-
elog(ERROR, "cannot process any more");
478+
backup_list = catalog_get_backup_list(INVALID_BACKUP_ID);
479+
if (backup_list == NULL)
480+
elog(ERROR, "Failed to get backup list.");
481481

482482
/* set the error processing function for the backup process */
483483
pgut_atexit_push(backup_cleanup, NULL);
@@ -489,7 +489,7 @@ do_backup(bool smooth_checkpoint)
489489
/* update backup status to DONE */
490490
current.end_time = time(NULL);
491491
current.status = BACKUP_STATUS_DONE;
492-
pgBackupWriteIni(&current);
492+
pgBackupWriteConf(&current);
493493

494494
/* Calculate the total data read */
495495
if (verbose)
@@ -995,15 +995,15 @@ backup_cleanup(bool fatal, void *userdata)
995995
}
996996

997997
/*
998-
* Update status of backup.ini to ERROR.
998+
* Update status of backup in BACKUP_CONF_FILE to ERROR.
999999
* end_time != 0 means backup finished
10001000
*/
10011001
if (current.status == BACKUP_STATUS_RUNNING && current.end_time == 0)
10021002
{
1003-
elog(LOG, "backup is running, update its status to ERROR");
1003+
elog(LOG, "Backup is running, update its status to ERROR");
10041004
current.end_time = time(NULL);
10051005
current.status = BACKUP_STATUS_ERROR;
1006-
pgBackupWriteIni(&current);
1006+
pgBackupWriteConf(&current);
10071007
}
10081008
}
10091009

catalog.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,17 @@ IsDir(const char *dirpath, const char *entry)
248248
}
249249

250250
/*
251-
* Create list fo backups started between begin and end from backup catalog.
252-
* If range was NULL, all of backup are listed.
251+
* Create list of backups.
252+
* If 'requested_backup_id' is INVALID_BACKUP_ID, return list of all backups.
253253
* The list is sorted in order of descending start time.
254+
* If valid backup id is passed only matching backup will be added to the list.
254255
*/
255256
parray *
256-
catalog_get_backup_list(time_t backup_id)
257+
catalog_get_backup_list(time_t requested_backup_id)
257258
{
258259
DIR *date_dir = NULL;
259260
struct dirent *date_ent = NULL;
260-
DIR *time_dir = NULL;
261261
char backups_path[MAXPGPATH];
262-
char date_path[MAXPGPATH];
263262
parray *backups = NULL;
264263
pgBackup *backup = NULL;
265264

@@ -273,38 +272,42 @@ catalog_get_backup_list(time_t backup_id)
273272
goto err_proc;
274273
}
275274

276-
/* scan date/time directories and list backups in the range */
275+
/* scan the directory and list backups */
277276
backups = parray_new();
278277
for (; (date_ent = readdir(date_dir)) != NULL; errno = 0)
279278
{
280-
char ini_path[MAXPGPATH];
279+
char backup_conf_path[MAXPGPATH];
280+
char date_path[MAXPGPATH];
281281

282282
/* skip not-directory entries and hidden entries */
283-
if (!IsDir(backups_path, date_ent->d_name) || date_ent->d_name[0] == '.')
283+
if (!IsDir(backups_path, date_ent->d_name)
284+
|| date_ent->d_name[0] == '.')
284285
continue;
285286

286-
/* open subdirectory (date directory) and search time directory */
287+
/* open subdirectory of specific backup */
287288
join_path_components(date_path, backups_path, date_ent->d_name);
288289

289-
/* read backup information from backup.ini */
290-
snprintf(ini_path, MAXPGPATH, "%s/%s", date_path, BACKUP_CONF_FILE);
291-
backup = read_backup_from_file(ini_path);
290+
/* read backup information from BACKUP_CONF_FILE */
291+
snprintf(backup_conf_path, MAXPGPATH, "%s/%s", date_path, BACKUP_CONF_FILE);
292+
backup = read_backup_from_file(backup_conf_path);
292293

293-
/* ignore corrupted backup */
294+
/* ignore corrupted backups */
294295
if (backup)
295296
{
296-
if (backup_id != 0 && backup_id != backup->start_time)
297+
if (requested_backup_id != INVALID_BACKUP_ID
298+
&& requested_backup_id != backup->start_time)
297299
{
298300
pgBackupFree(backup);
299301
continue;
300302
}
301303
parray_append(backups, backup);
302304
backup = NULL;
303305
}
306+
304307
if (errno && errno != ENOENT)
305308
{
306309
elog(WARNING, "cannot read date directory \"%s\": %s",
307-
date_ent->d_name, strerror(errno));
310+
date_ent->d_name, strerror(errno));
308311
goto err_proc;
309312
}
310313
}
@@ -323,8 +326,6 @@ catalog_get_backup_list(time_t backup_id)
323326
return backups;
324327

325328
err_proc:
326-
if (time_dir)
327-
closedir(time_dir);
328329
if (date_dir)
329330
closedir(date_dir);
330331
if (backup)
@@ -449,9 +450,9 @@ pgBackupWriteResultSection(FILE *out, pgBackup *backup)
449450
}
450451
}
451452

452-
/* create backup.conf */
453+
/* create BACKUP_CONF_FILE */
453454
void
454-
pgBackupWriteIni(pgBackup *backup)
455+
pgBackupWriteConf(pgBackup *backup)
455456
{
456457
FILE *fp = NULL;
457458
char ini_path[MAXPGPATH];
@@ -472,7 +473,7 @@ pgBackupWriteIni(pgBackup *backup)
472473
}
473474

474475
/*
475-
* Read backup.ini and create pgBackup.
476+
* Read BACKUP_CONF_FILE and create pgBackup.
476477
* - Comment starts with ';'.
477478
* - Do not care section.
478479
*/
@@ -646,6 +647,7 @@ pgBackupGetPath(const pgBackup *backup, char *path, size_t len, const char *subd
646647
void
647648
init_backup(pgBackup *backup)
648649
{
650+
backup->backup_id = INVALID_BACKUP_ID;
649651
backup->backup_mode = BACKUP_MODE_INVALID;
650652
backup->status = BACKUP_STATUS_INVALID;
651653
backup->tli = 0;

delete.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ do_delete(time_t backup_id)
3535
catalog_lock(false);
3636

3737
/* Get complete list of backups */
38-
backup_list = catalog_get_backup_list(0);
39-
if (!backup_list)
40-
elog(ERROR, "no backup list found, can't process any more");
38+
backup_list = catalog_get_backup_list(INVALID_BACKUP_ID);
39+
if (backup_list == NULL)
40+
elog(ERROR, "Failed to get backup list.");
4141

4242
delete_list = parray_new();
4343

@@ -120,7 +120,7 @@ do_deletewal(time_t backup_id, bool strict, bool need_catalog_lock)
120120
catalog_lock(false);
121121

122122
/* Find oldest LSN, used by backups */
123-
backup_list = catalog_get_backup_list(0);
123+
backup_list = catalog_get_backup_list(INVALID_BACKUP_ID);
124124
for (i = 0; i < parray_num(backup_list); i++)
125125
{
126126
pgBackup *last_backup = (pgBackup *) parray_get(backup_list, i);
@@ -177,7 +177,7 @@ do_retention_purge(void)
177177
catalog_lock(false);
178178

179179
/* Get a complete list of backups. */
180-
backup_list = catalog_get_backup_list(0);
180+
backup_list = catalog_get_backup_list(INVALID_BACKUP_ID);
181181
if (parray_num(backup_list) == 0)
182182
{
183183
elog(INFO, "backup list is empty, purging won't be executed");
@@ -271,7 +271,7 @@ pgBackupDeleteFiles(pgBackup *backup)
271271
* the error occurs before deleting all backup files.
272272
*/
273273
backup->status = BACKUP_STATUS_DELETING;
274-
pgBackupWriteIni(backup);
274+
pgBackupWriteConf(backup);
275275

276276
/* list files to be deleted */
277277
files = parray_new();

pg_probackup.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ int
107107
main(int argc, char *argv[])
108108
{
109109
ProbackupSubcmd backup_subcmd;
110-
time_t backup_id = 0;
111110
int i;
112111

113112
/* initialize configuration */
@@ -170,8 +169,8 @@ main(int argc, char *argv[])
170169

171170
if (backup_id_string_param != NULL)
172171
{
173-
backup_id = base36dec(backup_id_string_param);
174-
if (backup_id == 0)
172+
current.backup_id = base36dec(backup_id_string_param);
173+
if (current.backup_id == 0)
175174
elog(ERROR, "Invalid backup-id");
176175
}
177176

@@ -213,21 +212,19 @@ main(int argc, char *argv[])
213212
case BACKUP:
214213
return do_backup(smooth_checkpoint);
215214
case RESTORE:
216-
return do_restore(backup_id,
217-
target_time,
218-
target_xid,
219-
target_inclusive,
220-
target_tli);
215+
return do_restore_or_validate(current.backup_id,
216+
target_time, target_xid,
217+
target_inclusive, target_tli,
218+
true);
221219
case VALIDATE:
222-
return do_validate(backup_id,
223-
target_time,
224-
target_xid,
225-
target_inclusive,
226-
target_tli);
220+
return do_restore_or_validate(current.backup_id,
221+
target_time, target_xid,
222+
target_inclusive, target_tli,
223+
false);
227224
case SHOW:
228-
return do_show(backup_id);
225+
return do_show(current.backup_id);
229226
case DELETE:
230-
return do_delete(backup_id);
227+
return do_delete(current.backup_id);
231228
case CONFIGURE:
232229
elog(ERROR, "not implemented yet");
233230
}

pg_probackup.h

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "storage/bufpage.h"
2626
#include "storage/block.h"
2727
#include "storage/checksum.h"
28+
#include "access/timeline.h"
2829

2930
#ifndef WIN32
3031
#include <sys/mman.h>
@@ -116,13 +117,16 @@ typedef enum ProbackupSubcmd
116117
CONFIGURE
117118
} ProbackupSubcmd;
118119

120+
#define INVALID_BACKUP_ID 0
121+
119122

120123
/* special values of pgBackup fields */
121124
#define KEEP_INFINITE (INT_MAX)
122125
#define BYTES_INVALID (-1)
123126

124127
typedef struct pgBackup
125128
{
129+
time_t backup_id;
126130
/* Mode - one of BACKUP_MODE_xxx above*/
127131
BackupMode backup_mode;
128132

@@ -164,13 +168,6 @@ typedef struct pgBackup
164168
time_t parent_backup;
165169
} pgBackup;
166170

167-
168-
typedef struct pgTimeLine
169-
{
170-
TimeLineID tli;
171-
XLogRecPtr end;
172-
} pgTimeLine;
173-
174171
typedef struct pgRecoveryTarget
175172
{
176173
bool time_specified;
@@ -252,18 +249,18 @@ extern void process_block_change(ForkNumber forknum, RelFileNode rnode,
252249
BlockNumber blkno);
253250

254251
/* in restore.c */
255-
extern int do_restore(time_t backup_id,
252+
extern int do_restore_or_validate(time_t target_backup_id,
256253
const char *target_time,
257254
const char *target_xid,
258255
const char *target_inclusive,
259-
TimeLineID target_tli);
256+
TimeLineID target_tli,
257+
bool is_restore);
260258
extern bool satisfy_timeline(const parray *timelines, const pgBackup *backup);
261259
extern bool satisfy_recovery_target(const pgBackup *backup,
262260
const pgRecoveryTarget *rt);
263-
extern TimeLineID get_fullbackup_timeline(parray *backups,
264-
const pgRecoveryTarget *rt);
265-
extern TimeLineID findNewestTimeLine(TimeLineID startTLI);
266-
extern parray * readTimeLineHistory(TimeLineID targetTLI);
261+
// extern TimeLineID get_fullbackup_timeline(parray *backups,
262+
// const pgRecoveryTarget *rt);
263+
extern parray * readTimeLineHistory_probackup(TimeLineID targetTLI);
267264
extern pgRecoveryTarget *checkIfCreateRecoveryConf(
268265
const char *target_time,
269266
const char *target_xid,
@@ -275,8 +272,7 @@ extern void opt_tablespace_map(pgut_option *opt, const char *arg);
275272
extern int do_init(void);
276273

277274
/* in show.c */
278-
extern int do_show(time_t backup_id);
279-
extern int do_retention_show(void);
275+
extern int do_show(time_t requested_backup_id);
280276

281277
/* in delete.c */
282278
extern int do_delete(time_t backup_id);
@@ -290,28 +286,22 @@ extern char *slurpFile(const char *datadir,
290286
bool safe);
291287

292288
/* in validate.c */
293-
extern int do_validate(time_t backup_id,
294-
const char *target_time,
295-
const char *target_xid,
296-
const char *target_inclusive,
297-
TimeLineID target_tli);
298-
extern void do_validate_last(void);
299289
extern bool pgBackupValidate(pgBackup *backup,
300290
bool size_only,
301291
bool for_get_timeline);
302292

303293
extern pgBackup *read_backup(time_t timestamp);
304294
extern void init_backup(pgBackup *backup);
305295

306-
extern parray *catalog_get_backup_list(time_t backup_id);
296+
extern parray *catalog_get_backup_list(time_t requested_backup_id);
307297
extern pgBackup *catalog_get_last_data_backup(parray *backup_list,
308298
TimeLineID tli);
309299

310300
extern void catalog_lock(bool check_catalog);
311301

312302
extern void pgBackupWriteConfigSection(FILE *out, pgBackup *backup);
313303
extern void pgBackupWriteResultSection(FILE *out, pgBackup *backup);
314-
extern void pgBackupWriteIni(pgBackup *backup);
304+
extern void pgBackupWriteConf(pgBackup *backup);
315305
extern void pgBackupGetPath(const pgBackup *backup, char *path, size_t len, const char *subdir);
316306
extern int pgBackupCreateDir(pgBackup *backup);
317307
extern void pgBackupFree(void *backup);

0 commit comments

Comments
 (0)