-
Notifications
You must be signed in to change notification settings - Fork 203
/
expire.c
688 lines (584 loc) · 34.9 KB
/
expire.c
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/***********************************************************************************************************************************
Expire Command
***********************************************************************************************************************************/
#include "build.auto.h"
#include "command/archive/common.h"
#include "command/backup/common.h"
#include "common/type/list.h"
#include "common/debug.h"
#include "common/regExp.h"
#include "config/config.h"
#include "info/infoArchive.h"
#include "info/infoBackup.h"
#include "info/manifest.h"
#include "storage/helper.h"
#include <stdlib.h>
/***********************************************************************************************************************************
Helper functions and structures
***********************************************************************************************************************************/
typedef struct ArchiveExpired
{
uint64_t total;
String *start;
String *stop;
} ArchiveExpired;
static int
archiveIdAscComparator(const void *item1, const void *item2)
{
StringList *archiveSort1 = strLstNewSplitZ(*(String **)item1, "-");
StringList *archiveSort2 = strLstNewSplitZ(*(String **)item2, "-");
int int1 = atoi(strPtr(strLstGet(archiveSort1, 1)));
int int2 = atoi(strPtr(strLstGet(archiveSort2, 1)));
return (int1 - int2);
}
static int
archiveIdDescComparator(const void *item1, const void *item2)
{
StringList *archiveSort1 = strLstNewSplitZ(*(String **)item1, "-");
StringList *archiveSort2 = strLstNewSplitZ(*(String **)item2, "-");
int int1 = atoi(strPtr(strLstGet(archiveSort1, 1)));
int int2 = atoi(strPtr(strLstGet(archiveSort2, 1)));
return (int2 - int1);
}
static StringList *
sortArchiveId(StringList *sortString, SortOrder sortOrder)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(STRING_LIST, sortString);
FUNCTION_LOG_PARAM(ENUM, sortOrder);
FUNCTION_LOG_END();
ASSERT(sortString != NULL);
switch (sortOrder)
{
case sortOrderAsc:
{
lstSort((List *)sortString, archiveIdAscComparator);
break;
}
case sortOrderDesc:
{
lstSort((List *)sortString, archiveIdDescComparator);
break;
}
}
FUNCTION_LOG_RETURN(STRING_LIST, sortString);
}
typedef struct ArchiveRange
{
const String *start;
const String *stop;
} ArchiveRange;
/***********************************************************************************************************************************
Common function for expiring any backup
***********************************************************************************************************************************/
static void
expireBackup(InfoBackup *infoBackup, String *removeBackupLabel, String *backupExpired)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(INFO_BACKUP, infoBackup);
FUNCTION_LOG_PARAM(STRING, removeBackupLabel);
FUNCTION_LOG_PARAM(STRING, backupExpired);
FUNCTION_LOG_END();
ASSERT(infoBackup != NULL);
ASSERT(removeBackupLabel != NULL);
ASSERT(backupExpired != NULL);
storageRemoveNP(storageRepoWrite(), strNewFmt(STORAGE_REPO_BACKUP "/%s/" MANIFEST_FILE, strPtr(removeBackupLabel)));
storageRemoveNP(
storageRepoWrite(), strNewFmt(STORAGE_REPO_BACKUP "/%s/" MANIFEST_FILE INFO_COPY_EXT, strPtr(removeBackupLabel)));
// Remove the backup from the info file
infoBackupDataDelete(infoBackup, removeBackupLabel);
if (strSize(backupExpired) == 0)
strCat(backupExpired, strPtr(removeBackupLabel));
else
strCatFmt(backupExpired, ", %s", strPtr(removeBackupLabel));
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Expire differential backups
***********************************************************************************************************************************/
static unsigned int
expireDiffBackup(InfoBackup *infoBackup)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(INFO_BACKUP, infoBackup);
FUNCTION_LOG_END();
ASSERT(infoBackup != NULL);
unsigned int result = 0;
MEM_CONTEXT_TEMP_BEGIN()
{
unsigned int differentialRetention = cfgOptionTest(cfgOptRepoRetentionDiff) ? cfgOptionUInt(cfgOptRepoRetentionDiff) : 0;
// Find all the expired differential backups
if (differentialRetention > 0)
{
// Get a list of full and differential backups. Full are considered differential for the purpose of retention.
// Example: F1, D1, D2, F2, repo-retention-diff=2, then F1,D2,F2 will be retained, not D2 and D1 as might be expected.
StringList *currentBackupList = infoBackupDataLabelList(infoBackup, backupRegExpP(.full = true, .differential = true));
// If there are more backups than the number to retain, then expire the oldest ones
if (strLstSize(currentBackupList) > differentialRetention)
{
for (unsigned int diffIdx = 0; diffIdx < strLstSize(currentBackupList) - differentialRetention; diffIdx++)
{
// Skip if this is a full backup. Full backups only count as differential when deciding which differential
// backups to expire.
if (regExpMatchOne(backupRegExpP(.full = true), strLstGet(currentBackupList, diffIdx)))
continue;
// Get a list of all differential and incremental backups
StringList *removeList = infoBackupDataLabelList(
infoBackup, backupRegExpP(.differential = true, .incremental = true));
// Initialize the log message
String *backupExpired = strNew("");
// Remove the manifest files in each directory and remove the backup from the current section of backup.info
for (unsigned int rmvIdx = 0; rmvIdx < strLstSize(removeList); rmvIdx++)
{
// Remove all differential and incremental backups before the oldest valid differential
// (removeBackupLabel < oldest valid differential)
String *removeBackupLabel = strLstGet(removeList, rmvIdx);
if (strCmp(removeBackupLabel, strLstGet(currentBackupList, diffIdx + 1)) < 0)
{
expireBackup(infoBackup, removeBackupLabel, backupExpired);
result++;
}
}
// If the message contains a comma, then prepend "set:"
LOG_INFO("expire diff backup %s%s", (strChr(backupExpired, ',') != -1 ? "set: " : ""), strPtr(backupExpired));
}
}
}
}
MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN(UINT, result);
}
/***********************************************************************************************************************************
Expire full backups
***********************************************************************************************************************************/
static unsigned int
expireFullBackup(InfoBackup *infoBackup)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(INFO_BACKUP, infoBackup);
FUNCTION_LOG_END();
ASSERT(infoBackup != NULL);
unsigned int result = 0;
MEM_CONTEXT_TEMP_BEGIN()
{
unsigned int fullRetention = cfgOptionTest(cfgOptRepoRetentionFull) ? cfgOptionUInt(cfgOptRepoRetentionFull) : 0;
// Find all the expired full backups
if (fullRetention > 0)
{
// Get list of current full backups (default order is oldest to newest)
StringList *currentBackupList = infoBackupDataLabelList(infoBackup, backupRegExpP(.full = true));
// If there are more full backups then the number to retain, then expire the oldest ones
if (strLstSize(currentBackupList) > fullRetention)
{
// Expire all backups that depend on the full backup
for (unsigned int fullIdx = 0; fullIdx < strLstSize(currentBackupList) - fullRetention; fullIdx++)
{
// The list of backups to remove includes the full backup and the default sort order will put it first
StringList *removeList = infoBackupDataLabelList(
infoBackup, strNewFmt("^%s.*", strPtr(strLstGet(currentBackupList, fullIdx))));
// Initialize the log message
String *backupExpired = strNew("");
// Remove the manifest files in each directory and remove the backup from the current section of backup.info
for (unsigned int rmvIdx = 0; rmvIdx < strLstSize(removeList); rmvIdx++)
{
String *removeBackupLabel = strLstGet(removeList, rmvIdx);
expireBackup(infoBackup, removeBackupLabel, backupExpired);
result++;
}
// If the message contains a comma, then prepend "set:"
LOG_INFO("expire full backup %s%s", (strChr(backupExpired, ',') != -1 ? "set: " : ""), strPtr(backupExpired));
}
}
}
}
MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN(UINT, result);
}
/***********************************************************************************************************************************
Log detailed information about archive logs removed
***********************************************************************************************************************************/
static void
logExpire(ArchiveExpired *archiveExpire, String *archiveId)
{
if (archiveExpire->start != NULL)
{
// Force out any remaining message
LOG_DETAIL(
"remove archive: archiveId = %s, start = %s, stop = %s", strPtr(archiveId), strPtr(archiveExpire->start),
strPtr(archiveExpire->stop));
archiveExpire->start = NULL;
}
}
/***********************************************************************************************************************************
Process archive retention
***********************************************************************************************************************************/
static void
removeExpiredArchive(InfoBackup *infoBackup)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(INFO_BACKUP, infoBackup);
FUNCTION_LOG_END();
ASSERT(infoBackup != NULL);
MEM_CONTEXT_TEMP_BEGIN()
{
// Get the retention options. repo-archive-retention-type always has a value as it defaults to "full"
const String *archiveRetentionType = cfgOptionStr(cfgOptRepoRetentionArchiveType);
unsigned int archiveRetention = cfgOptionTest(cfgOptRepoRetentionArchive) ? cfgOptionUInt(cfgOptRepoRetentionArchive) : 0;
// If archive retention is undefined, then ignore archiving. The user does not have to set this - it will be defaulted in
// cfgLoadUpdateOption based on certain rules.
if (archiveRetention == 0)
{
LOG_INFO("option '%s' is not set - archive logs will not be expired", cfgOptionName(cfgOptRepoRetentionArchive));
}
else
{
// Determine which backup type to use for archive retention (full, differential, incremental) and get a list of the
// remaining non-expired backups, from newest to oldest, based on the type.
StringList *globalBackupRetentionList = NULL;
if (strCmp(archiveRetentionType, STRDEF(CFGOPTVAL_TMP_REPO_RETENTION_ARCHIVE_TYPE_FULL)) == 0)
{
globalBackupRetentionList = strLstSort(
infoBackupDataLabelList(infoBackup, backupRegExpP(.full = true)), sortOrderDesc);
}
else if (strCmp(archiveRetentionType, STRDEF(CFGOPTVAL_TMP_REPO_RETENTION_ARCHIVE_TYPE_DIFF)) == 0)
{
globalBackupRetentionList = strLstSort(
infoBackupDataLabelList(infoBackup, backupRegExpP(.full = true, .differential = true)), sortOrderDesc);
}
else
{ // Incrementals can depend on Full or Diff so get a list of all incrementals
globalBackupRetentionList = strLstSort(
infoBackupDataLabelList(infoBackup, backupRegExpP(.full = true, .differential = true, .incremental = true)),
sortOrderDesc);
}
// Expire archives. If no backups were found then preserve current archive logs - too soon to expire them.
if (strLstSize(globalBackupRetentionList) > 0)
{
// Attempt to load the archive info file
InfoArchive *infoArchive = infoArchiveLoadFile(
storageRepo(), INFO_ARCHIVE_PATH_FILE_STR, cipherType(cfgOptionStr(cfgOptRepoCipherType)),
cfgOptionStr(cfgOptRepoCipherPass));
InfoPg *infoArchivePgData = infoArchivePg(infoArchive);
// Get a list of archive directories (e.g. 9.4-1, 10-2, etc) sorted by the db-id (number after the dash).
StringList *listArchiveDisk = sortArchiveId(
storageListP(storageRepo(), STRDEF(STORAGE_REPO_ARCHIVE), .expression = STRDEF(REGEX_ARCHIVE_DIR_DB_VERSION)),
sortOrderAsc);
StringList *globalBackupArchiveRetentionList = strLstNew();
// globalBackupRetentionList is ordered newest to oldest backup, so create globalBackupArchiveRetentionList of the
// newest backups whose archives will be retained
for (unsigned int idx = 0;
idx < (archiveRetention < strLstSize(globalBackupRetentionList) ?
archiveRetention : strLstSize(globalBackupRetentionList));
idx++)
{
strLstAdd(globalBackupArchiveRetentionList, strLstGet(globalBackupRetentionList, idx));
}
// From newest to oldest, confirm the pgVersion and pgSystemId from the archive.info history id match that of the
// same history id of the backup.info and if not, there is a mismatch between the info files so do not continue.
// NOTE: If the archive.info file was reconstructed and contains history 4: version 10, sys-id 456 and history 3:
// version 9.6, sys-id 123, but backup.info contains history 4 and 3 (identical to archive.info) but also 2 and 1
// then an error will not be thrown since only archive 4 and 3 would have existed and the others expired so
// nothing really to do.
for (unsigned int infoPgIdx = 0; infoPgIdx < infoPgDataTotal(infoArchivePgData); infoPgIdx++)
{
InfoPgData archiveInfoPgHistory = infoPgData(infoArchivePgData, infoPgIdx);
InfoPgData backupInfoPgHistory = infoPgData(infoBackupPg(infoBackup), infoPgIdx);
if (archiveInfoPgHistory.id != backupInfoPgHistory.id ||
archiveInfoPgHistory.systemId != backupInfoPgHistory.systemId ||
archiveInfoPgHistory.version != backupInfoPgHistory.version)
{
THROW(FormatError, "archive expiration cannot continue - archive and backup history lists do not match");
}
}
// Loop through the archive.info history from oldest to newest and if there is a corresponding directory on disk
// then remove WAL that are not part of retention as long as the db:history id verion/system-id matches backup.info
for (unsigned int pgIdx = infoPgDataTotal(infoArchivePgData) - 1; (int)pgIdx >= 0; pgIdx--)
{
String *archiveId = infoPgArchiveId(infoArchivePgData, pgIdx);
StringList *localBackupRetentionList = strLstNew();
// Initialize the expired archive information for this archive ID
ArchiveExpired archiveExpire = {.total = 0, .start = NULL, .stop = NULL};
for (unsigned int archiveIdx = 0; archiveIdx < strLstSize(listArchiveDisk); archiveIdx++)
{
// Is there an archive directory for this archiveId? If not, move on to the next.
if (strCmp(archiveId, strLstGet(listArchiveDisk, archiveIdx)) != 0)
continue;
StringList *archiveSplit = strLstNewSplitZ(archiveId, "-");
unsigned int archivePgId = cvtZToUInt(strPtr(strLstGet(archiveSplit, 1)));
// From the global list of backups to retain, create a list of backups, oldest to newest, associated with
// this archiveId (e.g. 9.4-1), e.g. If globalBackupRetention has 4F, 3F, 2F, 1F then
// localBackupRetentionList will have 1F, 2F, 3F, 4F (assuming they all have same history id)
for (unsigned int retentionIdx = strLstSize(globalBackupRetentionList) - 1;
(int)retentionIdx >=0; retentionIdx--)
{
for (unsigned int backupIdx = 0; backupIdx < infoBackupDataTotal(infoBackup); backupIdx++)
{
InfoBackupData backupData = infoBackupData(infoBackup, backupIdx);
if (strCmp(backupData.backupLabel, strLstGet(globalBackupRetentionList, retentionIdx)) == 0 &&
backupData.backupPgId == archivePgId)
{
strLstAdd(localBackupRetentionList, backupData.backupLabel);
}
}
}
// If no backup to retain was found
if (strLstSize(localBackupRetentionList) == 0)
{
// If this is not the current database, then delete the archive directory else do nothing since the
// current DB archive directory must not be deleted
InfoPgData currentPg = infoPgDataCurrent(infoArchivePgData);
if (currentPg.id != archivePgId)
{
String *fullPath = storagePath(
storageRepo(), strNewFmt(STORAGE_REPO_ARCHIVE "/%s", strPtr(archiveId)));
storagePathRemoveP(storageRepoWrite(), fullPath, .recurse = true);
LOG_INFO("remove archive path: %s", strPtr(fullPath));
}
// Continue to next directory
break;
}
// If we get here, then a local backup was found for retention
StringList *localBackupArchiveRetentionList = strLstNew();
// If the archive retention is less than or equal to the number of all backups, then perform selective
// expiration
if (archiveRetention <= strLstSize(globalBackupRetentionList))
{
// From the full list of backups in archive retention, find the intersection of local backups to retain
// from oldest to newest
for (unsigned int globalIdx = strLstSize(globalBackupArchiveRetentionList) - 1;
(int)globalIdx >= 0; globalIdx--)
{
for (unsigned int localIdx = 0; localIdx < strLstSize(localBackupRetentionList); localIdx++)
{
if (strCmp(
strLstGet(globalBackupArchiveRetentionList, globalIdx),
strLstGet(localBackupRetentionList, localIdx)) == 0)
{
strLstAdd(localBackupArchiveRetentionList, strLstGet(localBackupRetentionList, localIdx));
}
}
}
}
// Else if there are not enough backups yet globally to start archive expiration then set the archive
// retention to the oldest backup so anything prior to that will be removed as it is not needed but
// everything else is. This is incase there are old archives left around so that they don't stay around
// forever.
else
{
LOG_INFO(
"full backup total < %u - using oldest full backup for %s archive retention", archiveRetention,
strPtr(archiveId));
strLstAdd(localBackupArchiveRetentionList, strLstGet(localBackupRetentionList, 0));
}
// If no local backups were found as part of retention then set the backup archive retention to the newest
// backup so that the database is fully recoverable (can be recovered from the last backup through pitr)
if (strLstSize(localBackupArchiveRetentionList) == 0)
{
strLstAdd(
localBackupArchiveRetentionList,
strLstGet(localBackupRetentionList, strLstSize(localBackupRetentionList) - 1));
}
// Get the data for the backup selected for retention and all backups associated with this archive id
List *archiveIdBackupList = lstNew(sizeof(InfoBackupData));
InfoBackupData archiveRetentionBackup = {0};
for (unsigned int infoBackupIdx = 0; infoBackupIdx < infoBackupDataTotal(infoBackup); infoBackupIdx++)
{
InfoBackupData archiveIdBackup = infoBackupData(infoBackup, infoBackupIdx);
// If this is the backup selected for retention, store its data
if (strCmp(archiveIdBackup.backupLabel, strLstGet(localBackupArchiveRetentionList, 0)) == 0)
archiveRetentionBackup = infoBackupData(infoBackup, infoBackupIdx);
// If this is a backup associated with this archive Id, then add it to the list to check
if (archiveIdBackup.backupPgId == archivePgId)
lstAdd(archiveIdBackupList, &archiveIdBackup);
}
// Only expire if the selected backup has archive data - backups performed with --no-online will
// not have archive data and cannot be used for expiration.
bool removeArchive = false;
if (archiveRetentionBackup.backupArchiveStart != NULL)
{
// Get archive ranges to preserve. Because archive retention can be less than total retention it is
// important to preserve archive that is required to make the older backups consistent even though they
// cannot be played any further forward with PITR.
String *archiveExpireMax = NULL;
List *archiveRangeList = lstNew(sizeof(ArchiveRange));
// From the full list of backups, loop through those associated with this archiveId
for (unsigned int backupListIdx = 0; backupListIdx < lstSize(archiveIdBackupList); backupListIdx++)
{
InfoBackupData *backupData = lstGet(archiveIdBackupList, backupListIdx);
// If the backup is earlier than or the same as the retention backup and the backup has an
// archive start
if (strCmp(backupData->backupLabel, archiveRetentionBackup.backupLabel) <= 0 &&
backupData->backupArchiveStart != NULL)
{
ArchiveRange archiveRange =
{
.start = strDup(backupData->backupArchiveStart),
.stop = NULL,
};
// If this is not the retention backup, then set the stop, otherwise set the expire max to
// the archive start of the archive to retain
if (strCmp(backupData->backupLabel, archiveRetentionBackup.backupLabel) != 0)
archiveRange.stop = strDup(backupData->backupArchiveStop);
else
archiveExpireMax = strDup(archiveRange.start);
LOG_DETAIL(
"archive retention on backup %s, archiveId = %s, start = %s%s",
strPtr(backupData->backupLabel), strPtr(archiveId), strPtr(archiveRange.start),
archiveRange.stop != NULL ?
strPtr(strNewFmt(", stop = %s", strPtr(archiveRange.stop))) : "");
// Add the archive range to the list
lstAdd(archiveRangeList, &archiveRange);
}
}
// Get all major archive paths (timeline and first 32 bits of LSN)
StringList *walPathList =
strLstSort(
storageListP(
storageRepo(), strNewFmt(STORAGE_REPO_ARCHIVE "/%s", strPtr(archiveId)),
.expression = STRDEF(WAL_SEGMENT_DIR_REGEXP)),
sortOrderAsc);
for (unsigned int walIdx = 0; walIdx < strLstSize(walPathList); walIdx++)
{
String *walPath = strLstGet(walPathList, walIdx);
removeArchive = true;
// Keep the path if it falls in the range of any backup in retention
for (unsigned int rangeIdx = 0; rangeIdx < lstSize(archiveRangeList); rangeIdx++)
{
ArchiveRange *archiveRange = lstGet(archiveRangeList, rangeIdx);
if (strCmp(walPath, strSubN(archiveRange->start, 0, 16)) >= 0 &&
(archiveRange->stop == NULL || strCmp(walPath, strSubN(archiveRange->stop, 0, 16)) <= 0))
{
removeArchive = false;
break;
}
}
// Remove the entire directory if all archive is expired
if (removeArchive)
{
storagePathRemoveP(
storageRepoWrite(), strNewFmt(STORAGE_REPO_ARCHIVE "/%s/%s", strPtr(archiveId),
strPtr(walPath)), .recurse = true);
archiveExpire.total++;
archiveExpire.start = strDup(walPath);
archiveExpire.stop = strDup(walPath);
}
// Else delete individual files instead if the major path is less than or equal to the most recent
// retention backup. This optimization prevents scanning though major paths that could not possibly
// have anything to expire.
else if (strCmp(walPath, strSubN(archiveExpireMax, 0, 16)) <= 0)
{
// Look for files in the archive directory
StringList *walSubPathList =
strLstSort(
storageListP(
storageRepo(),
strNewFmt(STORAGE_REPO_ARCHIVE "/%s/%s", strPtr(archiveId), strPtr(walPath)),
.expression = STRDEF("^[0-F]{24}.*$")),
sortOrderAsc);
for (unsigned int subIdx = 0; subIdx < strLstSize(walSubPathList); subIdx++)
{
removeArchive = true;
String *walSubPath = strLstGet(walSubPathList, subIdx);
// Determine if the individual archive log is used in a backup
for (unsigned int rangeIdx = 0; rangeIdx < lstSize(archiveRangeList); rangeIdx++)
{
ArchiveRange *archiveRange = lstGet(archiveRangeList, rangeIdx);
if (strCmp(strSubN(walSubPath, 0, 24), archiveRange->start) >= 0 &&
(archiveRange->stop == NULL ||
strCmp(strSubN(walSubPath, 0, 24), archiveRange->stop) <= 0))
{
removeArchive = false;
break;
}
}
// Remove archive log if it is not used in a backup
if (removeArchive)
{
storageRemoveNP(
storageRepoWrite(),
strNewFmt(STORAGE_REPO_ARCHIVE "/%s/%s/%s",
strPtr(archiveId), strPtr(walPath), strPtr(walSubPath)));
// Track that this archive was removed
archiveExpire.total++;
archiveExpire.stop = strDup(strSubN(walSubPath, 0, 24));
if (archiveExpire.start == NULL)
archiveExpire.start = strDup(strSubN(walSubPath, 0, 24));
}
else
logExpire(&archiveExpire, archiveId);
}
}
}
// Log if no archive was expired
if (archiveExpire.total == 0)
{
LOG_DETAIL("no archive to remove, archiveId = %s", strPtr(archiveId));
}
// Log if there is more to log
else
logExpire(&archiveExpire, archiveId);
}
}
}
}
}
}
MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Remove expired backups from repo
***********************************************************************************************************************************/
static void
removeExpiredBackup(InfoBackup *infoBackup)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(INFO_BACKUP, infoBackup);
FUNCTION_LOG_END();
ASSERT(infoBackup != NULL);
// Get all the current backups in backup.info
StringList *currentBackupList = strLstSort(infoBackupDataLabelList(infoBackup, NULL), sortOrderDesc);
StringList *backupList = strLstSort(
storageListP(
storageRepo(), STRDEF(STORAGE_REPO_BACKUP), .expression = backupRegExpP(.full = true, .differential = true,
.incremental = true)),
sortOrderDesc);
// Remove non-current backups from disk
for (unsigned int backupIdx = 0; backupIdx < strLstSize(backupList); backupIdx++)
{
if (!strLstExists(currentBackupList, strLstGet(backupList, backupIdx)))
{
LOG_INFO("remove expired backup %s", strPtr(strLstGet(backupList, backupIdx)));
storagePathRemoveP(
storageRepoWrite(), strNewFmt(STORAGE_REPO_BACKUP "/%s", strPtr(strLstGet(backupList, backupIdx))),
.recurse = true);
}
}
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Expire backups and archives
***********************************************************************************************************************************/
void
cmdExpire(void)
{
FUNCTION_LOG_VOID(logLevelDebug);
MEM_CONTEXT_TEMP_BEGIN()
{
// Get the repo storage in case it is remote and encryption settings need to be pulled down
storageRepo();
// Load the backup.info
InfoBackup *infoBackup = infoBackupLoadFile(
storageRepo(), INFO_BACKUP_PATH_FILE_STR, cipherType(cfgOptionStr(cfgOptRepoCipherType)),
cfgOptionStr(cfgOptRepoCipherPass));
expireFullBackup(infoBackup);
expireDiffBackup(infoBackup);
infoBackupSaveFile(
infoBackup, storageRepoWrite(), INFO_BACKUP_PATH_FILE_STR, cipherType(cfgOptionStr(cfgOptRepoCipherType)),
cfgOptionStr(cfgOptRepoCipherPass));
removeExpiredBackup(infoBackup);
removeExpiredArchive(infoBackup);
}
MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN_VOID();
}