-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathdelete.c
More file actions
419 lines (353 loc) · 10.7 KB
/
delete.c
File metadata and controls
419 lines (353 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*-------------------------------------------------------------------------
*
* delete.c: delete backup files.
*
* Copyright (c) 2009-2013, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
*-------------------------------------------------------------------------
*/
#include "pg_probackup.h"
#include <dirent.h>
#include <time.h>
#include <unistd.h>
static int pgBackupDeleteFiles(pgBackup *backup);
static void delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli,
bool delete_all);
int
do_delete(time_t backup_id)
{
int i;
int b_index;
int ret;
parray *backup_list;
pgBackup *last_backup = NULL;
/* DATE are always required */
if (backup_id == 0)
elog(ERROR, "required backup ID not specified");
/* Lock backup catalog */
ret = catalog_lock(false);
if (ret == -1)
elog(ERROR, "can't lock backup catalog.");
else if (ret == 1)
elog(ERROR,
"another pg_probackup is running, stop delete.");
/* Get complete list of backups */
backup_list = catalog_get_backup_list(0);
if (!backup_list)
elog(ERROR, "No backup list found, can't process any more.");
/* Find backup to be deleted */
for (i = 0; i < parray_num(backup_list); i++)
{
last_backup = (pgBackup *) parray_get(backup_list, i);
if (last_backup->start_time == backup_id)
goto found_backup;
}
elog(ERROR, "no backup found, cannot delete.");
found_backup:
b_index = i;
/* check for interrupt */
if (interrupted)
elog(ERROR, "interrupted during delete backup");
/* just do it */
pgBackupDeleteFiles(last_backup);
if (last_backup->status == BACKUP_STATUS_ERROR)
return 0;
/* remove all increments after removed backup */
for (i = b_index - 1; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(backup_list, i);
if (backup->backup_mode >= BACKUP_MODE_FULL)
break;
if ((backup->status == BACKUP_STATUS_OK || backup->status == BACKUP_STATUS_CORRUPT) &&
(backup->backup_mode == BACKUP_MODE_DIFF_PAGE || backup->backup_mode == BACKUP_MODE_DIFF_PTRACK)
)
pgBackupDeleteFiles(backup);
}
/* release catalog lock */
catalog_unlock();
/* cleanup */
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
if (delete_wal)
do_deletewal(backup_id, false);
return 0;
}
/*
* 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.
*/
int
do_deletewal(time_t backup_id, bool strict)
{
size_t i;
int ret;
parray *backup_list;
XLogRecPtr oldest_lsn = InvalidXLogRecPtr;
TimeLineID oldest_tli;
bool backup_found = false;
/* Lock backup catalog */
ret = catalog_lock(false);
if (ret == -1)
elog(ERROR, "can't lock backup catalog.");
else if (ret == 1)
elog(ERROR,
"another pg_probackup is running, stop delete.");
/* Find oldest LSN, used by backups */
backup_list = catalog_get_backup_list(0);
for (i = 0; i < parray_num(backup_list); i++)
{
pgBackup *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)
{
backup_found = true;
break;
}
}
}
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);
delete_walfiles(oldest_lsn, oldest_tli, true);
return 0;
}
/*
* Remove backups by retention policy. Retention policy is configured by
* retention_redundancy and retention_window variables.
*/
int
do_retention_purge(void)
{
parray *backup_list;
uint32 backup_num;
size_t i;
time_t days_threshold = time(NULL) - (retention_window * 60 * 60 * 24);
XLogRecPtr oldest_lsn = InvalidXLogRecPtr;
TimeLineID oldest_tli;
int ret;
bool keep_next_backup = true; /* Do not delete first full backup */
if (retention_redundancy > 0)
elog(LOG, "REDUNDANCY=%u", retention_redundancy);
if (retention_window > 0)
elog(LOG, "WINDOW=%u", retention_window);
if (retention_redundancy == 0 && retention_window == 0)
elog(ERROR, "retention policy is not set");
/* Lock backup catalog */
ret = catalog_lock(false);
if (ret == 1)
elog(ERROR,
"cannot lock backup catalog, another pg_probackup is running");
/* Get a complete list of backups. */
backup_list = catalog_get_backup_list(0);
if (parray_num(backup_list) == 0)
{
elog(INFO, "backup list is empty");
elog(INFO, "exit");
catalog_unlock();
return 0;
}
/* Find target backups to be deleted */
backup_num = 0;
for (i = 0; i < parray_num(backup_list); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backup_list, i);
uint32 backup_num_evaluate = backup_num;
/* Consider only validated and correct backups */
if (backup->status != BACKUP_STATUS_OK)
continue;
/*
* When a validate full backup was found, we can delete the
* backup that is older than it using the number of generations.
*/
if (backup->backup_mode == BACKUP_MODE_FULL)
backup_num++;
/* Evaluate if this backup is eligible for removal */
if (keep_next_backup ||
backup_num_evaluate + 1 <= retention_redundancy ||
(retention_window > 0 && backup->recovery_time >= days_threshold))
{
/* Save LSN and Timeline to remove unnecessary WAL segments */
oldest_lsn = backup->start_lsn;
oldest_tli = backup->tli;
/* Save parent backup of this incremental backup */
if (backup->backup_mode != BACKUP_MODE_FULL)
keep_next_backup = true;
/*
* Previous incremental backup was kept or this is first backup
* so do not delete this backup.
*/
else
keep_next_backup = false;
continue;
}
/* Delete backup and update status to DELETED */
pgBackupDeleteFiles(backup);
}
/* Purge WAL files */
delete_walfiles(oldest_lsn, oldest_tli, true);
/* Cleanup */
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
catalog_unlock();
elog(INFO, "purging is finished");
return 0;
}
/*
* Delete backup files of the backup and update the status of the backup to
* BACKUP_STATUS_DELETED.
*/
static int
pgBackupDeleteFiles(pgBackup *backup)
{
size_t i;
char path[MAXPGPATH];
char timestamp[20];
parray *files;
/*
* If the backup was deleted already, there is nothing to do.
*/
if (backup->status == BACKUP_STATUS_DELETED)
return 0;
time2iso(timestamp, lengthof(timestamp), backup->recovery_time);
elog(INFO, "delete: %s %s", base36enc(backup->start_time), timestamp);
/*
* Update STATUS to BACKUP_STATUS_DELETING in preparation for the case which
* the error occurs before deleting all backup files.
*/
if (!check)
{
backup->status = BACKUP_STATUS_DELETING;
pgBackupWriteIni(backup);
}
/* list files to be deleted */
files = parray_new();
pgBackupGetPath(backup, path, lengthof(path), NULL);
dir_list_file(files, path, false, true, true);
/* delete leaf node first */
parray_qsort(files, pgFileComparePathDesc);
for (i = 0; i < parray_num(files); i++)
{
pgFile *file = (pgFile *) parray_get(files, i);
/* print progress */
elog(LOG, "delete file(%zd/%lu) \"%s\"", i + 1,
(unsigned long) parray_num(files), file->path);
/* skip actual deletion in check mode */
if (!check)
{
if (remove(file->path))
{
elog(WARNING, "can't remove \"%s\": %s", file->path,
strerror(errno));
parray_walk(files, pgFileFree);
parray_free(files);
return 1;
}
}
}
parray_walk(files, pgFileFree);
parray_free(files);
return 0;
}
/*
* Delete WAL segments up to oldest_lsn.
*
* If oldest_lsn is invalid function exists. But if delete_all is true then
* WAL segements will be deleted anyway.
*/
static void
delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli, bool delete_all)
{
XLogSegNo targetSegNo;
char oldestSegmentNeeded[MAXFNAMELEN];
DIR *arcdir;
struct dirent *arcde;
char wal_file[MAXPGPATH];
char max_wal_file[MAXPGPATH];
char min_wal_file[MAXPGPATH];
int rc;
if (XLogRecPtrIsInvalid(oldest_lsn) && !delete_all)
return;
max_wal_file[0] = '\0';
min_wal_file[0] = '\0';
if (!XLogRecPtrIsInvalid(oldest_lsn))
{
XLByteToSeg(oldest_lsn, targetSegNo);
XLogFileName(oldestSegmentNeeded, oldest_tli, targetSegNo);
elog(LOG, "removing WAL segments older than %s", oldestSegmentNeeded);
}
else
elog(LOG, "removing all WAL segments");
/*
* Now it is time to do the actual work and to remove all the segments
* not needed anymore.
*/
if ((arcdir = opendir(arclog_path)) != NULL)
{
while (errno = 0, (arcde = readdir(arcdir)) != NULL)
{
/*
* We ignore the timeline part of the XLOG segment identifiers in
* deciding whether a segment is still needed. This ensures that
* we won't prematurely remove a segment from a parent timeline.
* We could probably be a little more proactive about removing
* segments of non-parent timelines, but that would be a whole lot
* more complicated.
*
* We use the alphanumeric sorting property of the filenames to
* decide which ones are earlier than the exclusiveCleanupFileName
* file. Note that this means files are not removed in the order
* they were originally written, in case this worries you.
*/
if (IsXLogFileName(arcde->d_name) ||
IsPartialXLogFileName(arcde->d_name) ||
IsBackupHistoryFileName(arcde->d_name))
{
if (XLogRecPtrIsInvalid(oldest_lsn) ||
strncmp(arcde->d_name + 8, oldestSegmentNeeded + 8, 16) < 0)
{
/*
* Use the original file name again now, including any
* extension that might have been chopped off before testing
* the sequence.
*/
snprintf(wal_file, MAXPGPATH, "%s/%s",
arclog_path, arcde->d_name);
rc = unlink(wal_file);
if (rc != 0)
{
elog(WARNING, "could not remove file \"%s\": %s",
wal_file, strerror(errno));
break;
}
if (verbose)
elog(LOG, "removed WAL segment \"%s\"", wal_file);
if (max_wal_file[0] == '\0' ||
strcmp(max_wal_file + 8, arcde->d_name + 8) < 0)
strcpy(max_wal_file, arcde->d_name);
if (min_wal_file[0] == '\0' ||
strcmp(min_wal_file + 8, arcde->d_name + 8) > 0)
strcpy(min_wal_file, arcde->d_name);
}
}
}
if (!verbose && min_wal_file[0] != '\0')
elog(INFO, "removed min WAL segment \"%s\"", min_wal_file);
if (!verbose && max_wal_file[0] != '\0')
elog(INFO, "removed max WAL segment \"%s\"", max_wal_file);
if (errno)
elog(WARNING, "could not read archive location \"%s\": %s",
arclog_path, strerror(errno));
if (closedir(arcdir))
elog(WARNING, "could not close archive location \"%s\": %s",
arclog_path, strerror(errno));
}
else
elog(WARNING, "could not open archive location \"%s\": %s",
arclog_path, strerror(errno));
}