Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a53c4c1

Browse files
committed
backup: allow to use delete-wal without delete-expired(in accordance with documentation)
1 parent 5ac16cd commit a53c4c1

File tree

3 files changed

+74
-52
lines changed

3 files changed

+74
-52
lines changed

src/backup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ do_backup(time_t start_time)
855855
* After successfil backup completion remove backups
856856
* which are expired according to retention policies
857857
*/
858-
if (delete_expired)
858+
if (delete_expired || delete_wal)
859859
do_retention_purge();
860860

861861
return 0;

src/delete.c

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,19 @@ do_retention_purge(void)
134134
bool keep_next_backup = true; /* Do not delete first full backup */
135135
bool backup_deleted = false; /* At least one backup was deleted */
136136

137-
if (retention_redundancy > 0)
138-
elog(LOG, "REDUNDANCY=%u", retention_redundancy);
139-
if (retention_window > 0)
140-
elog(LOG, "WINDOW=%u", retention_window);
141-
142-
if (retention_redundancy == 0 && retention_window == 0 && !delete_wal)
137+
if (delete_expired)
143138
{
144-
elog(WARNING, "Retention policy is not set");
145-
return 0;
139+
if (retention_redundancy > 0)
140+
elog(LOG, "REDUNDANCY=%u", retention_redundancy);
141+
if (retention_window > 0)
142+
elog(LOG, "WINDOW=%u", retention_window);
143+
144+
if (retention_redundancy == 0 && retention_window == 0)
145+
{
146+
elog(WARNING, "Retention policy is not set");
147+
if (!delete_wal)
148+
return 0;
149+
}
146150
}
147151

148152
/* Get exclusive lock of backup catalog */
@@ -157,52 +161,67 @@ do_retention_purge(void)
157161
}
158162

159163
/* Find target backups to be deleted */
160-
backup_num = 0;
161-
for (i = 0; i < parray_num(backup_list); i++)
164+
if (delete_expired && (retention_redundancy > 0 || retention_window > 0))
162165
{
163-
pgBackup *backup = (pgBackup *) parray_get(backup_list, i);
164-
uint32 backup_num_evaluate = backup_num;
165-
166-
/* Consider only validated and correct backups */
167-
if (backup->status != BACKUP_STATUS_OK)
168-
continue;
169-
170-
/*
171-
* When a validate full backup was found, we can delete the
172-
* backup that is older than it using the number of generations.
173-
*/
174-
if (backup->backup_mode == BACKUP_MODE_FULL)
175-
backup_num++;
176-
177-
/* Evaluate if this backup is eligible for removal */
178-
if (keep_next_backup ||
179-
backup_num_evaluate + 1 <= retention_redundancy ||
180-
(retention_window > 0 && backup->recovery_time >= days_threshold))
166+
backup_num = 0;
167+
for (i = 0; i < parray_num(backup_list); i++)
181168
{
182-
/* Save LSN and Timeline to remove unnecessary WAL segments */
183-
oldest_lsn = backup->start_lsn;
184-
oldest_tli = backup->tli;
169+
pgBackup *backup = (pgBackup *) parray_get(backup_list, i);
170+
uint32 backup_num_evaluate = backup_num;
185171

186-
/* Save parent backup of this incremental backup */
187-
if (backup->backup_mode != BACKUP_MODE_FULL)
188-
keep_next_backup = true;
172+
/* Consider only validated and correct backups */
173+
if (backup->status != BACKUP_STATUS_OK)
174+
continue;
189175
/*
190-
* Previous incremental backup was kept or this is first backup
191-
* so do not delete this backup.
176+
* When a validate full backup was found, we can delete the
177+
* backup that is older than it using the number of generations.
192178
*/
193-
else
194-
keep_next_backup = false;
179+
if (backup->backup_mode == BACKUP_MODE_FULL)
180+
backup_num++;
195181

196-
continue;
182+
/* Evaluateretention_redundancy if this backup is eligible for removal */
183+
if (keep_next_backup ||
184+
retention_redundancy >= backup_num_evaluate + 1 ||
185+
(retention_window > 0 && backup->recovery_time >= days_threshold))
186+
{
187+
/* Save LSN and Timeline to remove unnecessary WAL segments */
188+
oldest_lsn = backup->start_lsn;
189+
oldest_tli = backup->tli;
190+
191+
/* Save parent backup of this incremental backup */
192+
if (backup->backup_mode != BACKUP_MODE_FULL)
193+
keep_next_backup = true;
194+
/*
195+
* Previous incremental backup was kept or this is first backup
196+
* so do not delete this backup.
197+
*/
198+
else
199+
keep_next_backup = false;
200+
201+
continue;
202+
}
203+
/* Delete backup and update status to DELETED */
204+
pgBackupDeleteFiles(backup);
205+
backup_deleted = true;
197206
}
207+
}
198208

