Skip to content

Commit 9864f58

Browse files
committed
[Issue #120] Review
1 parent d5d58f5 commit 9864f58

File tree

6 files changed

+66
-52
lines changed

6 files changed

+66
-52
lines changed

doc/pgprobackup.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3239,7 +3239,7 @@ pg_probackup delete -B <replaceable>backup_dir</replaceable> --instance <replace
32393239
policy, without performing any irreversible actions.
32403240
</para>
32413241
<para>
3242-
To delete all backups with some status, use the <option>--status</option>:
3242+
To delete all backups with specific status, use the <option>--status</option>:
32433243
</para>
32443244
<programlisting>
32453245
pg_probackup delete -B <replaceable>backup_dir</replaceable> --instance <replaceable>instance_name</replaceable> --status=ERROR

src/delete.c

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ do_delete(time_t backup_id)
123123
* which FULL backup should be keeped for redundancy obligation(only valid do),
124124
* but if invalid backup is not guarded by retention - it is removed
125125
*/
126-
int do_retention(void)
126+
void do_retention(void)
127127
{
128128
parray *backup_list = NULL;
129129
parray *to_keep_list = parray_new();
@@ -154,7 +154,7 @@ int do_retention(void)
154154
/* Retention is disabled but we still can cleanup wal */
155155
elog(WARNING, "Retention policy is not set");
156156
if (!delete_wal)
157-
return 0;
157+
return;
158158
}
159159
else
160160
/* At least one retention policy is active */
@@ -196,9 +196,6 @@ int do_retention(void)
196196
parray_free(backup_list);
197197
parray_free(to_keep_list);
198198
parray_free(to_purge_list);
199-
200-
return 0;
201-
202199
}
203200

