2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# EXPIRE MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Expire;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
|
|
|
use Exporter qw(import);
|
|
|
|
use File::Basename qw(dirname);
|
|
|
|
use Scalar::Util qw(looks_like_number);
|
|
|
|
|
2017-06-21 14:02:21 +02:00
|
|
|
use pgBackRest::Archive::Common;
|
|
|
|
use pgBackRest::Archive::Get::Get;
|
|
|
|
use pgBackRest::Archive::Info;
|
2017-06-09 23:51:41 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Ini;
|
|
|
|
use pgBackRest::Common::Log;
|
2017-05-15 22:01:00 +02:00
|
|
|
use pgBackRest::Backup::Common;
|
|
|
|
use pgBackRest::Backup::Info;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Config::Config;
|
2017-04-03 16:42:55 +02:00
|
|
|
use pgBackRest::InfoCommon;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Manifest;
|
2017-05-15 17:12:14 +02:00
|
|
|
use pgBackRest::Protocol::Helper;
|
2017-06-09 23:51:41 +02:00
|
|
|
use pgBackRest::Protocol::Storage::Helper;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# new
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
2016-08-11 23:32:28 +02:00
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '->new');
|
2015-10-08 17:43:56 +02:00
|
|
|
|
2016-01-21 00:24:44 +02:00
|
|
|
# Initialize total archive expired
|
|
|
|
$self->{iArchiveExpireTotal} = 0;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-21 00:24:44 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# logExpire
|
|
|
|
#
|
|
|
|
# Tracks which archive logs have been removed and provides log messages when needed.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub logExpire
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2017-04-07 01:36:37 +02:00
|
|
|
my $strArchiveId = shift;
|
2016-01-21 00:24:44 +02:00
|
|
|
my $strArchiveFile = shift;
|
|
|
|
|
|
|
|
if (defined($strArchiveFile))
|
|
|
|
{
|
|
|
|
if (!defined($self->{strArchiveExpireStart}))
|
|
|
|
{
|
|
|
|
$self->{strArchiveExpireStart} = $strArchiveFile;
|
|
|
|
$self->{strArchiveExpireStop} = $strArchiveFile;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$self->{strArchiveExpireStop} = $strArchiveFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{iArchiveExpireTotal}++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (defined($self->{strArchiveExpireStart}))
|
|
|
|
{
|
2017-04-07 01:36:37 +02:00
|
|
|
&log(DETAIL, "remove archive: archiveId = ${strArchiveId}, start = " . substr($self->{strArchiveExpireStart}, 0, 24) .
|
|
|
|
", stop = " . substr($self->{strArchiveExpireStop}, 0, 24));
|
2016-01-21 00:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
undef($self->{strArchiveExpireStart});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# process
|
|
|
|
#
|
|
|
|
# Removes expired backups and archive logs from the backup directory. Partial backups are not counted for expiration, so if full
|
|
|
|
# or differential retention is set to 2, there must be three complete backups before the oldest one can be deleted.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub process
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
2016-08-11 23:32:28 +02:00
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '->process');
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
my @stryPath;
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
my $oStorageRepo = storageRepo();
|
|
|
|
my $strBackupClusterPath = $oStorageRepo->pathGet(STORAGE_REPO_BACKUP);
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iFullRetention = cfgOption(CFGOPT_RETENTION_FULL, false);
|
|
|
|
my $iDifferentialRetention = cfgOption(CFGOPT_RETENTION_DIFF, false);
|
|
|
|
my $strArchiveRetentionType = cfgOption(CFGOPT_RETENTION_ARCHIVE_TYPE, false);
|
|
|
|
my $iArchiveRetention = cfgOption(CFGOPT_RETENTION_ARCHIVE, false);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-12-20 23:52:20 +02:00
|
|
|
# Load the backup.info
|
2017-06-09 23:51:41 +02:00
|
|
|
my $oBackupInfo = new pgBackRest::Backup::Info($oStorageRepo->pathGet(STORAGE_REPO_BACKUP));
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Find all the expired full backups
|
|
|
|
if (defined($iFullRetention))
|
|
|
|
{
|
|
|
|
# Make sure iFullRetention is valid
|
|
|
|
if (!looks_like_number($iFullRetention) || $iFullRetention < 1)
|
|
|
|
{
|
2015-11-22 21:02:14 +02:00
|
|
|
confess &log(ERROR, 'retention-full must be a number >= 1');
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-01-19 14:39:29 +02:00
|
|
|
@stryPath = $oBackupInfo->list(backupRegExpGet(true));
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-11-22 21:02:14 +02:00
|
|
|
if (@stryPath > $iFullRetention)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-01-19 14:39:29 +02:00
|
|
|
# Expire all backups that depend on the full backup
|
2015-11-22 21:02:14 +02:00
|
|
|
for (my $iFullIdx = 0; $iFullIdx < @stryPath - $iFullRetention; $iFullIdx++)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2015-11-22 21:02:14 +02:00
|
|
|
my @stryRemoveList;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-02-23 16:25:22 +02:00
|
|
|
foreach my $strPath ($oBackupInfo->list('^' . $stryPath[$iFullIdx] . '.*'))
|
2015-11-22 21:02:14 +02:00
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
$oStorageRepo->remove(STORAGE_REPO_BACKUP . "/${strPath}/" . FILE_MANIFEST . INI_COPY_EXT);
|
|
|
|
$oStorageRepo->remove(STORAGE_REPO_BACKUP . "/${strPath}/" . FILE_MANIFEST);
|
2015-11-22 21:02:14 +02:00
|
|
|
$oBackupInfo->delete($strPath);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-11-22 21:02:14 +02:00
|
|
|
if ($strPath ne $stryPath[$iFullIdx])
|
|
|
|
{
|
|
|
|
push(@stryRemoveList, $strPath);
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-01-19 14:39:29 +02:00
|
|
|
&log(INFO, 'expire full backup ' . (@stryRemoveList > 0 ? 'set: ' : '') . $stryPath[$iFullIdx] .
|
2015-11-22 21:02:14 +02:00
|
|
|
(@stryRemoveList > 0 ? ', ' . join(', ', @stryRemoveList) : ''));
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Find all the expired differential backups
|
|
|
|
if (defined($iDifferentialRetention))
|
|
|
|
{
|
|
|
|
# Make sure iDifferentialRetention is valid
|
|
|
|
if (!looks_like_number($iDifferentialRetention) || $iDifferentialRetention < 1)
|
|
|
|
{
|
2015-11-22 21:02:14 +02:00
|
|
|
confess &log(ERROR, 'retention-diff must be a number >= 1');
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 14:07:37 +02:00
|
|
|
# Get a list of full and differential backups. Full are considered differential for the purpose of retention.
|
|
|
|
# Example: F1, D1, D2, F2 and retention-diff=2, then F1,D2,F2 will be retained, not D2 and D1 as might be expected.
|
|
|
|
@stryPath = $oBackupInfo->list(backupRegExpGet(true, true));
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-11-22 21:02:14 +02:00
|
|
|
if (@stryPath > $iDifferentialRetention)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2015-11-22 21:02:14 +02:00
|
|
|
for (my $iDiffIdx = 0; $iDiffIdx < @stryPath - $iDifferentialRetention; $iDiffIdx++)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-09-07 14:07:37 +02:00
|
|
|
# Skip if this is a full backup. Full backups only count as differential when deciding which differential backups
|
|
|
|
# to expire.
|
|
|
|
next if ($stryPath[$iDiffIdx] =~ backupRegExpGet(true));
|
|
|
|
|
2015-11-22 21:02:14 +02:00
|
|
|
# Get a list of all differential and incremental backups
|
|
|
|
my @stryRemoveList;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-02-23 16:25:22 +02:00
|
|
|
foreach my $strPath ($oBackupInfo->list(backupRegExpGet(false, true, true)))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2015-11-22 21:02:14 +02:00
|
|
|
logDebugMisc($strOperation, "checking ${strPath} for differential expiration");
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-11-22 21:02:14 +02:00
|
|
|
# Remove all differential and incremental backups before the oldest valid differential
|
|
|
|
if ($strPath lt $stryPath[$iDiffIdx + 1])
|
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
$oStorageRepo->remove(STORAGE_REPO_BACKUP . "/${strPath}" . FILE_MANIFEST);
|
2015-11-22 21:02:14 +02:00
|
|
|
$oBackupInfo->delete($strPath);
|
|
|
|
|
|
|
|
if ($strPath ne $stryPath[$iDiffIdx])
|
|
|
|
{
|
|
|
|
push(@stryRemoveList, $strPath);
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2015-11-22 21:02:14 +02:00
|
|
|
|
2016-01-19 14:39:29 +02:00
|
|
|
&log(INFO, 'expire diff backup ' . (@stryRemoveList > 0 ? 'set: ' : '') . $stryPath[$iDiffIdx] .
|
2015-11-22 21:02:14 +02:00
|
|
|
(@stryRemoveList > 0 ? ', ' . join(', ', @stryRemoveList) : ''));
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:39:29 +02:00
|
|
|
$oBackupInfo->save();
|
|
|
|
|
|
|
|
# Remove backups from disk
|
2017-06-09 23:51:41 +02:00
|
|
|
foreach my $strBackup ($oStorageRepo->list(
|
|
|
|
STORAGE_REPO_BACKUP, {strExpression => backupRegExpGet(true, true, true), strSortOrder => 'reverse'}))
|
2016-01-19 14:39:29 +02:00
|
|
|
{
|
|
|
|
if (!$oBackupInfo->current($strBackup))
|
|
|
|
{
|
|
|
|
&log(INFO, "remove expired backup ${strBackup}");
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
$oStorageRepo->remove("${strBackupClusterPath}/${strBackup}", {bRecurse => true});
|
2016-01-19 14:39:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 14:07:37 +02:00
|
|
|
# If archive retention is still undefined, then ignore archiving
|
|
|
|
if (!defined($iArchiveRetention))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
&log(INFO, "option '" . cfgOptionName(CFGOPT_RETENTION_ARCHIVE) . "' is not set - archive logs will not be expired");
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
my @stryGlobalBackupRetention;
|
|
|
|
|
2016-09-07 14:07:37 +02:00
|
|
|
# Determine which backup type to use for archive retention (full, differential, incremental) and get a list of the
|
|
|
|
# remaining non-expired backups based on the type.
|
2017-08-25 22:47:47 +02:00
|
|
|
if ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_FULL)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
@stryGlobalBackupRetention = $oBackupInfo->list(backupRegExpGet(true), 'reverse');
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2017-08-25 22:47:47 +02:00
|
|
|
elsif ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_DIFF)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
@stryGlobalBackupRetention = $oBackupInfo->list(backupRegExpGet(true, true), 'reverse');
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2017-08-25 22:47:47 +02:00
|
|
|
elsif ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_INCR)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
@stryGlobalBackupRetention = $oBackupInfo->list(backupRegExpGet(true, true, true), 'reverse');
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 14:07:37 +02:00
|
|
|
# If no backups were found then preserve current archive logs - too soon to expire them
|
2017-04-03 16:42:55 +02:00
|
|
|
my $iBackupTotal = scalar @stryGlobalBackupRetention;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
if ($iBackupTotal > 0)
|
|
|
|
{
|
2017-06-21 14:02:21 +02:00
|
|
|
my $oArchiveInfo = new pgBackRest::Archive::Info($oStorageRepo->pathGet(STORAGE_REPO_ARCHIVE), true);
|
2017-06-09 23:51:41 +02:00
|
|
|
my @stryListArchiveDisk = $oStorageRepo->list(
|
|
|
|
STORAGE_REPO_ARCHIVE, {strExpression => REGEX_ARCHIVE_DIR_DB_VERSION, bIgnoreMissing => true});
|
2017-04-03 16:42:55 +02:00
|
|
|
|
|
|
|
# Make sure the current database versions match between the two files
|
|
|
|
if (!($oArchiveInfo->test(INFO_ARCHIVE_SECTION_DB, INFO_ARCHIVE_KEY_DB_VERSION, undef,
|
|
|
|
($oBackupInfo->get(INFO_BACKUP_SECTION_DB, INFO_BACKUP_KEY_DB_VERSION)))) ||
|
|
|
|
!($oArchiveInfo->test(INFO_ARCHIVE_SECTION_DB, INFO_ARCHIVE_KEY_DB_SYSTEM_ID, undef,
|
|
|
|
($oBackupInfo->get(INFO_BACKUP_SECTION_DB, INFO_BACKUP_KEY_SYSTEM_ID)))))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
confess &log(ERROR, "archive and backup database versions do not match\n" .
|
|
|
|
"HINT: has a stanza-upgrade been performed?", ERROR_FILE_INVALID);
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# Get the list of backups that are part of archive retention
|
|
|
|
my @stryTmp = @stryGlobalBackupRetention;
|
|
|
|
my @stryGlobalBackupArchiveRetention = splice(@stryTmp, 0, $iArchiveRetention);
|
|
|
|
|
|
|
|
# For each archiveId, remove WAL that are not part of retention
|
|
|
|
foreach my $strArchiveId (@stryListArchiveDisk)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
# 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)
|
|
|
|
my @stryLocalBackupRetention = $oBackupInfo->listByArchiveId($strArchiveId,
|
2017-06-09 23:51:41 +02:00
|
|
|
$oStorageRepo->pathGet(STORAGE_REPO_ARCHIVE), \@stryGlobalBackupRetention, 'reverse');
|
2015-09-02 01:05:10 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# If no backup to retain was found
|
|
|
|
if (!@stryLocalBackupRetention)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
# Get the backup db-id corresponding to this archiveId
|
2017-06-09 23:51:41 +02:00
|
|
|
my $iDbHistoryId = $oBackupInfo->backupArchiveDbHistoryId(
|
|
|
|
$strArchiveId, $oStorageRepo->pathGet(STORAGE_REPO_ARCHIVE));
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# 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
|
|
|
|
if (!defined($iDbHistoryId) || !$oBackupInfo->test(INFO_BACKUP_SECTION_DB, INFO_BACKUP_KEY_HISTORY_ID, undef,
|
|
|
|
$iDbHistoryId))
|
2016-01-21 00:24:44 +02:00
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
my $strFullPath = $oStorageRepo->pathGet(STORAGE_REPO_ARCHIVE . "/${strArchiveId}");
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
$oStorageRepo->remove($strFullPath, {bRecurse => true});
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-04-07 01:36:37 +02:00
|
|
|
&log(INFO, "remove archive path: ${strFullPath}");
|
2017-04-03 16:42:55 +02:00
|
|
|
}
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# Continue to next directory
|
|
|
|
next;
|
|
|
|
}
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
my @stryLocalBackupArchiveRentention;
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# If the archive retention is less than or equal to the number of all backups, then perform selective expiration
|
|
|
|
if (@stryGlobalBackupArchiveRetention && $iArchiveRetention <= scalar @stryGlobalBackupRetention)
|
|
|
|
{
|
|
|
|
# From the full list of backups in archive retention, find the intersection of local backups to retain
|
|
|
|
foreach my $strGlobalBackupArchiveRetention (@stryGlobalBackupArchiveRetention)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
foreach my $strLocalBackupRetention (@stryLocalBackupRetention)
|
2016-01-21 00:24:44 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
if ($strLocalBackupRetention eq $strGlobalBackupArchiveRetention)
|
2016-01-21 00:24:44 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
unshift(@stryLocalBackupArchiveRentention, $strLocalBackupRetention);
|
2016-01-21 00:24:44 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-03 16:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# 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
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
if ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_FULL && scalar @stryLocalBackupRetention > 0)
|
2017-04-03 16:42:55 +02:00
|
|
|
{
|
2017-04-07 01:36:37 +02:00
|
|
|
&log(INFO, "full backup total < ${iArchiveRetention} - using oldest full backup for ${strArchiveId} " .
|
|
|
|
"archive retention");
|
2017-04-03 16:42:55 +02:00
|
|
|
$stryLocalBackupArchiveRentention[0] = $stryLocalBackupRetention[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 (!@stryLocalBackupArchiveRentention)
|
|
|
|
{
|
|
|
|
$stryLocalBackupArchiveRentention[0] = $stryLocalBackupRetention[-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
my $strArchiveRetentionBackup = $stryLocalBackupArchiveRentention[0];
|
|
|
|
|
|
|
|
# If a backup has been selected for retention then continue
|
|
|
|
if (defined($strArchiveRetentionBackup))
|
|
|
|
{
|
|
|
|
my $bRemove;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# Only expire if the selected backup has archive info - backups performed with --no-online will
|
|
|
|
# not have archive info and cannot be used for expiration.
|
|
|
|
if ($oBackupInfo->test(INFO_BACKUP_SECTION_BACKUP_CURRENT,
|
|
|
|
$strArchiveRetentionBackup, INFO_BACKUP_KEY_ARCHIVE_START))
|
|
|
|
{
|
|
|
|
# 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.
|
|
|
|
my $strArchiveExpireMax;
|
|
|
|
my @oyArchiveRange;
|
|
|
|
my @stryBackupList = $oBackupInfo->list();
|
|
|
|
|
|
|
|
# With the full list of backups, loop through only those associated with this archiveId
|
2017-06-09 23:51:41 +02:00
|
|
|
foreach my $strBackup (
|
|
|
|
$oBackupInfo->listByArchiveId(
|
|
|
|
$strArchiveId, $oStorageRepo->pathGet(STORAGE_REPO_ARCHIVE), \@stryBackupList))
|
2016-01-16 06:37:17 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
if ($strBackup le $strArchiveRetentionBackup &&
|
|
|
|
$oBackupInfo->test(INFO_BACKUP_SECTION_BACKUP_CURRENT, $strBackup, INFO_BACKUP_KEY_ARCHIVE_START))
|
|
|
|
{
|
|
|
|
my $oArchiveRange = {};
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
$$oArchiveRange{start} = $oBackupInfo->get(INFO_BACKUP_SECTION_BACKUP_CURRENT,
|
|
|
|
$strBackup, INFO_BACKUP_KEY_ARCHIVE_START);
|
2015-09-02 01:05:10 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
if ($strBackup ne $strArchiveRetentionBackup)
|
|
|
|
{
|
|
|
|
$$oArchiveRange{stop} = $oBackupInfo->get(INFO_BACKUP_SECTION_BACKUP_CURRENT,
|
|
|
|
$strBackup, INFO_BACKUP_KEY_ARCHIVE_STOP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strArchiveExpireMax = $$oArchiveRange{start};
|
|
|
|
}
|
|
|
|
|
|
|
|
&log(DETAIL, "archive retention on backup ${strBackup}, archiveId = ${strArchiveId}, " .
|
|
|
|
"start = $$oArchiveRange{start}" .
|
|
|
|
(defined($$oArchiveRange{stop}) ? ", stop = $$oArchiveRange{stop}" : ''));
|
|
|
|
|
|
|
|
push(@oyArchiveRange, $oArchiveRange);
|
|
|
|
}
|
2016-01-16 06:37:17 +02:00
|
|
|
}
|
2017-04-03 16:42:55 +02:00
|
|
|
|
|
|
|
# Get all major archive paths (timeline and first 32 bits of LSN)
|
2017-06-09 23:51:41 +02:00
|
|
|
foreach my $strPath ($oStorageRepo->list(
|
|
|
|
STORAGE_REPO_ARCHIVE . "/${strArchiveId}", {strExpression => REGEX_ARCHIVE_DIR_WAL}))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
logDebugMisc($strOperation, "found major WAL path: ${strPath}");
|
|
|
|
$bRemove = true;
|
|
|
|
|
|
|
|
# Keep the path if it falls in the range of any backup in retention
|
|
|
|
foreach my $oArchiveRange (@oyArchiveRange)
|
|
|
|
{
|
|
|
|
if ($strPath ge substr($$oArchiveRange{start}, 0, 16) &&
|
|
|
|
(!defined($$oArchiveRange{stop}) || $strPath le substr($$oArchiveRange{stop}, 0, 16)))
|
|
|
|
{
|
|
|
|
$bRemove = false;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove the entire directory if all archive is expired
|
|
|
|
if ($bRemove)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
my $strFullPath = $oStorageRepo->pathGet(STORAGE_REPO_ARCHIVE . "/${strArchiveId}") . "/${strPath}";
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
$oStorageRepo->remove($strFullPath, {bRecurse => true});
|
2017-04-03 16:42:55 +02:00
|
|
|
|
|
|
|
# Log expire info
|
|
|
|
logDebugMisc($strOperation, "remove major WAL path: ${strFullPath}");
|
2017-04-07 01:36:37 +02:00
|
|
|
$self->logExpire($strArchiveId, $strPath);
|
2017-04-03 16:42:55 +02:00
|
|
|
}
|
|
|
|
# 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.
|
|
|
|
elsif ($strPath le substr($strArchiveExpireMax, 0, 16))
|
|
|
|
{
|
|
|
|
# Look for files in the archive directory
|
2017-06-09 23:51:41 +02:00
|
|
|
foreach my $strSubPath ($oStorageRepo->list(
|
|
|
|
STORAGE_REPO_ARCHIVE . "/${strArchiveId}/${strPath}", {strExpression => "^[0-F]{24}.*\$"}))
|
2016-01-21 00:24:44 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
$bRemove = true;
|
|
|
|
|
|
|
|
# Determine if the individual archive log is used in a backup
|
|
|
|
foreach my $oArchiveRange (@oyArchiveRange)
|
2016-01-16 06:37:17 +02:00
|
|
|
{
|
2017-04-03 16:42:55 +02:00
|
|
|
if (substr($strSubPath, 0, 24) ge $$oArchiveRange{start} &&
|
|
|
|
(!defined($$oArchiveRange{stop}) || substr($strSubPath, 0, 24) le $$oArchiveRange{stop}))
|
|
|
|
{
|
|
|
|
$bRemove = false;
|
|
|
|
last;
|
|
|
|
}
|
2016-01-16 06:37:17 +02:00
|
|
|
}
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# Remove archive log if it is not used in a backup
|
|
|
|
if ($bRemove)
|
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
$oStorageRepo->remove(STORAGE_REPO_ARCHIVE . "/${strArchiveId}/${strSubPath}");
|
2016-01-21 00:24:44 +02:00
|
|
|
|
2017-04-07 01:36:37 +02:00
|
|
|
logDebugMisc($strOperation, "remove WAL segment: ${strArchiveId}/${strSubPath}");
|
2015-09-02 01:05:10 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# Log expire info
|
2017-04-07 01:36:37 +02:00
|
|
|
$self->logExpire($strArchiveId, substr($strSubPath, 0, 24));
|
2017-04-03 16:42:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Log that the file was not expired
|
2017-04-07 01:36:37 +02:00
|
|
|
$self->logExpire($strArchiveId);
|
2017-04-03 16:42:55 +02:00
|
|
|
}
|
2016-01-16 06:37:17 +02:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 01:05:10 +02:00
|
|
|
|
2017-04-03 16:42:55 +02:00
|
|
|
# Log if no archive was expired
|
|
|
|
if ($self->{iArchiveExpireTotal} == 0)
|
|
|
|
{
|
2017-04-07 01:36:37 +02:00
|
|
|
&log(DETAIL, "no archive to remove, archiveId = ${strArchiveId}");
|
2017-04-03 16:42:55 +02:00
|
|
|
}
|
2016-01-16 06:37:17 +02:00
|
|
|
}
|
2015-09-02 01:05:10 +02:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2016-08-11 23:32:28 +02:00
|
|
|
return logDebugReturn($strOperation);
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|