199-
/* Delete backup and update status to DELETED */
200-
pgBackupDeleteFiles(backup);
201-
backup_deleted = true;
209+
/*
210+
* If oldest_lsn wasn`t set in prevous step
211+
* get oldest backup LSN and TLI for WAL purging
212+
*/
213+
if (delete_wal && (XLogRecPtrIsInvalid(oldest_lsn)))
214+
{
215+
pgBackup *backup = (pgBackup *) parray_get(backup_list, parray_num(backup_list) - 1);
216+
oldest_lsn = backup->start_lsn;
217+
oldest_tli = backup->tli;
202218
}
203219

204220
/* Purge WAL files */
205-
delete_walfiles(oldest_lsn, oldest_tli);
221+
if (delete_wal)
222+
{
223+
delete_walfiles(oldest_lsn, oldest_tli);
224+
}
206225

207226
/* Cleanup */
208227
parray_walk(backup_list, pgBackupFree);
@@ -257,7 +276,7 @@ pgBackupDeleteFiles(pgBackup *backup)
257276
pgFile *file = (pgFile *) parray_get(files, i);
258277

259278
/* print progress */
260-
elog(LOG, "delete file(%zd/%lu) \"%s\"", i + 1,
279+
elog(VERBOSE, "delete file(%zd/%lu) \"%s\"", i + 1,
261280
(unsigned long) parray_num(files), file->path);
262281

263282
if (remove(file->path))

src/pg_probackup.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ static pgut_option options[] =
114114
{ 'f', 'b', "backup-mode", opt_backup_mode, SOURCE_CMDLINE },
115115
{ 'b', 'C', "smooth-checkpoint", &smooth_checkpoint, SOURCE_CMDLINE },
116116
{ 's', 'S', "slot", &replication_slot, SOURCE_CMDLINE },
117-
{ 'u', 11, "archive-timeout", &archive_timeout, SOURCE_CMDLINE },
118-
{ 'b', 12, "delete-expired", &delete_expired, SOURCE_CMDLINE },
119-
{ 's', 13, "master-db", &master_db, SOURCE_CMDLINE, },
120-
{ 's', 14, "master-host", &master_host, SOURCE_CMDLINE, },
121-
{ 's', 15, "master-port", &master_port, SOURCE_CMDLINE, },
122-
{ 's', 16, "master-user", &master_user, SOURCE_CMDLINE, },
123-
{ 'u', 17, "replica-timeout", &replica_timeout, SOURCE_CMDLINE, SOURCE_DEFAULT, OPTION_UNIT_S },
117+
{ 'u', 11, "archive-timeout", &archive_timeout, SOURCE_CMDLINE, SOURCE_DEFAULT, OPTION_UNIT_S },
118+
{ 'b', 12, "delete-wal", &delete_wal, SOURCE_CMDLINE },
119+
{ 'b', 13, "delete-expired", &delete_expired, SOURCE_CMDLINE },
120+
{ 's', 14, "master-db", &master_db, SOURCE_CMDLINE, },
121+
{ 's', 15, "master-host", &master_host, SOURCE_CMDLINE, },
122+
{ 's', 16, "master-port", &master_port, SOURCE_CMDLINE, },
123+
{ 's', 17, "master-user", &master_user, SOURCE_CMDLINE, },
124+
{ 'u', 18, "replica-timeout", &replica_timeout, SOURCE_CMDLINE, SOURCE_DEFAULT, OPTION_UNIT_S },
124125
/* TODO not completed feature. Make it unavailiable from user level
125126
{ 'b', 18, "remote", &is_remote_backup, SOURCE_CMDLINE, }, */
126127
/* restore options */
@@ -457,6 +458,8 @@ main(int argc, char *argv[])
457458
elog(ERROR, "You cannot specify --delete-expired and --backup-id options together");
458459
if (!delete_expired && !delete_wal && !backup_id_string_param)
459460
elog(ERROR, "You must specify at least one of the delete options: --expired |--wal |--backup_id");
461+
if (delete_wal && !delete_expired && !backup_id_string_param)
462+
return do_retention_purge();
460463
if (delete_expired)
461464
return do_retention_purge();
462465
else

0 commit comments

Comments
 (0)