Skip to content

Commit ceaa2ce

Browse files
committed
PGPRO-533: Allow to show backup list in json format
1 parent 37ced69 commit ceaa2ce

File tree

8 files changed

+527
-145
lines changed

8 files changed

+527
-145
lines changed

src/backup.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ do_backup_instance(void)
633633
* For backup from master wait for previous segment.
634634
* For backup from replica wait for current segment.
635635
*/
636-
!from_replica, backup_files_list);
636+
!current.from_replica, backup_files_list);
637637
}
638638

639639
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
@@ -815,11 +815,15 @@ do_backup(time_t start_time)
815815
pgut_atexit_push(backup_disconnect, NULL);
816816

817817
current.primary_conninfo = pgut_get_conninfo_string(backup_conn);
818+
819+
current.compress_alg = compress_alg;
820+
current.compress_level = compress_level;
821+
818822
/* Confirm data block size and xlog block size are compatible */
819823
confirm_block_size("block_size", BLCKSZ);
820824
confirm_block_size("wal_block_size", XLOG_BLCKSZ);
821825

822-
from_replica = pg_is_in_recovery();
826+
current.from_replica = pg_is_in_recovery();
823827

824828
/* Confirm that this server version is supported */
825829
check_server_version();
@@ -859,7 +863,7 @@ do_backup(time_t start_time)
859863
}
860864
}
861865

862-
if (from_replica)
866+
if (current.from_replica)
863867
{
864868
/* Check master connection options */
865869
if (master_host == NULL)
@@ -956,7 +960,7 @@ check_server_version(void)
956960
"server version is %s, must be %s or higher",
957961
server_version_str, "9.5");
958962

959-
if (from_replica && server_version < 90600)
963+
if (current.from_replica && server_version < 90600)
960964
elog(ERROR,
961965
"server version is %s, must be %s or higher for backup from replica",
962966
server_version_str, "9.6");
@@ -1061,7 +1065,7 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
10611065
params[0] = label;
10621066

10631067
/* For replica we call pg_start_backup() on master */
1064-
conn = (from_replica) ? master_conn : backup_conn;
1068+
conn = (backup->from_replica) ? master_conn : backup_conn;
10651069

10661070
/* 2nd argument is 'fast'*/
10671071
params[1] = smooth ? "false" : "true";
@@ -1106,7 +1110,7 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
11061110
}
11071111

11081112
/* Wait for start_lsn to be replayed by replica */
1109-
if (from_replica)
1113+
if (backup->from_replica)
11101114
wait_replica_wal_lsn(backup->start_lsn, true);
11111115

11121116
/*
@@ -1554,8 +1558,6 @@ wait_replica_wal_lsn(XLogRecPtr lsn, bool is_start_backup)
15541558
{
15551559
uint32 try_count = 0;
15561560

1557-
Assert(from_replica);
1558-
15591561
while (true)
15601562
{
15611563
PGresult *res;
@@ -1650,7 +1652,7 @@ pg_stop_backup(pgBackup *backup)
16501652
elog(FATAL, "backup is not in progress");
16511653

16521654
/* For replica we call pg_stop_backup() on master */
1653-
conn = (from_replica) ? master_conn : backup_conn;
1655+
conn = (current.from_replica) ? master_conn : backup_conn;
16541656

