Files
pgbackrest/lib/pgBackRest/Backup/Common.pm
T

250 lines
8.1 KiB
Perl
Raw Normal View History

2015-06-13 18:25:49 -04:00
####################################################################################################################################
# BACKUP COMMON MODULE
####################################################################################################################################
2017-05-15 16:01:00 -04:00
package pgBackRest::Backup::Common;
2015-06-13 18:25:49 -04:00
use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use Exporter qw(import);
our @EXPORT = qw();
2015-06-13 18:25:49 -04:00
use File::Basename;
use pgBackRest::Common::Log;
use pgBackRest::Common::String;
2017-04-05 10:16:16 -04:00
use pgBackRest::Common::Wait;
use pgBackRest::Config::Config;
2017-06-09 17:51:41 -04:00
use pgBackRest::Protocol::Storage::Helper;
use pgBackRest::Storage::Helper;
2017-04-05 10:16:16 -04:00
use pgBackRest::Manifest;
2016-12-03 10:23:26 -05:00
####################################################################################################################################
# Latest backup link constant
####################################################################################################################################
use constant LINK_LATEST => OPTION_DEFAULT_RESTORE_SET;
push @EXPORT, qw(LINK_LATEST);
2015-06-13 18:25:49 -04:00
####################################################################################################################################
# backupRegExpGet
#
# Generate a regexp depending on the backups that need to be found.
2015-06-13 18:25:49 -04:00
####################################################################################################################################
sub backupRegExpGet
{
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$bFull,
$bDifferential,
2016-04-14 22:50:02 -04:00
$bIncremental,
$bAnchor
) =
logDebugParam
(
__PACKAGE__ . '::backupRegExpGet', \@_,
{name => 'bFull', default => false},
{name => 'bDifferential', default => false},
2016-04-14 22:50:02 -04:00
{name => 'bIncremental', default => false},
{name => 'bAnchor', default => true}
);
2015-06-13 18:25:49 -04:00
# One of the types must be selected
if (!($bFull || $bDifferential || $bIncremental))
2015-06-13 18:25:49 -04:00
{
confess &log(ASSERT, 'at least one backup type must be selected');
2015-06-13 18:25:49 -04:00
}
# Standard regexp to match date and time formattting
2015-06-13 18:25:49 -04:00
my $strDateTimeRegExp = "[0-9]{8}\\-[0-9]{6}";
# Start the expression with the anchor if requested, date/time regexp and full backup indicator
my $strRegExp = ($bAnchor ? '^' : '') . $strDateTimeRegExp . 'F';
2015-06-13 18:25:49 -04:00
# Add the diff and/or incr expressions if requested
2015-06-13 18:25:49 -04:00
if ($bDifferential || $bIncremental)
{
# If full requested then diff/incr is optional
2015-06-13 18:25:49 -04:00
if ($bFull)
{
$strRegExp .= "(\\_";
}
# Else diff/incr is required
2015-06-13 18:25:49 -04:00
else
{
$strRegExp .= "\\_";
}
# Append date/time regexp for diff/incr
2015-06-13 18:25:49 -04:00
$strRegExp .= $strDateTimeRegExp;
# Filter on both diff/incr
2015-06-13 18:25:49 -04:00
if ($bDifferential && $bIncremental)
{
$strRegExp .= '(D|I)';
}
# Else just diff
2015-06-13 18:25:49 -04:00
elsif ($bDifferential)
{
$strRegExp .= 'D';
}
# Else just incr
2015-06-13 18:25:49 -04:00
else
{
$strRegExp .= 'I';
}
# If full requested then diff/incr is optional
2015-06-13 18:25:49 -04:00
if ($bFull)
{
$strRegExp .= '){0,1}';
}
}
# Append the end anchor if requested
2016-04-14 22:50:02 -04:00
$strRegExp .= $bAnchor ? "\$" : '';
2015-06-13 18:25:49 -04:00
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'strRegExp', value => $strRegExp}
);
2015-06-13 18:25:49 -04:00
}
push @EXPORT, qw(backupRegExpGet);
####################################################################################################################################
# backupLabelFormat
#
# Format the label for a backup.
####################################################################################################################################
sub backupLabelFormat
{
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strType,
$strBackupLabelLast,
2017-06-09 17:51:41 -04:00
$lTimestampStart
) =
logDebugParam
(
__PACKAGE__ . '::backupLabelFormat', \@_,
{name => 'strType', trace => true},
{name => 'strBackupLabelLast', required => false, trace => true},
2017-06-09 17:51:41 -04:00
{name => 'lTimestampTart', trace => true}
);
# Full backup label
my $strBackupLabel;
if ($strType eq BACKUP_TYPE_FULL)
{
# Last backup label must not be defined
if (defined($strBackupLabelLast))
{
confess &log(ASSERT, "strBackupLabelLast must not be defined when strType = '${strType}'");
}
# Format the timestamp and add the full indicator
2017-06-09 17:51:41 -04:00
$strBackupLabel = timestampFileFormat(undef, $lTimestampStart) . 'F';
}
# Else diff or incr label
else
{
# Last backup label must be defined
if (!defined($strBackupLabelLast))
{
confess &log(ASSERT, "strBackupLabelLast must be defined when strType = '${strType}'");
}
# Get the full backup portion of the last backup label
$strBackupLabel = substr($strBackupLabelLast, 0, 16);
# Format the timestamp
2017-06-09 17:51:41 -04:00
$strBackupLabel .= '_' . timestampFileFormat(undef, $lTimestampStart);
# Add the diff indicator
if ($strType eq BACKUP_TYPE_DIFF)
{
$strBackupLabel .= 'D';
}
# Else incr indicator
else
{
$strBackupLabel .= 'I';
}
}
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'strBackupLabel', value => $strBackupLabel, trace => true}
);
}
push @EXPORT, qw(backupLabelFormat);
2017-04-05 10:16:16 -04:00
####################################################################################################################################
# backupLabel
#
# Get unique backup label.
####################################################################################################################################
sub backupLabel
{
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
2017-06-09 17:51:41 -04:00
$oStorageRepo,
2017-04-05 10:16:16 -04:00
$strType,
$strBackupLabelLast,
2017-06-09 17:51:41 -04:00
$lTimestampStart
2017-04-05 10:16:16 -04:00
) =
logDebugParam
(
__PACKAGE__ . '::backupLabelFormat', \@_,
2017-06-09 17:51:41 -04:00
{name => 'oStorageRepo', trace => true},
2017-04-05 10:16:16 -04:00
{name => 'strType', trace => true},
{name => 'strBackupLabelLast', required => false, trace => true},
2017-06-09 17:51:41 -04:00
{name => 'lTimestampStart', trace => true}
2017-04-05 10:16:16 -04:00
);
# Create backup label
2017-06-09 17:51:41 -04:00
my $strBackupLabel = backupLabelFormat($strType, $strBackupLabelLast, $lTimestampStart);
2017-04-05 10:16:16 -04:00
# Make sure that the timestamp has not already been used by a prior backup. This is unlikely for online backups since there is
# already a wait after the manifest is built but it's still possible if the remote and local systems don't have synchronized
# clocks. In practice this is most useful for making offline testing faster since it allows the wait after manifest build to
# be skipped by dealing with any backup label collisions here.
2017-06-09 17:51:41 -04:00
if ($oStorageRepo->list(
STORAGE_REPO_BACKUP,
{strExpression =>
($strType eq BACKUP_TYPE_FULL ? '^' : '_') . timestampFileFormat(undef, $lTimestampStart) .
($strType eq BACKUP_TYPE_FULL ? 'F' : '(D|I)$')}) ||
$oStorageRepo->list(
STORAGE_REPO_BACKUP . qw{/} . PATH_BACKUP_HISTORY . '/' . timestampFormat('%4d', $lTimestampStart),
{strExpression =>
($strType eq BACKUP_TYPE_FULL ? '^' : '_') . timestampFileFormat(undef, $lTimestampStart) .
($strType eq BACKUP_TYPE_FULL ? 'F' : '(D|I)\.manifest\.' . COMPRESS_EXT . qw{$}),
bIgnoreMissing => true}))
2017-04-05 10:16:16 -04:00
{
waitRemainder();
$strBackupLabel = backupLabelFormat($strType, $strBackupLabelLast, time());
}
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'strBackupLabel', value => $strBackupLabel, trace => true}
);
}
push @EXPORT, qw(backupLabel);
2015-06-13 18:25:49 -04:00
1;