Skip to content

Commit ad41956

Browse files
committed
Added deletion all childs with any status when deleting backup with specific status (--status)
1 parent accfc34 commit ad41956

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

src/delete.c

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,41 +1021,36 @@ do_delete_instance(void)
10211021
return 0;
10221022
}
10231023

1024-
/* checks that backup childs has status like parent */
1025-
bool checkChilds(parray *backup_list, time_t backup_id)
1024+
/* checks that parray contains element */
1025+
bool parray_contains(parray *array, void *elem)
10261026
{
10271027
int i;
1028-
pgBackup *target_backup = NULL;
1029-
pgBackup *backup;
1030-
/* search target bakcup */
1031-
for (i = 0; i < parray_num(backup_list); i++)
1028+
for (i = 0; i < parray_num(array); i++)
10321029
{
1033-
backup = (pgBackup *)parray_get(backup_list, i);
1034-
if (backup->start_time == backup_id)
1035-
{
1036-
target_backup = backup;
1037-
break;
1038-
}
1030+
if (parray_get(array, i) == elem) return true;
10391031
}
1040-
if (target_backup == NULL) return false;
1032+
return false;
1033+
}
1034+
10411035

1042-
/* check childs */
1036+
void append_childs(parray *backup_list, pgBackup *target_backup, parray *delete_list)
1037+
{
1038+
int i;
1039+
pgBackup *backup;
10431040
for (i = 0; i < parray_num(backup_list); i++)
10441041
{
10451042
backup = (pgBackup *)parray_get(backup_list, i);
1043+
if (backup == target_backup) continue;
10461044
/* check if backup is descendant of delete target */
10471045
if (is_parent(target_backup->start_time, backup, false))
10481046
{
1049-
if (backup->status != target_backup->status){
1050-
elog(INFO, "Skip deleting the backup %s because the backup has children with a different status",
1051-
base36enc(target_backup->start_time));
1052-
return false;
1053-
}
1047+
if (!parray_contains(delete_list, backup))
1048+
parray_append(delete_list, backup);
10541049
/* recursive call */
1055-
if (!checkChilds(backup_list, backup->start_time)) return false;
1050+
append_childs(backup_list, backup, delete_list);
10561051
}
10571052
}
1058-
return true;
1053+
10591054
}
10601055

10611056
/* Delete all backups of given status in instance */
@@ -1093,30 +1088,42 @@ do_delete_status(InstanceConfig *instance_config, const char *status)
10931088

10941089
elog(INFO, "Deleting all backups with status '%s'", pretty_status);
10951090

1096-
/* Selects backups for deleting to delete_list array. Will delete all backups with specified status */
1091+
/* Selects backups for deleting to delete_list array. Will delete all backups with specified status and childs*/
10971092
for (i = 0; i < parray_num(backup_list); i++)
10981093
{
10991094
backup = (pgBackup *) parray_get(backup_list, i);
11001095

11011096
if (backup->status == status_for_delete)
11021097
{
1103-
11041098
n_found++;
1105-
if (!checkChilds(backup_list, backup->start_time)) continue;
1099+
if (parray_contains(delete_list, backup)) continue;
1100+
parray_append(delete_list, backup);
1101+
append_childs(backup_list, backup, delete_list);
1102+
}
1103+
}
1104+
/* delete and calculate free size from delete_list */
1105+
for (i = 0; i < parray_num(delete_list); i++)
1106+
{
1107+
backup = (pgBackup *)parray_get(delete_list, i);
1108+
elog(dry_run ? INFO : LOG, "Backup %s with status %s %s be deleted",
1109+
base36enc(backup->start_time), status2str(backup->status), dry_run ? "can" : "will");
11061110

1107-
elog(dry_run ? INFO: LOG, "Backup %s %s be deleted",
1108-
base36enc(backup->start_time), dry_run ? "can" : "will");
1109-
size_to_delete += backup->data_bytes;
1110-
if (backup->stream)
1111-
size_to_delete += backup->wal_bytes;
1112-
if (!dry_run){
1113-
parray_append(delete_list, backup);
1114-
}
1111+
size_to_delete += backup->data_bytes;
1112+
if (backup->stream)
1113+
size_to_delete += backup->wal_bytes;
1114+
1115+
1116+
if (!dry_run && lock_backup(backup))
1117+
{
1118+
if (interrupted)
1119+
elog(ERROR, "interrupted during delete backup");
1120+
1121+
delete_backup_files(backup);
11151122
n_deleted++;
11161123

11171124
}
1118-
}
11191125

1126+
}
11201127
/* Inform about data size to free */
11211128
if (size_to_delete >= 0)
11221129
{
@@ -1127,27 +1134,19 @@ do_delete_status(InstanceConfig *instance_config, const char *status)
11271134

11281135
/* delete selected backups */
11291136
if (!dry_run)
1130-
{
1131-
for (i = 0; i < parray_num(delete_list); i++)
1132-
{
1133-
backup = (pgBackup *)parray_get(delete_list, i);
1134-
if (lock_backup(backup))
1135-
{
1136-
delete_backup_files(backup);
1137-
}
1138-
else n_deleted--;
1139-
}
1140-
elog(INFO, "Successfully deleted %i %s with status '%s' from instance '%s'",
1137+
elog(INFO, "Successfully deleted %i %s from instance '%s'",
11411138
n_deleted, n_deleted == 1 ? "backup" : "backups",
1142-
pretty_status, instance_config->name);
1143-
1144-
}
1139+
instance_config->name);
11451140

11461141

11471142
if (n_found == 0)
11481143
elog(WARNING, "Instance '%s' has no backups with status '%s'",
11491144
instance_config->name, pretty_status);
11501145

1146+
/* Clean WAL segments */
1147+
if (delete_wal)
1148+
do_retention_wal(dry_run);
1149+
11511150
/* Cleanup */
11521151
parray_free(delete_list);
11531152
parray_walk(backup_list, pgBackupFree);

src/help.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ help_pg_probackup(void)
193193
printf(_(" [--retention-redundancy=retention-redundancy]\n"));
194194
printf(_(" [--retention-window=retention-window]\n"));
195195
printf(_(" [--wal-depth=wal-depth]\n"));
196-
printf(_(" [--delete-wal] [-i backup-id | --delete-expired | --merge-expired]\n"));
196+
printf(_(" [--delete-wal] [-i backup-id | --delete-expired | --merge-expired | --status= ]\n"));
197197
printf(_(" [--dry-run]\n"));
198198
printf(_(" [--help]\n"));
199199

@@ -635,7 +635,8 @@ help_delete(void)
635635
printf(_(" --wal-depth=wal-depth number of latest valid backups per timeline that must\n"));
636636
printf(_(" retain the ability to perform PITR; 0 disables; (default: 0)\n"));
637637
printf(_(" --dry-run perform a trial run without any changes\n"));
638-
638+
printf(_(" --status=backups_status delete all backups with specific status\n"));
639+
639640
printf(_("\n Logging options:\n"));
640641
printf(_(" --log-level-console=log-level-console\n"));
641642
printf(_(" level for console logging (default: info)\n"));

0 commit comments

Comments
 (0)