1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2025-01-09 14:45:47 +02:00

Refactor delete command and add delwal command.

This commit is contained in:
stalkerg 2016-11-16 17:38:11 +03:00
parent ff61fcc341
commit 7ba03021c5
4 changed files with 48 additions and 22 deletions

View File

@ -13,6 +13,7 @@
#include <unistd.h>
static int pgBackupDeleteFiles(pgBackup *backup);
int do_deletewal(time_t backup_id, bool strict);
int
do_delete(time_t backup_id)
@ -21,8 +22,6 @@ do_delete(time_t backup_id)
int b_index;
int ret;
parray *backup_list;
XLogRecPtr oldest_lsn = InvalidXLogRecPtr;
TimeLineID oldest_tli;
pgBackup *last_backup;
/* DATE are always required */
@ -82,37 +81,58 @@ found_backup:
/* cleanup */
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
if (delete_wal)
do_deletewal(backup_id, false);
return 0;
}
int do_deletewal(time_t backup_id, bool strict)
{
int i;
int ret;
parray *backup_list;
XLogRecPtr oldest_lsn = InvalidXLogRecPtr;
TimeLineID oldest_tli;
pgBackup *last_backup;
bool backup_found = false;
/*
* Delete in archive WAL segments that are not needed anymore. The oldest
* segment to be kept is the first segment that the oldest full backup
* found around needs to keep.
*/
if (delete_wal)
{
/* Lock backup catalog */
ret = catalog_lock();
if (ret == -1)
elog(ERROR, "can't lock backup catalog.");
else if (ret == 1)
elog(ERROR,
"another pg_arman is running, stop delete.");
/* Lock backup catalog */
ret = catalog_lock();
if (ret == -1)
elog(ERROR, "can't lock backup catalog.");
else if (ret == 1)
elog(ERROR,
"another pg_arman is running, stop delete.");
backup_list = catalog_get_backup_list(0);
for (i = 0; i < parray_num(backup_list); i++)
backup_list = catalog_get_backup_list(0);
for (i = 0; i < parray_num(backup_list); i++)
{
last_backup = (pgBackup *) parray_get(backup_list, i);
if (last_backup->status == BACKUP_STATUS_OK)
{
last_backup = (pgBackup *) parray_get(backup_list, i);
if (last_backup->status == BACKUP_STATUS_OK)
oldest_lsn = last_backup->start_lsn;
oldest_tli = last_backup->tli;
if (strict && backup_id != 0 && backup_id >= last_backup->start_time)
{
oldest_lsn = last_backup->start_lsn;
oldest_tli = last_backup->tli;
backup_found = true;
break;
}
}
catalog_unlock();
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
}
if (strict && backup_id != 0 && backup_found == false)
elog(ERROR, "not found backup for deletwal command");
catalog_unlock();
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
if (delete_wal && !XLogRecPtrIsInvalid(oldest_lsn))
if (!XLogRecPtrIsInvalid(oldest_lsn))
{
XLogSegNo targetSegNo;
char oldestSegmentNeeded[MAXFNAMELEN];

View File

@ -10,6 +10,7 @@ Usage:
pg_arman OPTION show [ID]
pg_arman OPTION validate [ID]
pg_arman OPTION delete ID
pg_arman OPTION delwal [ID]
Common Options:
-D, --pgdata=PATH location of the database storage area

View File

@ -110,7 +110,8 @@ main(int argc, char *argv[])
if(strcmp(cmd, "show") != 0 &&
strcmp(cmd, "validate") != 0 &&
strcmp(cmd, "delete") != 0 &&
strcmp(cmd, "restore") != 0)
strcmp(cmd, "restore") != 0 &&
strcmp(cmd, "delwal") != 0)
break;
} else if (backup_id_string == NULL)
backup_id_string = argv[i];
@ -209,6 +210,8 @@ main(int argc, char *argv[])
return do_validate(backup_id);
else if (pg_strcasecmp(cmd, "delete") == 0)
return do_delete(backup_id);
else if (pg_strcasecmp(cmd, "delwal") == 0)
return do_deletewal(backup_id, true);
else
elog(ERROR, "invalid command \"%s\"", cmd);
@ -226,6 +229,7 @@ pgut_help(bool details)
printf(_(" %s OPTION show [ID]\n"), PROGRAM_NAME);
printf(_(" %s OPTION validate [ID]\n"), PROGRAM_NAME);
printf(_(" %s OPTION delete ID\n"), PROGRAM_NAME);
printf(_(" %s OPTION delwal [ID]\n"), PROGRAM_NAME);
if (!details)
return;

View File

@ -230,6 +230,7 @@ extern int do_show(time_t backup_id, bool show_all);
/* in delete.c */
extern int do_delete(time_t backup_id);
extern void pgBackupDelete(int keep_generations, int keep_days);
extern int do_deletewal(time_t backup_id, bool strict);
/* in fetch.c */
extern char *slurpFile(const char *datadir,