16551657
/* Remove annoying NOTICE messages generated by backend */
16561658
res = pgut_execute(conn, "SET client_min_messages = warning;",
@@ -1663,7 +1665,7 @@ pg_stop_backup(pgBackup *backup)
16631665
const char *params[1];
16641666
char name[1024];
16651667

1666-
if (!from_replica)
1668+
if (!current.from_replica)
16671669
snprintf(name, lengthof(name), "pg_probackup, backup_id %s",
16681670
base36enc(backup->start_time));
16691671
else
@@ -1891,7 +1893,7 @@ pg_stop_backup(pgBackup *backup)
18911893
stream_xlog_path[MAXPGPATH];
18921894

18931895
/* Wait for stop_lsn to be received by replica */
1894-
if (from_replica)
1896+
if (backup->from_replica)
18951897
wait_replica_wal_lsn(stop_backup_lsn, false);
18961898
/*
18971899
* Wait for stop_lsn to be archived or streamed.

src/catalog.c

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,11 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
385385

386386
fprintf(out, "#Configuration\n");
387387
fprintf(out, "backup-mode = %s\n", pgBackupGetBackupMode(backup));
388-
fprintf(out, "stream = %s\n", backup->stream?"true":"false");
389-
fprintf(out, "compress-alg = %s\n", deparse_compress_alg(compress_alg));
390-
fprintf(out, "compress-level = %d\n", compress_level);
391-
fprintf(out, "from-replica = %s\n", from_replica?"true":"false");
388+
fprintf(out, "stream = %s\n", backup->stream ? "true" : "false");
389+
fprintf(out, "compress-alg = %s\n",
390+
deparse_compress_alg(backup->compress_alg));
391+
fprintf(out, "compress-level = %d\n", backup->compress_level);
392+
fprintf(out, "from-replica = %s\n", backup->from_replica ? "true" : "false");
392393

393394
fprintf(out, "\n#Compatibility\n");
394395
fprintf(out, "block-size = %u\n", backup->block_size);
@@ -429,7 +430,7 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
429430
if (backup->data_bytes != BYTES_INVALID)
430431
fprintf(out, "data-bytes = " INT64_FORMAT "\n", backup->data_bytes);
431432

432-
if (backup->data_bytes != BYTES_INVALID)
433+
if (backup->wal_bytes != BYTES_INVALID)
433434
fprintf(out, "wal-bytes = " INT64_FORMAT "\n", backup->wal_bytes);
434435

435436
fprintf(out, "status = %s\n", status2str(backup->status));
@@ -475,10 +476,8 @@ readBackupControlFile(const char *path)
475476
char *stop_lsn = NULL;
476477
char *status = NULL;
477478
char *parent_backup = NULL;
478-
char *compress_alg = NULL;
479479
char *server_version = NULL;
480-
int *compress_level;
481-
bool *from_replica;
480+
char *compress_alg = NULL;
482481

483482
pgut_option options[] =
484483
{
@@ -500,8 +499,8 @@ readBackupControlFile(const char *path)
500499
{'s', 0, "status", &status, SOURCE_FILE_STRICT},
501500
{'s', 0, "parent-backup-id", &parent_backup, SOURCE_FILE_STRICT},
502501
{'s', 0, "compress-alg", &compress_alg, SOURCE_FILE_STRICT},
503-
{'u', 0, "compress-level", &compress_level, SOURCE_FILE_STRICT},
504-
{'b', 0, "from-replica", &from_replica, SOURCE_FILE_STRICT},
502+
{'u', 0, "compress-level", &backup->compress_level, SOURCE_FILE_STRICT},
503+
{'b', 0, "from-replica", &backup->from_replica, SOURCE_FILE_STRICT},
505504
{'s', 0, "primary-conninfo", &backup->primary_conninfo, SOURCE_FILE_STRICT},
506505
{0}
507506
};
@@ -578,6 +577,9 @@ readBackupControlFile(const char *path)
578577
pfree(server_version);
579578
}
580579

580+
if (compress_alg)
581+
backup->compress_alg = parse_compress_alg(compress_alg);
582+
581583
return backup;
582584
}
583585

@@ -626,6 +628,48 @@ deparse_backup_mode(BackupMode mode)
626628
return NULL;
627629
}
628630

631+
CompressAlg
632+
parse_compress_alg(const char *arg)
633+
{
634+
size_t len;
635+
636+
/* Skip all spaces detected */
637+
while (isspace((unsigned char)*arg))
638+
arg++;
639+
len = strlen(arg);
640+
641+
if (len == 0)
642+
elog(ERROR, "compress algrorithm is empty");
643+
644+
if (pg_strncasecmp("zlib", arg, len) == 0)
645+
return ZLIB_COMPRESS;
646+
else if (pg_strncasecmp("pglz", arg, len) == 0)
647+
return PGLZ_COMPRESS;
648+
else if (pg_strncasecmp("none", arg, len) == 0)
649+
return NONE_COMPRESS;
650+
else
651+
elog(ERROR, "invalid compress algorithm value \"%s\"", arg);
652+
653+
return NOT_DEFINED_COMPRESS;
654+
}
655+
656+
const char*
657+
deparse_compress_alg(int alg)
658+
{
659+
switch (alg)
660+
{
661+
case NONE_COMPRESS:
662+
case NOT_DEFINED_COMPRESS:
663+
return "none";
664+
case ZLIB_COMPRESS:
665+
return "zlib";
666+
case PGLZ_COMPRESS:
667+
return "pglz";
668+
}
669+
670+
return NULL;
671+
}
672+
629673
/* free pgBackup object */
630674
void
631675
pgBackupFree(void *backup)

src/help.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ help_pg_probackup(void)
128128

129129
printf(_("\n %s show -B backup-dir\n"), PROGRAM_NAME);
130130
printf(_(" [--instance=instance_name [-i backup-id]]\n"));
131+
printf(_(" [--format=format]\n"));
131132

132133
printf(_("\n %s delete -B backup-dir --instance=instance_name\n"), PROGRAM_NAME);
133134
printf(_(" [--wal] [-i backup-id | --expired]\n"));
@@ -362,6 +363,7 @@ help_show(void)
362363
printf(_(" -B, --backup-path=backup-path location of the backup storage area\n"));
363364
printf(_(" --instance=instance_name show info about specific intstance\n"));
364365
printf(_(" -i, --backup-id=backup-id show info about specific backups\n"));
366+
printf(_(" --format=format show format=PLAIN|JSON\n"));
365367
}
366368