204201
/* Evaluate every backup by retention policies and populate purge and keep lists.
@@ -1024,42 +1021,58 @@ do_delete_instance(void)
10241021
return 0;
10251022
}
10261023

1027-
/* Delete all error backup files of given instance. */
1028-
int
1029-
do_delete_status(char* status)
1024+
/* Delete all backups of given status in instance */
1025+
void
1026+
do_delete_status(InstanceConfig *instance_config, const char *status)
10301027
{
10311028
parray *backup_list;
1032-
parray *xlog_files_list;
10331029
int i;
1034-
int rc;
1035-
char instance_config_path[MAXPGPATH];
1036-
1037-
BackupStatus status_for_delete;
1030+
const char *pretty_status;
1031+
int n_deleted = 0;
10381032

1039-
status_for_delete = str2status(status);
1033+
BackupStatus status_for_delete = str2status(status);
10401034

10411035
if (status_for_delete == BACKUP_STATUS_INVALID)
1042-
elog(ERROR, "Unknown '%s' value for --status option", status);
1036+
elog(ERROR, "Unknown value for '--status' option: '%s'", status);
1037+
1038+
/*
1039+
* User may have provided status string in lower case, but
1040+
* we should print backup statuses consistently with show command,
1041+
* so convert it.
1042+
*/
1043+
pretty_status = status2str(status_for_delete);
10431044

1045+
backup_list = catalog_get_backup_list(instance_config->name, INVALID_BACKUP_ID);
10441046

1045-
/* Delete all error backups. */
1046-
backup_list = catalog_get_backup_list(instance_name, INVALID_BACKUP_ID);
1047+
if (parray_num(backup_list) == 0)
1048+
{
1049+
elog(WARNING, "Instance '%s' has no backups", instance_config->name);
1050+
return;
1051+
}
1052+
1053+
elog(INFO, "Deleting all backups with status '%s'", pretty_status);
10471054

1055+
/* Delete all backups with specified status */
10481056
for (i = 0; i < parray_num(backup_list); i++)
10491057
{
10501058
pgBackup *backup = (pgBackup *) parray_get(backup_list, i);
1051-
if (backup->status == status_for_delete){
1052-
/* elog(INFO, "Delete error backup '%s' ", base36enc(backup->backup_id)); */
1053-
catalog_lock_backup_list(backup_list, i, i);
1059+
1060+
if (backup->status == status_for_delete)
1061+
{
1062+
lock_backup(backup);
10541063
delete_backup_files(backup);
1064+
n_deleted++;
10551065
}
10561066
}
10571067

1068+
if (n_deleted > 0)
1069+
elog(INFO, "Successfully deleted all backups with status '%s' from instance '%s'",
1070+
pretty_status, instance_config->name);
1071+
else
1072+
elog(WARNING, "Instance '%s' has no backups with status '%s'",
1073+
instance_config->name, pretty_status);
1074+
10581075
/* Cleanup */
10591076
parray_walk(backup_list, pgBackupFree);
10601077
parray_free(backup_list);
1061-
1062-
1063-
elog(INFO, "Backups with status '%s' from instance '%s' successfully deleted", status, instance_name);
1064-
return 0;
10651078
}

src/pg_probackup.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,16 @@ main(int argc, char *argv[])
806806
elog(ERROR, "You cannot specify --merge-expired and (-i, --backup-id) options together");
807807
if (delete_status && backup_id_string)
808808
elog(ERROR, "You cannot specify --status and (-i, --backup-id) options together");
809-
if (!delete_expired && !merge_expired && !delete_wal && delete_status==NULL && !backup_id_string)
809+
if (!delete_expired && !merge_expired && !delete_wal && delete_status == NULL && !backup_id_string)
810810
elog(ERROR, "You must specify at least one of the delete options: "
811811
"--delete-expired |--delete-wal |--merge-expired |--status |(-i, --backup-id)");
812812
if (!backup_id_string)
813-
if (delete_status)
814-
do_delete_status(delete_status);
813+
{
814+
if (delete_status)
815+
do_delete_status(&instance_config, delete_status);
815816
else
816-
return do_retention();
817+
do_retention();
818+
}
817819
else
818820
do_delete(current.backup_id);
819821
break;

src/pg_probackup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,9 @@ extern int do_show(const char *instance_name, time_t requested_backup_id, bool s
709709
/* in delete.c */
710710
extern void do_delete(time_t backup_id);
711711
extern void delete_backup_files(pgBackup *backup);
712-
extern int do_retention(void);
712+
extern void do_retention(void);
713713
extern int do_delete_instance(void);
714-
extern int do_delete_status(char *status);
714+
extern void do_delete_status(InstanceConfig *instance_config, const char *status);
715715

716716
/* in fetch.c */
717717
extern char *slurpFile(const char *datadir,

src/util.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818

1919
#include <sys/stat.h>
2020

21+
static const char *statusName[] =
22+
{
23+
"UNKNOWN",
24+
"OK",
25+
"ERROR",
26+
"RUNNING",
27+
"MERGING",
28+
"MERGED",
29+
"DELETING",
30+
"DELETED",
31+
"DONE",
32+
"ORPHAN",
33+
"CORRUPT"
34+
};
35+
2136
const char *
2237
base36enc(long unsigned int value)
2338
{
@@ -458,20 +473,6 @@ parse_program_version(const char *program_version)
458473

459474
return result;
460475
}
461-
static const char *statusName[] =
462-
{
463-
"UNKNOWN",
464-
"OK",
465-
"ERROR",
466-
"RUNNING",
467-
"MERGING",
468-
"MERGED",
469-
"DELETING",
470-
"DELETED",
471-
"DONE",
472-
"ORPHAN",
473-
"CORRUPT"
474-
};
475476

476477
const char *
477478
status2str(BackupStatus status)
@@ -482,16 +483,15 @@ status2str(BackupStatus status)
482483
return statusName[status];
483484
}
484485

485-
486-
487486
BackupStatus
488487
str2status(const char *status)
489488
{
489+
BackupStatus i;
490490

491-
for (int i = 0; i <= BACKUP_STATUS_CORRUPT; i++)
491+
for (i = BACKUP_STATUS_INVALID; i <= BACKUP_STATUS_CORRUPT; i++)
492492
{
493493
if (pg_strcasecmp(status, statusName[i]) == 0) return i;
494494
}
495495

496-
return 0;
496+
return BACKUP_STATUS_INVALID;
497497
}

tests/delete.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,5 @@ def test_delete_error_backups(self):
845845
self.assertEqual(show_backups[0]['status'], "OK")
846846
self.assertEqual(show_backups[1]['status'], "OK")
847847

848-
# Clean after yourself
849-
self.del_test_dir(module_name, fname)
850-
848+
# Clean after yourself
849+
self.del_test_dir(module_name, fname)

0 commit comments

Comments
 (0)