2013-01-24 09:35:48 +03:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2017-04-13 18:37:29 +02:00
|
|
|
* show.c: show backup information.
|
2013-01-24 09:35:48 +03:00
|
|
|
*
|
2017-03-01 15:50:07 +02:00
|
|
|
* Portions Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
|
|
|
|
* Portions Copyright (c) 2015-2017, Postgres Professional
|
2013-01-24 09:35:48 +03:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-11-16 19:34:21 +02:00
|
|
|
#include "pg_probackup.h"
|
2017-03-22 10:13:06 +02:00
|
|
|
#include <time.h>
|
2017-05-29 17:52:43 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-11-16 20:32:26 +02:00
|
|
|
static void show_backup_list(FILE *out, parray *backup_list);
|
2013-01-24 09:35:48 +03:00
|
|
|
static void show_backup_detail(FILE *out, pgBackup *backup);
|
2017-05-29 17:52:43 +02:00
|
|
|
static int do_show_instance(time_t requested_backup_id);
|
|
|
|
|
|
|
|
int
|
|
|
|
do_show(time_t requested_backup_id)
|
|
|
|
{
|
|
|
|
|
2017-05-31 14:59:09 +02:00
|
|
|
if (instance_name == NULL
|
|
|
|
&& requested_backup_id != INVALID_BACKUP_ID)
|
|
|
|
elog(ERROR, "You must specify --instance to use --backup_id option");
|
|
|
|
|
2017-05-29 17:52:43 +02:00
|
|
|
if (instance_name == NULL)
|
|
|
|
{
|
|
|
|
/* Show list of instances */
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dent;
|
|
|
|
|
|
|
|
/* open directory and list contents */
|
|
|
|
join_path_components(path, backup_path, BACKUPS_DIR);
|
|
|
|
dir = opendir(path);
|
|
|
|
if (dir == NULL)
|
|
|
|
elog(ERROR, "cannot open directory \"%s\": %s", path, strerror(errno));
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
while ((dent = readdir(dir)))
|
|
|
|
{
|
|
|
|
char child[MAXPGPATH];
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/* skip entries point current dir or parent dir */
|
|
|
|
if (strcmp(dent->d_name, ".") == 0 ||
|
|
|
|
strcmp(dent->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
join_path_components(child, path, dent->d_name);
|
|
|
|
|
|
|
|
if (lstat(child, &st) == -1)
|
|
|
|
elog(ERROR, "cannot stat file \"%s\": %s", child, strerror(errno));
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
instance_name = dent->d_name;
|
|
|
|
sprintf(backup_instance_path, "%s/%s/%s", backup_path, BACKUPS_DIR, instance_name);
|
|
|
|
fprintf(stdout, "\nBACKUP INSTANCE '%s'\n", instance_name);
|
|
|
|
do_show_instance(0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return do_show_instance(requested_backup_id);
|
|
|
|
}
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/*
|
2017-04-13 18:37:29 +02:00
|
|
|
* If 'requested_backup_id' is INVALID_BACKUP_ID, show brief meta information
|
2017-05-29 17:52:43 +02:00
|
|
|
* about all backups in the backup instance.
|
2017-04-13 18:37:29 +02:00
|
|
|
* If valid backup id is passed, show detailed meta information
|
|
|
|
* about specified backup.
|
2013-01-24 09:35:48 +03:00
|
|
|
*/
|
2017-05-29 17:52:43 +02:00
|
|
|
static int
|
|
|
|
do_show_instance(time_t requested_backup_id)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2017-04-13 18:37:29 +02:00
|
|
|
if (requested_backup_id != INVALID_BACKUP_ID)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2017-03-06 10:55:12 +02:00
|
|
|
pgBackup *backup;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2017-04-13 18:37:29 +02:00
|
|
|
backup = read_backup(requested_backup_id);
|
2013-01-24 09:35:48 +03:00
|
|
|
if (backup == NULL)
|
|
|
|
{
|
2017-04-13 18:37:29 +02:00
|
|
|
elog(INFO, "Requested backup \"%s\" is not found.",
|
2017-04-21 11:06:07 +02:00
|
|
|
/* We do not need free base36enc's result, we exit anyway */
|
2017-04-13 18:37:29 +02:00
|
|
|
base36enc(requested_backup_id));
|
|
|
|
/* This is not error */
|
2013-01-24 09:35:48 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-06 10:55:12 +02:00
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
show_backup_detail(stdout, backup);
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
pgBackupFree(backup);
|
2017-04-13 18:37:29 +02:00
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parray *backup_list;
|
|
|
|
|
2017-04-13 18:37:29 +02:00
|
|
|
backup_list = catalog_get_backup_list(INVALID_BACKUP_ID);
|
2016-01-14 09:36:39 +02:00
|
|
|
if (backup_list == NULL)
|
2017-04-13 18:37:29 +02:00
|
|
|
elog(ERROR, "Failed to get backup list.");
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-11-16 20:32:26 +02:00
|
|
|
show_backup_list(stdout, backup_list);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2017-04-27 16:10:26 +02:00
|
|
|
/* Timeline 1 does not have a history file and parent timeline */
|
|
|
|
if (child_tli == 1)
|
|
|
|
return 0;
|
|
|
|
|
2014-01-24 16:36:31 +03:00
|
|
|
/* Search history file in archives */
|
|
|
|
snprintf(path, lengthof(path), "%s/%08X.history", arclog_path,
|
|
|
|
child_tli);
|
2013-01-24 09:35:48 +03:00
|
|
|
fd = fopen(path, "rt");
|
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR, "could not open file \"%s\": %s", path,
|
2013-01-24 09:35:48 +03:00
|
|
|
strerror(errno));
|
|
|
|
|
2017-04-27 16:10:26 +02:00
|
|
|
/* Did not find history file, do not raise the error */
|
2013-01-24 09:35:48 +03:00
|
|
|
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)
|
2016-01-19 05:41:30 +02:00
|
|
|
elog(ERROR,
|
2016-01-14 09:36:39 +02:00
|
|
|
"syntax error(timeline ID) in history file: %s",
|
2013-01-24 09:35:48 +03:00
|
|
|
fline);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
/* TLI of the last line is parent TLI */
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-16 20:32:26 +02:00
|
|
|
show_backup_list(FILE *out, parray *backup_list)
|
2013-01-24 09:35:48 +03:00
|
|
|
{
|
2017-03-06 10:55:12 +02:00
|
|
|
int i;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2017-06-07 15:59:22 +02:00
|
|
|
/* if you add new fields here, fix the header */
|
2013-01-24 09:35:48 +03:00
|
|
|
/* show header */
|
2017-11-09 16:55:13 +02:00
|
|
|
fputs("============================================================================================================================================\n", out);
|
|
|
|
fputs(" Instance Version ID Recovery time Mode WAL Current/Parent TLI Time Data Start LSN Stop LSN Status \n", out);
|
|
|
|
fputs("============================================================================================================================================\n", out);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
for (i = 0; i < parray_num(backup_list); i++)
|
|
|
|
{
|
2017-03-06 10:55:12 +02:00
|
|
|
pgBackup *backup = parray_get(backup_list, i);
|
|
|
|
TimeLineID parent_tli;
|
2017-11-09 16:55:13 +02:00
|
|
|
FILE *fp;
|
2017-04-21 11:06:07 +02:00
|
|
|
char *backup_id;
|
2017-11-09 16:55:13 +02:00
|
|
|
char pg_version_path[MAXPGPATH];
|
|
|
|
char pg_version[100] = "---";
|
2017-10-06 10:42:08 +02:00
|
|
|
char timestamp[100] = "----";
|
2017-03-06 10:55:12 +02:00
|
|
|
char duration[20] = "----";
|
|
|
|
char data_bytes_str[10] = "----";
|
|
|
|
|
2017-02-14 16:45:18 +02:00
|
|
|
if (backup->recovery_time != (time_t) 0)
|
|
|
|
time2iso(timestamp, lengthof(timestamp), backup->recovery_time);
|
2013-01-24 09:35:48 +03:00
|
|
|
if (backup->end_time != (time_t) 0)
|
2017-03-22 10:44:18 +02:00
|
|
|
snprintf(duration, lengthof(duration), "%.*lfs", 0,
|
2017-03-22 10:13:06 +02:00
|
|
|
difftime(backup->end_time, backup->start_time));
|
2014-01-09 22:11:27 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate Data field, in the case of full backup this shows the
|
2014-01-30 09:58:55 +03:00
|
|
|
* total amount of data. For an differential backup, this size is only
|
|
|
|
* the difference of data accumulated.
|
2014-01-09 22:11:27 +03:00
|
|
|
*/
|
|
|
|
pretty_size(backup->data_bytes, data_bytes_str,
|
|
|
|
lengthof(data_bytes_str));
|
|
|
|
|
2013-12-12 18:53:27 +03:00
|
|
|
/* Get parent timeline before printing */
|
2013-01-24 09:35:48 +03:00
|
|
|
parent_tli = get_parent_tli(backup->tli);
|
2017-04-21 11:06:07 +02:00
|
|
|
backup_id = base36enc(backup->start_time);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2017-11-09 16:55:13 +02:00
|
|
|
/* Read PG_VERSION file to show backup version */
|
|
|
|
pgBackupGetPath2(backup, pg_version_path, lengthof(pg_version_path),
|
|
|
|
DATABASE_DIR, "PG_VERSION");
|
|
|
|
fp = pgut_fopen(pg_version_path, "rt", true);
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
if (fgets(pg_version, lengthof(pg_version), fp))
|
|
|
|
{
|
|
|
|
int len = strlen(pg_version);
|
|
|
|
|
|
|
|
/* Remove trailing new line */
|
|
|
|
if (len > 0 && pg_version[len - 1] == '\n')
|
|
|
|
pg_version[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(out, " %-11s %-8s %-6s %-19s %-6s %-7s %3d / %-3d %5s %6s %2X/%-8X %2X/%-8X %-8s\n",
|
|
|
|
instance_name,
|
|
|
|
pg_version,
|
|
|
|
backup_id,
|
2014-01-09 22:11:27 +03:00
|
|
|
timestamp,
|
2017-04-21 13:54:33 +02:00
|
|
|
pgBackupGetBackupMode(backup),
|
2017-04-24 16:56:05 +02:00
|
|
|
backup->stream ? "STREAM": "ARCHIVE",
|
2014-01-09 22:11:27 +03:00
|
|
|
backup->tli,
|
|
|
|
parent_tli,
|
|
|
|
duration,
|
|
|
|
data_bytes_str,
|
2017-03-20 18:08:18 +02:00
|
|
|
(uint32) (backup->start_lsn >> 32),
|
|
|
|
(uint32) backup->start_lsn,
|
|
|
|
(uint32) (backup->stop_lsn >> 32),
|
|
|
|
(uint32) backup->stop_lsn,
|
2014-01-09 22:11:27 +03:00
|
|
|
status2str(backup->status));
|
2017-04-21 11:06:07 +02:00
|
|
|
|
|
|
|
free(backup_id);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_backup_detail(FILE *out, pgBackup *backup)
|
|
|
|
{
|
2017-04-19 11:01:10 +02:00
|
|
|
pgBackupWriteControl(out, backup);
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|