367369
static void

src/pg_probackup.c

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ char *replication_slot = NULL;
4747
/* backup options */
4848
bool backup_logs = false;
4949
bool smooth_checkpoint;
50-
bool from_replica = false;
5150
bool is_remote_backup = false;
5251
/* Wait timeout for WAL segment archiving */
5352
uint32 archive_timeout = 300; /* default is 300 seconds */
@@ -83,7 +82,7 @@ uint32 retention_window = 0;
8382
/* compression options */
8483
CompressAlg compress_alg = NOT_DEFINED_COMPRESS;
8584
int compress_level = DEFAULT_COMPRESS_LEVEL;
86-
bool compress_shortcut = false;
85+
bool compress_shortcut = false;
8786

8887
/* other options */
8988
char *instance_name;
@@ -94,6 +93,9 @@ static char *wal_file_path;
9493
static char *wal_file_name;
9594
static bool file_overwrite = false;
9695

96+
/* show options */
97+
ShowFormat show_format = SHOW_PLAIN;
98+
9799
/* current settings */
98100
pgBackup current;
99101
ProbackupSubcmd backup_subcmd;
@@ -104,6 +106,7 @@ static void opt_backup_mode(pgut_option *opt, const char *arg);
104106
static void opt_log_level_console(pgut_option *opt, const char *arg);
105107
static void opt_log_level_file(pgut_option *opt, const char *arg);
106108
static void opt_compress_alg(pgut_option *opt, const char *arg);
109+
static void opt_show_format(pgut_option *opt, const char *arg);
107110

108111
static void compress_init(void);
109112

@@ -178,6 +181,8 @@ static pgut_option options[] =
178181
{ 's', 160, "wal-file-path", &wal_file_path, SOURCE_CMDLINE },
179182
{ 's', 161, "wal-file-name", &wal_file_name, SOURCE_CMDLINE },
180183
{ 'b', 162, "overwrite", &file_overwrite, SOURCE_CMDLINE },
184+
/* show options */
185+
{ 'f', 170, "format", opt_show_format, SOURCE_CMDLINE },
181186
{ 0 }
182187
};
183188

@@ -517,49 +522,31 @@ opt_log_level_file(pgut_option *opt, const char *arg)
517522
log_level_file = parse_log_level(arg);
518523
}
519524

520-
CompressAlg
521-
parse_compress_alg(const char *arg)
525+
static void
526+
opt_show_format(pgut_option *opt, const char *arg)
522527
{
528+
const char *v = arg;
523529
size_t len;
524530

525531
/* Skip all spaces detected */
526-
while (isspace((unsigned char)*arg))
527-
arg++;
528-
len = strlen(arg);
529-
530-
if (len == 0)
531-
elog(ERROR, "compress algrorithm is empty");
532-
533-
if (pg_strncasecmp("zlib", arg, len) == 0)
534-
return ZLIB_COMPRESS;
535-
else if (pg_strncasecmp("pglz", arg, len) == 0)
536-
return PGLZ_COMPRESS;
537-
else if (pg_strncasecmp("none", arg, len) == 0)
538-
return NONE_COMPRESS;
539-
else
540-
elog(ERROR, "invalid compress algorithm value \"%s\"", arg);
532+
while (IsSpace(*v))
533+
v++;
534+
len = strlen(v);
541535

542-
return NOT_DEFINED_COMPRESS;
543-
}
544-
545-
const char*
546-
deparse_compress_alg(int alg)
547-
{
548-
switch (alg)
536+
if (len > 0)
549537
{
550-
case NONE_COMPRESS:
551-
case NOT_DEFINED_COMPRESS:
552-
return "none";
553-
case ZLIB_COMPRESS:
554-
return "zlib";
555-
case PGLZ_COMPRESS:
556-
return "pglz";
538+
if (pg_strncasecmp("plain", v, len) == 0)
539+
show_format = SHOW_PLAIN;
540+
else if (pg_strncasecmp("json", v, len) == 0)
541+
show_format = SHOW_JSON;
542+
else
543+
elog(ERROR, "Invalid show format \"%s\"", arg);
557544
}
558-
559-
return NULL;
545+
else
546+
elog(ERROR, "Invalid show format \"%s\"", arg);
560547
}
561548

562-
void
549+
static void
563550
opt_compress_alg(pgut_option *opt, const char *arg)
564551
{
565552
compress_alg = parse_compress_alg(arg);
@@ -568,8 +555,8 @@ opt_compress_alg(pgut_option *opt, const char *arg)
568555
/*
569556
* Initialize compress and sanity checks for compress.
570557
*/
571-
static
572-
void compress_init(void)
558+
static void
559+
compress_init(void)
573560
{
574561
/* Default algorithm is zlib */
575562
if (compress_shortcut)

0 commit comments

Comments
 (0)