2014-12-18 18:42:54 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# RESTORE MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Restore;
|
2014-12-18 18:42:54 +02:00
|
|
|
|
|
|
|
use threads;
|
2014-12-22 18:24:32 +02:00
|
|
|
use threads::shared;
|
|
|
|
use Thread::Queue;
|
2014-12-18 18:42:54 +02:00
|
|
|
use strict;
|
2015-03-03 07:57:20 +02:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2014-12-18 18:42:54 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
use Cwd qw(abs_path);
|
2016-05-11 15:21:39 +02:00
|
|
|
use File::Basename qw(basename dirname);
|
2015-01-08 22:43:43 +02:00
|
|
|
use File::stat qw(lstat);
|
2014-12-18 18:42:54 +02:00
|
|
|
|
|
|
|
use lib dirname($0);
|
2016-04-15 04:50:02 +02:00
|
|
|
use pgBackRest::BackupInfo;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Config::Config;
|
2016-08-12 04:35:24 +02:00
|
|
|
use pgBackRest::DbVersion;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::File;
|
2016-04-15 04:50:02 +02:00
|
|
|
use pgBackRest::FileCommon;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Manifest;
|
|
|
|
use pgBackRest::RestoreFile;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2014-12-18 18:42:54 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# 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-07-26 22:21:14 +02:00
|
|
|
# Initialize protocol
|
|
|
|
$self->{oProtocol} = protocolGet();
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Initialize default file object
|
2016-04-14 15:30:54 +02:00
|
|
|
$self->{oFile} = new pgBackRest::File
|
2015-08-29 20:20:46 +02:00
|
|
|
(
|
2015-10-08 17:43:56 +02:00
|
|
|
optionGet(OPTION_STANZA),
|
2016-04-14 15:30:54 +02:00
|
|
|
optionGet(OPTION_REPO_PATH),
|
2015-10-08 17:43:56 +02:00
|
|
|
optionRemoteType(),
|
2016-07-26 22:21:14 +02:00
|
|
|
$self->{oProtocol}
|
2015-10-08 17:43:56 +02:00
|
|
|
);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2014-12-18 18:42:54 +02:00
|
|
|
# Initialize variables
|
2015-08-29 20:20:46 +02:00
|
|
|
$self->{strDbClusterPath} = optionGet(OPTION_DB_PATH);
|
|
|
|
$self->{strBackupSet} = optionGet(OPTION_SET);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
2014-12-18 18:42:54 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# DESTROY
|
|
|
|
####################################################################################################################################
|
|
|
|
sub DESTROY
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
2016-08-11 23:32:28 +02:00
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '->DESTROY');
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
undef($self->{oFile});
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2016-08-11 23:32:28 +02:00
|
|
|
return logDebugReturn($strOperation);
|
2015-10-08 17:43:56 +02:00
|
|
|
}
|
|
|
|
|
2014-12-30 18:59:57 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# manifestOwnershipCheck
|
2014-12-30 18:59:57 +02:00
|
|
|
#
|
|
|
|
# Checks the users and groups that exist in the manifest and emits warnings for ownership that cannot be set properly, either
|
|
|
|
# because the current user does not have permissions or because the user/group does not exist.
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub manifestOwnershipCheck
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
|
|
|
my $self = shift; # Class hash
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oManifest # Backup manifest
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->manifestOwnershipCheck', \@_,
|
2016-04-15 04:50:02 +02:00
|
|
|
{name => 'oManifest'}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-12-30 18:59:57 +02:00
|
|
|
|
|
|
|
# Create hashes to track valid/invalid users/groups
|
|
|
|
my %oOwnerHash = ();
|
|
|
|
|
2016-06-27 03:01:20 +02:00
|
|
|
# Create hash for each type to be checked
|
|
|
|
my %oFileTypeHash =
|
|
|
|
(
|
|
|
|
&MANIFEST_SECTION_TARGET_PATH => true,
|
|
|
|
&MANIFEST_SECTION_TARGET_LINK => true,
|
|
|
|
&MANIFEST_SECTION_TARGET_FILE => true
|
|
|
|
);
|
|
|
|
|
|
|
|
# Create hash for default owners (user, group) for when the owner in the manifest cannot be used because it does not exist or
|
|
|
|
# was not mapped to a name during the original backup. It's preferred to use the owner of the PGDATA directory but if that was
|
|
|
|
# not valid in the original backup then the current user/group will be used as a last resort.
|
|
|
|
my %oOwnerTypeHash;
|
|
|
|
|
|
|
|
if ($oManifest->test(MANIFEST_SECTION_TARGET_PATH, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_USER) &&
|
|
|
|
!$oManifest->boolTest(MANIFEST_SECTION_TARGET_PATH, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_USER, false))
|
|
|
|
{
|
|
|
|
$oOwnerTypeHash{&MANIFEST_SUBKEY_USER} =
|
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_PATH, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_USER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oOwnerTypeHash{&MANIFEST_SUBKEY_USER} = getpwuid($<);
|
|
|
|
}
|
2014-12-30 22:41:43 +02:00
|
|
|
|
2016-06-27 03:01:20 +02:00
|
|
|
if ($oManifest->test(MANIFEST_SECTION_TARGET_PATH, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_GROUP) &&
|
|
|
|
!$oManifest->boolTest(MANIFEST_SECTION_TARGET_PATH, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_GROUP, false))
|
|
|
|
{
|
|
|
|
$oOwnerTypeHash{&MANIFEST_SUBKEY_GROUP} =
|
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_PATH, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_GROUP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oOwnerTypeHash{&MANIFEST_SUBKEY_USER} = getgrgid($();
|
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
|
2014-12-31 18:20:46 +02:00
|
|
|
# Loop through owner types (user, group)
|
2014-12-30 22:41:43 +02:00
|
|
|
foreach my $strOwnerType (sort (keys %oOwnerTypeHash))
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# Loop through types (path, link, file)
|
|
|
|
foreach my $strSection (sort (keys %oFileTypeHash))
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
foreach my $strName ($oManifest->keys($strSection))
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strOwner = $oManifest->get($strSection, $strName, $strOwnerType);
|
2015-01-20 21:13:35 +02:00
|
|
|
|
2016-06-27 03:01:20 +02:00
|
|
|
# If the owner was invalid then set it to something valid
|
|
|
|
if ($oManifest->boolTest($strSection, $strName, $strOwnerType, false))
|
|
|
|
{
|
|
|
|
$strOwner = $oOwnerTypeHash{$strOwnerType};
|
|
|
|
|
|
|
|
&log(WARN, "backup ${strOwnerType} for ${strName} was not mapped to a name, set to ${strOwner}");
|
|
|
|
$oManifest->set($strSection, $strName, $strOwnerType, $strOwner);
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# If root then test to see if the user/group is valid
|
|
|
|
if ($< == 0)
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# If the owner has not been tested yet then test it
|
|
|
|
if (!defined($oOwnerHash{$strOwnerType}{$strOwner}))
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strOwnerId;
|
2014-12-31 19:28:16 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($strOwnerType eq 'user')
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
$strOwnerId = getpwnam($strOwner);
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
2014-12-31 19:28:16 +02:00
|
|
|
else
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
$strOwnerId = getgrnam($strOwner);
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
|
|
|
|
$oOwnerHash{$strOwnerType}{$strOwner} = defined($strOwnerId) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$oOwnerHash{$strOwnerType}{$strOwner})
|
|
|
|
{
|
|
|
|
$oManifest->set($strSection, $strName, $strOwnerType, $oOwnerTypeHash{$strOwnerType});
|
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
# Else set user/group to current user/group
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($strOwner ne $oOwnerTypeHash{$strOwnerType})
|
|
|
|
{
|
|
|
|
$oOwnerHash{$strOwnerType}{$strOwner} = false;
|
|
|
|
$oManifest->set($strSection, $strName, $strOwnerType, $oOwnerTypeHash{$strOwnerType});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 22:41:43 +02:00
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
|
2014-12-30 22:41:43 +02:00
|
|
|
# Output warning for any invalid owners
|
2014-12-31 19:28:16 +02:00
|
|
|
if (defined($oOwnerHash{$strOwnerType}))
|
2014-12-30 22:41:43 +02:00
|
|
|
{
|
2015-06-21 18:06:13 +02:00
|
|
|
foreach my $strOwner (sort (keys(%{$oOwnerHash{$strOwnerType}})))
|
2014-12-31 18:20:46 +02:00
|
|
|
{
|
2014-12-31 19:28:16 +02:00
|
|
|
if (!$oOwnerHash{$strOwnerType}{$strOwner})
|
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
&log(WARN, "${strOwnerType} ${strOwner} in manifest " . ($< == 0 ? 'does not exist locally ' : '') .
|
2016-06-27 03:01:20 +02:00
|
|
|
"cannot be used for restore, set to $oOwnerTypeHash{$strOwnerType}");
|
2014-12-31 19:28:16 +02:00
|
|
|
}
|
2014-12-31 18:20:46 +02:00
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 18:42:54 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# manifestLoad
|
2014-12-23 19:48:25 +02:00
|
|
|
#
|
2014-12-23 22:03:06 +02:00
|
|
|
# Loads the backup manifest and performs requested tablespace remaps.
|
2014-12-18 18:42:54 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub manifestLoad
|
2014-12-18 18:42:54 +02:00
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
my $self = shift; # Class hash
|
2014-12-23 19:48:25 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
2016-08-11 23:32:28 +02:00
|
|
|
my ($strOperation) = logDebugParam (__PACKAGE__ . '->manifestLoad');
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Error if the backup set does not exist
|
|
|
|
if (!$self->{oFile}->exists(PATH_BACKUP_CLUSTER, $self->{strBackupSet}))
|
2014-12-18 18:42:54 +02:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
confess &log(ERROR, 'backup ' . $self->{strBackupSet} . ' does not exist');
|
|
|
|
}
|
2014-12-18 18:42:54 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Copy the backup manifest to the db cluster path
|
2016-04-15 04:50:02 +02:00
|
|
|
$self->{oFile}->copy(PATH_BACKUP_CLUSTER, "$self->{strBackupSet}/" . FILE_MANIFEST,
|
2015-08-29 20:20:46 +02:00
|
|
|
PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_MANIFEST);
|
2014-12-24 01:52:38 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Load the manifest into a hash
|
2016-04-14 15:30:54 +02:00
|
|
|
my $oManifest = new pgBackRest::Manifest($self->{oFile}->pathGet(PATH_DB_ABSOLUTE,
|
2015-08-29 20:20:46 +02:00
|
|
|
$self->{strDbClusterPath} . '/' . FILE_MANIFEST));
|
2014-12-31 19:28:16 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# If backup is latest then set it equal to backup label, else verify that requested backup and label match
|
|
|
|
my $strBackupLabel = $oManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_LABEL);
|
2014-12-24 01:52:38 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($self->{strBackupSet} eq OPTION_DEFAULT_RESTORE_SET)
|
|
|
|
{
|
|
|
|
$self->{strBackupSet} = $strBackupLabel;
|
|
|
|
}
|
|
|
|
elsif ($self->{strBackupSet} ne $strBackupLabel)
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "request backup $self->{strBackupSet} and label ${strBackupLabel} do not match " .
|
|
|
|
' - this indicates some sort of corruption (at the very least paths have been renamed)');
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($self->{strDbClusterPath} ne $oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_PATH))
|
|
|
|
{
|
|
|
|
&log(INFO, 'remap $PGDATA directory to ' . $self->{strDbClusterPath});
|
|
|
|
|
|
|
|
$oManifest->set(MANIFEST_SECTION_BACKUP_TARGET, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_PATH, $self->{strDbClusterPath});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remap tablespaces when requested
|
|
|
|
my $oTablespaceRemap;
|
|
|
|
|
|
|
|
if (optionTest(OPTION_TABLESPACE_MAP))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $oTablespaceRemapRequest = optionGet(OPTION_TABLESPACE_MAP);
|
|
|
|
|
|
|
|
for my $strKey (sort(keys(%{$oTablespaceRemapRequest})))
|
|
|
|
{
|
|
|
|
my $bFound = false;
|
|
|
|
|
|
|
|
for my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))
|
|
|
|
{
|
|
|
|
if ($oManifest->test(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_TABLESPACE_ID, $strKey) ||
|
|
|
|
$oManifest->test(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_TABLESPACE_NAME, $strKey))
|
|
|
|
{
|
|
|
|
if (defined(${$oTablespaceRemap}{$strTarget}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "tablespace ${strKey} has already been remapped to ${$oTablespaceRemap}{$strTarget}",
|
|
|
|
ERROR_TABLESPACE_MAP);
|
|
|
|
}
|
|
|
|
|
|
|
|
${$oTablespaceRemap}{$strTarget} = ${$oTablespaceRemapRequest}{$strKey};
|
|
|
|
$bFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Error if the tablespace was not found to be remapped
|
|
|
|
if (!$bFound)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "cannot remap invalid tablespace ${strKey} to ${$oTablespaceRemapRequest}{$strKey}",
|
|
|
|
ERROR_TABLESPACE_MAP);
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2015-01-20 21:13:35 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Remap all tablespaces (except ones that were done individually above)
|
|
|
|
if (optionTest(OPTION_TABLESPACE_MAP_ALL))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
for my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))
|
2015-05-09 00:34:27 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($oManifest->isTargetTablespace($strTarget))
|
2015-05-09 00:34:27 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
if (!defined(${$oTablespaceRemap}{$strTarget}))
|
|
|
|
{
|
|
|
|
${$oTablespaceRemap}{$strTarget} = optionGet(OPTION_TABLESPACE_MAP_ALL) . '/' .
|
|
|
|
$oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_TABLESPACE_NAME);
|
|
|
|
}
|
2015-05-09 00:34:27 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
|
|
|
|
# Alter manifest for remapped tablespaces
|
|
|
|
if (defined($oTablespaceRemap))
|
|
|
|
{
|
|
|
|
foreach my $strTarget (sort(keys(%{$oTablespaceRemap})))
|
|
|
|
{
|
|
|
|
my $strRemapPath = ${$oTablespaceRemap}{$strTarget};
|
|
|
|
|
|
|
|
# Remap the tablespace in the manifest
|
|
|
|
&log(INFO, "remap tablespace ${strTarget} directory to ${strRemapPath}");
|
|
|
|
|
|
|
|
$oManifest->set(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_PATH, $strRemapPath);
|
|
|
|
$oManifest->set(MANIFEST_SECTION_TARGET_LINK, MANIFEST_TARGET_PGDATA . "/${strTarget}", MANIFEST_SUBKEY_DESTINATION,
|
|
|
|
$strRemapPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->manifestOwnershipCheck($oManifest);
|
|
|
|
|
|
|
|
# Remap links when requested
|
|
|
|
my $oLinkRemap;
|
|
|
|
|
|
|
|
if (optionTest(OPTION_LINK_MAP))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $oLinkRemapRequest = optionGet(OPTION_LINK_MAP);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
for my $strKey (sort(keys(%{$oLinkRemapRequest})))
|
2015-01-23 03:11:33 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strTarget = MANIFEST_TARGET_PGDATA . "/${strKey}";
|
|
|
|
|
|
|
|
# Only remap if a valid target link but not a tablespace
|
|
|
|
if ($oManifest->isTargetValid($strTarget, false) &&
|
|
|
|
$oManifest->isTargetLink($strTarget) && !$oManifest->isTargetTablespace($strTarget))
|
|
|
|
{
|
|
|
|
if (defined(${$oTablespaceRemap}{$strTarget}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "tablespace ${strKey} has already been remapped to ${$oLinkRemap}{$strTarget}",
|
|
|
|
ERROR_LINK_MAP);
|
|
|
|
}
|
|
|
|
|
|
|
|
${$oLinkRemap}{$strTarget} = ${$oLinkRemapRequest}{$strKey};
|
2015-01-23 03:11:33 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
&log(INFO, "remap link ${strTarget} destination to ${$oLinkRemap}{$strTarget}");
|
|
|
|
}
|
|
|
|
# Else error
|
|
|
|
else
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
confess &log(ERROR, "cannot remap invalid link ${strKey} to ${$oLinkRemapRequest}{$strKey}",
|
|
|
|
ERROR_LINK_MAP);
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 03:11:33 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Remap all links (except ones that were done individually above)
|
|
|
|
if (optionGet(OPTION_LINK_ALL))
|
|
|
|
{
|
|
|
|
for my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))
|
|
|
|
{
|
|
|
|
# If target is a link but not a tablespace and has not already been remapped when remap it
|
|
|
|
if ($oManifest->isTargetLink($strTarget) && !$oManifest->isTargetTablespace($strTarget) &&
|
|
|
|
!defined(${$oLinkRemap}{$strTarget}))
|
|
|
|
{
|
|
|
|
${$oLinkRemap}{$strTarget} =
|
|
|
|
$oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_PATH);
|
2015-01-23 03:11:33 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($oManifest->isTargetFile($strTarget))
|
|
|
|
{
|
|
|
|
${$oLinkRemap}{$strTarget} .= '/' .
|
|
|
|
$oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_FILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Alter manifest for remapped links
|
|
|
|
for my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))
|
|
|
|
{
|
|
|
|
if ($oManifest->isTargetLink($strTarget) && !$oManifest->isTargetTablespace($strTarget))
|
|
|
|
{
|
|
|
|
# If the link will be remapped
|
|
|
|
if (defined(${$oLinkRemap}{$strTarget}))
|
|
|
|
{
|
|
|
|
my $strTargetPath = ${$oLinkRemap}{$strTarget};
|
|
|
|
|
|
|
|
# If this link is to a file then the specified path must be split into file and path parts
|
|
|
|
if ($oManifest->isTargetFile($strTarget))
|
|
|
|
{
|
|
|
|
$strTargetPath = dirname($strTargetPath);
|
|
|
|
|
|
|
|
# Error when the path is not deep enough to be valid
|
|
|
|
if (!defined($strTargetPath))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "${$oLinkRemap}{$strTarget} is not long enough to be target for ${strTarget}");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set the file part
|
|
|
|
$oManifest->set(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_FILE,
|
|
|
|
substr(${$oLinkRemap}{$strTarget}, length($strTargetPath) + 1));
|
|
|
|
}
|
2015-01-23 03:11:33 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Set the target path
|
|
|
|
$oManifest->set(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_PATH, $strTargetPath);
|
|
|
|
|
|
|
|
# Since this will be a link remove the associated path (??? perhaps this should be in build like it is for ts?)
|
|
|
|
$oManifest->remove(MANIFEST_SECTION_TARGET_PATH, $strTarget);
|
|
|
|
}
|
|
|
|
# Else the link will be restored directly into $PGDATA instead
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($oManifest->test(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_FILE))
|
|
|
|
{
|
|
|
|
&log(WARN, 'file link ' . $oManifest->dbPathGet(undef, $strTarget) .
|
|
|
|
' will be restored as a file at the same location');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&log(WARN, 'contents of directory link ' . $oManifest->dbPathGet(undef, $strTarget) .
|
|
|
|
' will be restored in a directory at the same location');
|
|
|
|
}
|
|
|
|
|
|
|
|
$oManifest->remove(MANIFEST_SECTION_BACKUP_TARGET, $strTarget);
|
|
|
|
$oManifest->remove(MANIFEST_SECTION_TARGET_LINK, $strTarget);
|
|
|
|
}
|
2015-01-23 03:11:33 +02:00
|
|
|
}
|
2014-12-18 18:42:54 +02:00
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Make sure links are still valid after remapping
|
|
|
|
$oManifest->linkCheck();
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oManifest', value => $oManifest, trace => true}
|
|
|
|
);
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# clean
|
2014-12-23 22:03:06 +02:00
|
|
|
#
|
|
|
|
# Checks that the restore paths are empty, or if --force was used then it cleans files/paths/links from the restore directories that
|
|
|
|
# are not present in the manifest.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub clean
|
|
|
|
{
|
|
|
|
my $self = shift; # Class hash
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oManifest # Backup manifest
|
|
|
|
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->clean', \@_,
|
2016-04-15 04:50:02 +02:00
|
|
|
{name => 'oManifest'}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-01-02 21:15:15 +02:00
|
|
|
# Track if files/links/paths where removed
|
2016-04-15 04:50:02 +02:00
|
|
|
my %oRemoveHash =
|
|
|
|
(
|
|
|
|
&MANIFEST_SECTION_TARGET_FILE => 0,
|
|
|
|
&MANIFEST_SECTION_TARGET_PATH => 0,
|
|
|
|
&MANIFEST_SECTION_TARGET_LINK => 0
|
|
|
|
);
|
2015-01-02 21:15:15 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Check that all targets exist and are empty (unless --force or --delta specified)
|
|
|
|
my %oTargetFound;
|
|
|
|
my $bDelta = optionGet(OPTION_FORCE) || optionGet(OPTION_DELTA);
|
|
|
|
|
|
|
|
for my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
${$self->{oTargetPath}}{$strTarget} = $oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_PATH);
|
|
|
|
$oTargetFound{$strTarget} = false;
|
|
|
|
|
|
|
|
my $strCheckPath = ${$self->{oTargetPath}}{$strTarget};
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($oManifest->isTargetLink($strTarget) && index($strCheckPath, '/') != 0)
|
2016-02-02 21:33:15 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strBasePath = $oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_PATH);
|
|
|
|
my $strTargetPath = dirname($oManifest->dbPathGet(undef, $strTarget));
|
|
|
|
|
|
|
|
if ($strTargetPath ne '.')
|
|
|
|
{
|
|
|
|
$strBasePath .= "/${strTargetPath}";
|
|
|
|
}
|
|
|
|
|
|
|
|
${$self->{oTargetPath}}{$strTarget} = pathAbsolute($strBasePath, $strCheckPath);
|
|
|
|
|
|
|
|
$strCheckPath = ${$self->{oTargetPath}}{$strTarget};
|
|
|
|
|
|
|
|
if ($oManifest->isTargetTablespace($strTarget))
|
|
|
|
{
|
|
|
|
$strCheckPath = dirname(${$self->{oTargetPath}}{$strTarget});
|
|
|
|
}
|
2016-02-02 21:33:15 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
&log(DETAIL, "check ${strCheckPath} exists");
|
2014-12-21 17:11:17 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Check if the path exists
|
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, $strCheckPath))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
confess &log(ERROR, "cannot restore to missing path ${strCheckPath}", ERROR_PATH_MISSING);
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Check if the file exists
|
|
|
|
if ($oManifest->isTargetFile($strTarget))
|
|
|
|
{
|
|
|
|
# Construct the file to check
|
|
|
|
my $strCheckFile = "${strCheckPath}/" .
|
|
|
|
$oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, $strTarget, MANIFEST_SUBKEY_FILE);
|
|
|
|
|
|
|
|
# Check if the file exists
|
|
|
|
if ($self->{oFile}->exists(PATH_DB_ABSOLUTE, $strCheckFile))
|
|
|
|
{
|
|
|
|
# If the file exists and this is not a delta then error
|
|
|
|
if (!$bDelta)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "cannot restore file '${strCheckFile}' that already exists - " .
|
|
|
|
'try using --delta if this is what you intended', ERROR_RESTORE_PATH_NOT_EMPTY);
|
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Mark that a file was found
|
|
|
|
$oTargetFound{$strTarget} = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Check the directory for files
|
|
|
|
else
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# If this is a tablespace search for the special directory that Postgres puts in every tablespace directory
|
|
|
|
if ($oManifest->isTargetTablespace($strTarget))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# Construct the special tablespace path
|
|
|
|
${$self->{oTargetPath}}{$strTarget} = "${$self->{oTargetPath}}{$strTarget}/" . $oManifest->tablespacePathGet();
|
|
|
|
|
|
|
|
# If this path does not exist then skip the rest of the checking - the path will be created later
|
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, ${$self->{oTargetPath}}{$strTarget}))
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Build a manifest of the path to check for existing files
|
|
|
|
my %oTargetManifest;
|
|
|
|
$self->{oFile}->manifest(PATH_DB_ABSOLUTE, ${$self->{oTargetPath}}{$strTarget}, \%oTargetManifest);
|
|
|
|
|
|
|
|
for my $strName (keys(%{$oTargetManifest{name}}))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# Skip the root path and backup.manifest in the base path
|
|
|
|
if ($strName eq '.' ||
|
2016-05-14 16:33:12 +02:00
|
|
|
(($strName eq FILE_MANIFEST || $strName eq DB_FILE_RECOVERYCONF) && $strTarget eq MANIFEST_TARGET_PGDATA))
|
2016-04-15 04:50:02 +02:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# The presense of any other file will cause an error (unless --force or --delta specified)
|
|
|
|
if (!$bDelta)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "cannot restore to path '${$self->{oTargetPath}}{$strTarget}' that contains files - " .
|
|
|
|
'try using --delta if this is what you intended', ERROR_RESTORE_PATH_NOT_EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Mark that some files were found
|
|
|
|
$oTargetFound{$strTarget} = true;
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Clean up each target starting from the most nested
|
|
|
|
my %oFileChecked;
|
2014-12-21 17:11:17 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
for my $strTarget (sort {$b cmp $a} ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET)))
|
|
|
|
{
|
|
|
|
if ($oTargetFound{$strTarget})
|
|
|
|
{
|
|
|
|
&log(INFO, "remove invalid files/paths/links from ${$self->{oTargetPath}}{$strTarget}");
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# OK for the special tablespace path to not exist yet - it will be created later
|
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, ${$self->{oTargetPath}}{$strTarget}) &&
|
|
|
|
$oManifest->isTargetTablespace($strTarget))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
next;
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Load path manifest so it can be compared to deleted files/paths/links that are not in the backup
|
|
|
|
my %oTargetManifest;
|
|
|
|
$self->{oFile}->manifest(PATH_DB_ABSOLUTE, ${$self->{oTargetPath}}{$strTarget}, \%oTargetManifest);
|
2015-01-07 17:59:43 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# If the target is a file it doesn't matter whether it already exists or not.
|
|
|
|
if ($oManifest->isTargetFile($strTarget))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
next;
|
|
|
|
}
|
2014-12-21 17:11:17 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
foreach my $strName (sort {$b cmp $a} (keys(%{$oTargetManifest{name}})))
|
|
|
|
{
|
|
|
|
# Skip the root path
|
|
|
|
if ($strName eq '.' || ($strName eq FILE_MANIFEST && $strTarget eq MANIFEST_TARGET_PGDATA))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $strOsFile = "${$self->{oTargetPath}}{$strTarget}/${strName}";
|
|
|
|
my $strManifestFile = $oManifest->repoPathGet($strTarget, $strName);
|
|
|
|
|
|
|
|
# Determine the file/path/link type
|
|
|
|
my $strSection = MANIFEST_SECTION_TARGET_FILE;
|
2014-12-21 17:11:17 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($oTargetManifest{name}{$strName}{type} eq 'd')
|
|
|
|
{
|
|
|
|
$strSection = MANIFEST_SECTION_TARGET_PATH;
|
|
|
|
}
|
|
|
|
elsif ($oTargetManifest{name}{$strName}{type} eq 'l')
|
|
|
|
{
|
|
|
|
$strSection = MANIFEST_SECTION_TARGET_LINK;
|
2014-12-21 17:11:17 +02:00
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Check to see if the file/path/link exists in the manifest
|
|
|
|
if ($oManifest->test($strSection, $strManifestFile) &&
|
|
|
|
!defined($oFileChecked{$strSection}{$strManifestFile}))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strUser = $oManifest->get($strSection, $strManifestFile, MANIFEST_SUBKEY_USER);
|
|
|
|
my $strGroup = $oManifest->get($strSection, $strManifestFile, MANIFEST_SUBKEY_GROUP);
|
|
|
|
|
|
|
|
# If ownership does not match, fix it
|
2016-06-27 03:01:20 +02:00
|
|
|
if (!defined($oTargetManifest{name}{$strName}{user}) ||
|
|
|
|
$strUser ne $oTargetManifest{name}{$strName}{user} ||
|
|
|
|
!defined($oTargetManifest{name}{$strName}{group}) ||
|
2016-04-15 04:50:02 +02:00
|
|
|
$strGroup ne $oTargetManifest{name}{$strName}{group})
|
2015-01-07 17:59:43 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
&log(DETAIL, "set ownership ${strUser}:${strGroup} on ${strOsFile}");
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
$self->{oFile}->owner(PATH_DB_ABSOLUTE, $strOsFile, $strUser, $strGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
# If a link does not have the same destination, then delete it (it will be recreated later)
|
|
|
|
if ($strSection eq MANIFEST_SECTION_TARGET_LINK)
|
|
|
|
{
|
|
|
|
if ($oManifest->get($strSection, $strManifestFile, MANIFEST_SUBKEY_DESTINATION) ne
|
|
|
|
$oTargetManifest{name}{$strName}{link_destination})
|
|
|
|
{
|
|
|
|
&log(DETAIL, "remove link ${strOsFile} - destination changed");
|
|
|
|
fileRemove($strOsFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else if file/path mode does not match, fix it
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my $strMode = $oManifest->get($strSection, $strManifestFile, MANIFEST_SUBKEY_MODE);
|
|
|
|
|
|
|
|
if ($strMode ne $oTargetManifest{name}{$strName}{mode})
|
|
|
|
{
|
|
|
|
&log(DETAIL, "set mode ${strMode} on ${strOsFile}");
|
|
|
|
|
|
|
|
chmod(oct($strMode), $strOsFile)
|
|
|
|
or confess 'unable to set mode ${strMode} on ${strOsFile}';
|
|
|
|
}
|
2015-01-07 17:59:43 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
# If it does not then remove it
|
2015-01-07 17:59:43 +02:00
|
|
|
else
|
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# If a path then remove it, all the files should have already been deleted since we are going in reverse order
|
|
|
|
if ($strSection eq MANIFEST_SECTION_TARGET_PATH)
|
|
|
|
{
|
|
|
|
&log(DETAIL, "remove path ${strOsFile}");
|
|
|
|
rmdir($strOsFile) or confess &log(ERROR, "unable to delete path ${strOsFile}, is it empty?");
|
2015-01-07 17:59:43 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
$oRemoveHash{$strSection} += 1;
|
|
|
|
}
|
|
|
|
# Else delete a file/link
|
|
|
|
else
|
2015-01-07 17:59:43 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strType = (split('\:', $strSection))[1];
|
|
|
|
|
|
|
|
# Delete only if this is not the recovery.conf file. This is in case the user wants the recovery.conf file
|
|
|
|
# preserved. It will be written/deleted/preserved as needed in recovery().
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($oManifest->dbPathGet(undef, $strManifestFile) eq DB_FILE_RECOVERYCONF)
|
2016-04-15 04:50:02 +02:00
|
|
|
{
|
|
|
|
&log(DETAIL, "preserve ${strType} ${strOsFile}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&log(DETAIL, "remove ${strType} ${strOsFile}");
|
|
|
|
fileRemove($strOsFile);
|
2015-01-07 17:59:43 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
$oRemoveHash{$strSection} += 1;
|
|
|
|
}
|
2015-01-07 17:59:43 +02:00
|
|
|
}
|
2014-12-21 17:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Mark all files, paths, and links in this target as having been checked. This prevents them from being eligible for
|
|
|
|
# cleaning again in a higher level of the hierarchy.
|
|
|
|
foreach my $strSection (sort (keys %oRemoveHash))
|
|
|
|
{
|
|
|
|
for my $strManifestFile ($oManifest->keys($strSection))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# If a link exactly matches the target then it should be checked at a higher level of the hierarchy. Otherwise
|
|
|
|
# determine if the file/path/link should be marked as checked.
|
|
|
|
if ($strSection ne MANIFEST_SECTION_TARGET_LINK || $strManifestFile ne $strTarget)
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# If the file/path/link parent path matches the target then it should be marked as checked.
|
|
|
|
if (index($strManifestFile, "${strTarget}/") == 0)
|
2015-01-26 21:59:58 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# Mark the file/path/link as checked
|
|
|
|
$oFileChecked{$strSection}{$strManifestFile} = true;
|
2015-01-26 21:59:58 +02:00
|
|
|
}
|
2014-12-21 17:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
2015-01-02 21:15:15 +02:00
|
|
|
|
|
|
|
# Loop through types (path, link, file) and emit info if any were removed
|
2015-08-29 20:20:46 +02:00
|
|
|
my @stryMessage;
|
|
|
|
|
2015-01-02 21:15:15 +02:00
|
|
|
foreach my $strFileType (sort (keys %oRemoveHash))
|
|
|
|
{
|
|
|
|
if ($oRemoveHash{$strFileType} > 0)
|
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
push(@stryMessage, "$oRemoveHash{$strFileType} " . (split('\:', $strFileType))[1] .
|
|
|
|
($oRemoveHash{$strFileType} > 1 ? 's' : ''));
|
2015-01-02 21:15:15 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
if (@stryMessage)
|
|
|
|
{
|
|
|
|
&log(INFO, 'cleanup removed ' . join(', ', @stryMessage));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# build
|
2014-12-23 22:03:06 +02:00
|
|
|
#
|
2016-04-15 04:50:02 +02:00
|
|
|
# Creates missing paths and links and sets ownership/mode.
|
2014-12-23 22:03:06 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub build
|
|
|
|
{
|
2015-01-07 17:59:43 +02:00
|
|
|
my $self = shift; # Class hash
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oManifest # Backup manifest
|
|
|
|
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->build', \@_,
|
2016-04-15 04:50:02 +02:00
|
|
|
{name => 'oManifest'}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-12-23 22:03:06 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Create target paths (except for MANIFEST_TARGET_PGDATA because that must already exist)
|
|
|
|
foreach my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))
|
2014-12-23 22:03:06 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($strTarget ne MANIFEST_TARGET_PGDATA)
|
2016-02-02 21:33:15 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strPath = ${$self->{oTargetPath}}{$strTarget};
|
2016-02-02 21:33:15 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
if ($oManifest->isTargetTablespace($strTarget))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
$strPath = dirname($strPath);
|
2014-12-21 17:11:17 +02:00
|
|
|
}
|
|
|
|
|
2015-01-20 21:13:35 +02:00
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, $strPath))
|
2014-12-30 22:41:43 +02:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
$self->{oFile}->pathCreate(PATH_DB_ABSOLUTE, $strPath,
|
2016-04-15 04:50:02 +02:00
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_PATH, $strTarget, MANIFEST_SUBKEY_MODE));
|
|
|
|
|
|
|
|
# Set ownership (??? this could be done better inside the file functions)
|
|
|
|
my $strUser = $oManifest->get(MANIFEST_SECTION_TARGET_PATH, $strTarget, MANIFEST_SUBKEY_USER);
|
|
|
|
my $strGroup = $oManifest->get(MANIFEST_SECTION_TARGET_PATH, $strTarget, MANIFEST_SUBKEY_GROUP);
|
|
|
|
|
|
|
|
if ($strUser ne getpwuid($<) || $strGroup ne getgrgid($())
|
|
|
|
{
|
|
|
|
$self->{oFile}->owner(PATH_DB_ABSOLUTE, $strPath, $strUser, $strGroup);
|
|
|
|
}
|
2014-12-30 22:41:43 +02:00
|
|
|
}
|
2014-12-21 17:11:17 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
$oManifest->remove(MANIFEST_SECTION_TARGET_PATH, $strTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create paths and links by level. It's done this way so that all paths and links can be created within the hierarchy of
|
|
|
|
# $PGDATA without having to worry about where linked files and directories are pointed at.
|
|
|
|
my $iLevel = 1;
|
|
|
|
my $iFound;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
$iFound = 0;
|
|
|
|
|
|
|
|
&log(DEBUG, "build level ${iLevel} paths/links");
|
2015-01-20 21:13:35 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Iterate path/link sections
|
|
|
|
foreach my $strSection (&MANIFEST_SECTION_TARGET_PATH, &MANIFEST_SECTION_TARGET_LINK)
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# Iterate paths/links in the section
|
2015-01-23 03:11:33 +02:00
|
|
|
foreach my $strName ($oManifest->keys($strSection))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# Skip the root path since this must already exist
|
|
|
|
if ($strName eq '.')
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Determine the level by splitting the path
|
|
|
|
my @stryName = split('\/', $oManifest->dbPathGet(undef, $strName));
|
2015-01-23 03:11:33 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Only create the path/link of at the current level
|
|
|
|
if (@stryName == $iLevel)
|
2015-01-23 03:11:33 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $strDbPath = $oManifest->dbPathGet($self->{strDbClusterPath}, $strName);
|
|
|
|
|
|
|
|
# If the path/link does not already exist then create it. The clean() method should have determined if the
|
|
|
|
# permissions, destinations, etc. are correct
|
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, $strDbPath))
|
|
|
|
{
|
|
|
|
# Create a path
|
|
|
|
if ($strSection eq &MANIFEST_SECTION_TARGET_PATH)
|
|
|
|
{
|
|
|
|
$self->{oFile}->pathCreate(PATH_DB_ABSOLUTE, $strDbPath,
|
|
|
|
$oManifest->get($strSection, $strName, MANIFEST_SUBKEY_MODE));
|
|
|
|
}
|
|
|
|
# Else create a link
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Retrieve the link destination
|
|
|
|
my $strDestination = $oManifest->get($strSection, $strName, MANIFEST_SUBKEY_DESTINATION);
|
|
|
|
|
|
|
|
# In order to create relative links they must be converted to absolute links and then made relative
|
|
|
|
# again by linkCreate(). It's possible to modify linkCreate() to accept relative paths but that could
|
|
|
|
# have an impact elsewhere and doesn't seem worth it.
|
|
|
|
$self->{oFile}->linkCreate(PATH_DB_ABSOLUTE, pathAbsolute(dirname($strDbPath), $strDestination),
|
|
|
|
PATH_DB_ABSOLUTE, $strDbPath, undef, (index($strDestination, '/') != 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set ownership (??? this could be done better inside the file functions)
|
|
|
|
my $strUser = $oManifest->get($strSection, $strName, MANIFEST_SUBKEY_USER);
|
|
|
|
my $strGroup = $oManifest->get($strSection, $strName, MANIFEST_SUBKEY_GROUP);
|
|
|
|
|
|
|
|
if ($strUser ne getpwuid($<) || $strGroup ne getgrgid($())
|
|
|
|
{
|
|
|
|
$self->{oFile}->owner(PATH_DB_ABSOLUTE, $strDbPath, $strUser, $strGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Indicate that at least one path/link was found at this level
|
|
|
|
$iFound++;
|
2015-01-23 03:11:33 +02:00
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-09 00:34:27 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Move to the next path/link level
|
|
|
|
$iLevel++;
|
2015-05-09 00:34:27 +02:00
|
|
|
}
|
2016-04-15 04:50:02 +02:00
|
|
|
while ($iFound > 0);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-01-26 21:59:58 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# recovery
|
2015-01-26 21:59:58 +02:00
|
|
|
#
|
|
|
|
# Creates the recovery.conf file.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub recovery
|
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
my $self = shift; # Class hash
|
|
|
|
my $strDbVersion = shift; # Version to restore
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
2016-08-11 23:32:28 +02:00
|
|
|
my ($strOperation) = logDebugParam (__PACKAGE__ . '->recovery');
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-01-26 21:59:58 +02:00
|
|
|
# Create recovery.conf path/file
|
2016-05-14 16:33:12 +02:00
|
|
|
my $strRecoveryConf = $self->{strDbClusterPath} . '/' . DB_FILE_RECOVERYCONF;
|
2015-01-26 21:59:58 +02:00
|
|
|
|
|
|
|
# See if recovery.conf already exists
|
|
|
|
my $bRecoveryConfExists = $self->{oFile}->exists(PATH_DB_ABSOLUTE, $strRecoveryConf);
|
|
|
|
|
2015-05-09 00:34:27 +02:00
|
|
|
# If RECOVERY_TYPE_PRESERVE then warn if recovery.conf does not exist and return
|
2015-08-29 20:20:46 +02:00
|
|
|
if (optionTest(OPTION_TYPE, RECOVERY_TYPE_PRESERVE))
|
2015-01-26 21:59:58 +02:00
|
|
|
{
|
|
|
|
if (!$bRecoveryConfExists)
|
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
&log(WARN, "recovery type is " . optionGet(OPTION_TYPE) . " but recovery file does not exist at ${strRecoveryConf}");
|
2015-01-26 21:59:58 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
else
|
2015-01-26 21:59:58 +02:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
# In all other cases the old recovery.conf should be removed if it exists
|
|
|
|
if ($bRecoveryConfExists)
|
|
|
|
{
|
|
|
|
$self->{oFile}->remove(PATH_DB_ABSOLUTE, $strRecoveryConf);
|
|
|
|
}
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# If RECOVERY_TYPE_NONE then return
|
|
|
|
if (!optionTest(OPTION_TYPE, RECOVERY_TYPE_NONE))
|
2015-01-26 21:59:58 +02:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
# Write recovery options read from the configuration file
|
|
|
|
my $strRecovery = '';
|
|
|
|
my $bRestoreCommandOverride = false;
|
2015-01-31 20:48:09 +02:00
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
if (optionTest(OPTION_RESTORE_RECOVERY_OPTION))
|
2015-01-31 20:48:09 +02:00
|
|
|
{
|
2015-09-08 13:31:24 +02:00
|
|
|
my $oRecoveryRef = optionGet(OPTION_RESTORE_RECOVERY_OPTION);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
foreach my $strKey (sort(keys(%$oRecoveryRef)))
|
|
|
|
{
|
|
|
|
my $strPgKey = $strKey;
|
|
|
|
$strPgKey =~ s/\-/\_/g;
|
|
|
|
|
|
|
|
if ($strPgKey eq 'restore_command')
|
|
|
|
{
|
|
|
|
$bRestoreCommandOverride = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$strRecovery .= "${strPgKey} = '$$oRecoveryRef{$strKey}'\n";
|
|
|
|
}
|
2015-01-31 20:48:09 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Write the restore command
|
|
|
|
if (!$bRestoreCommandOverride)
|
|
|
|
{
|
|
|
|
$strRecovery .= "restore_command = '" . commandWrite(CMD_ARCHIVE_GET) . " %f \"%p\"'\n";
|
|
|
|
}
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2016-01-14 03:35:12 +02:00
|
|
|
# If type is RECOVERY_TYPE_IMMEDIATE
|
|
|
|
if (optionTest(OPTION_TYPE, RECOVERY_TYPE_IMMEDIATE))
|
|
|
|
{
|
|
|
|
$strRecovery .= "recovery_target = '" . RECOVERY_TYPE_IMMEDIATE . "'\n";
|
|
|
|
}
|
|
|
|
# If type is not RECOVERY_TYPE_DEFAULT write target options
|
|
|
|
elsif (!optionTest(OPTION_TYPE, RECOVERY_TYPE_DEFAULT))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
|
|
|
# Write the recovery target
|
|
|
|
$strRecovery .= "recovery_target_" . optionGet(OPTION_TYPE) . " = '" . optionGet(OPTION_TARGET) . "'\n";
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Write recovery_target_inclusive
|
|
|
|
if (optionGet(OPTION_TARGET_EXCLUSIVE, false))
|
|
|
|
{
|
|
|
|
$strRecovery .= "recovery_target_inclusive = 'false'\n";
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Write pause_at_recovery_target
|
2016-01-14 03:35:12 +02:00
|
|
|
if (optionTest(OPTION_TARGET_ACTION))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-01-14 03:35:12 +02:00
|
|
|
my $strTargetAction = optionGet(OPTION_TARGET_ACTION);
|
|
|
|
|
|
|
|
if ($strTargetAction ne OPTION_DEFAULT_RESTORE_TARGET_ACTION)
|
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($strDbVersion >= PG_VERSION_95)
|
2016-01-14 03:35:12 +02:00
|
|
|
{
|
|
|
|
$strRecovery .= "recovery_target_action = '${strTargetAction}'\n";
|
|
|
|
}
|
2016-05-14 16:33:12 +02:00
|
|
|
elsif ($strDbVersion >= PG_VERSION_91)
|
2016-01-14 03:35:12 +02:00
|
|
|
{
|
|
|
|
$strRecovery .= "pause_at_recovery_target = 'false'\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
confess &log(ERROR, OPTION_TARGET_ACTION . ' option is only available in PostgreSQL >= ' . PG_VERSION_91)
|
2016-01-14 03:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Write recovery_target_timeline
|
|
|
|
if (optionTest(OPTION_TARGET_TIMELINE))
|
|
|
|
{
|
|
|
|
$strRecovery .= "recovery_target_timeline = '" . optionGet(OPTION_TARGET_TIMELINE) . "'\n";
|
|
|
|
}
|
2015-01-27 18:44:23 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Write recovery.conf
|
|
|
|
my $hFile;
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
open($hFile, '>', $strRecoveryConf)
|
|
|
|
or confess &log(ERROR, "unable to open ${strRecoveryConf}: $!");
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
syswrite($hFile, $strRecovery)
|
|
|
|
or confess "unable to write section ${strRecoveryConf}: $!";
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
close($hFile)
|
|
|
|
or confess "unable to close ${strRecoveryConf}: $!";
|
2015-01-26 21:59:58 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
&log(INFO, "wrote $strRecoveryConf");
|
|
|
|
}
|
|
|
|
}
|
2015-05-09 00:34:27 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
2015-01-26 21:59:58 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# process
|
2014-12-23 22:03:06 +02:00
|
|
|
#
|
|
|
|
# Takes a backup and restores it back to the original or a remapped location.
|
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub process
|
2014-12-23 22:03:06 +02:00
|
|
|
{
|
|
|
|
my $self = shift; # Class hash
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# 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
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
if (!fileExists($self->{strDbClusterPath}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "\$PGDATA directory $self->{strDbClusterPath} does not exist");
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
# Make sure that Postgres is not running
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->{oFile}->exists(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . DB_FILE_POSTMASTERPID))
|
2014-12-23 22:03:06 +02:00
|
|
|
{
|
2015-01-28 20:14:46 +02:00
|
|
|
confess &log(ERROR, 'unable to restore while Postgres is running', ERROR_POSTMASTER_RUNNING);
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 06:03:29 +02:00
|
|
|
# If the restore will be destructive attempt to verify that $PGDATA is valid
|
|
|
|
if ((optionGet(OPTION_DELTA) || optionGet(OPTION_FORCE)) &&
|
2016-05-14 16:33:12 +02:00
|
|
|
!($self->{oFile}->exists(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . DB_FILE_PGVERSION) ||
|
2016-02-06 06:03:29 +02:00
|
|
|
$self->{oFile}->exists(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_MANIFEST)))
|
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
&log(WARN, '--delta or --force specified but unable to find \'' . DB_FILE_PGVERSION . '\' or \'' . FILE_MANIFEST .
|
2016-02-06 06:03:29 +02:00
|
|
|
'\' in \'' . $self->{strDbClusterPath} . '\' to confirm that this is a valid $PGDATA directory.' .
|
|
|
|
' --delta and --force have been disabled and if any files exist in the destination directories the restore' .
|
|
|
|
' will be aborted.');
|
|
|
|
|
|
|
|
optionSet(OPTION_DELTA, false);
|
|
|
|
optionSet(OPTION_FORCE, false);
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Copy backup info, load it, then delete
|
|
|
|
$self->{oFile}->copy(PATH_BACKUP_CLUSTER, FILE_BACKUP_INFO,
|
|
|
|
PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_BACKUP_INFO);
|
|
|
|
|
|
|
|
my $oBackupInfo = new pgBackRest::BackupInfo($self->{strDbClusterPath}, false);
|
|
|
|
|
|
|
|
$self->{oFile}->remove(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_BACKUP_INFO, undef, false);
|
|
|
|
|
|
|
|
# If set to restore is latest then get the actual set
|
|
|
|
if ($self->{strBackupSet} eq OPTION_DEFAULT_RESTORE_SET)
|
|
|
|
{
|
|
|
|
$self->{strBackupSet} = $oBackupInfo->last(BACKUP_TYPE_INCR);
|
|
|
|
|
|
|
|
if (!defined($self->{strBackupSet}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "no backup sets to restore", ERROR_BACKUP_SET_INVALID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Otherwise check to make sure specified set is valid
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!$oBackupInfo->current($self->{strBackupSet}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "backup set $self->{strBackupSet} is not valid", ERROR_BACKUP_SET_INVALID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Log the backup set to restore
|
|
|
|
&log(INFO, "restore backup set " . $self->{strBackupSet});
|
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
# Make sure the backup path is valid and load the manifest
|
2015-08-29 20:20:46 +02:00
|
|
|
my $oManifest = $self->manifestLoad();
|
2014-12-23 22:03:06 +02:00
|
|
|
|
2015-07-16 17:12:48 +02:00
|
|
|
# Delete pg_control file. This will be copied from the backup at the very end to prevent a partially restored database
|
2015-08-07 16:21:08 +02:00
|
|
|
# from being started by PostgreSQL.
|
2016-04-15 04:50:02 +02:00
|
|
|
$self->{oFile}->remove(PATH_DB_ABSOLUTE, $oManifest->dbPathGet(
|
|
|
|
$oManifest->get(MANIFEST_SECTION_BACKUP_TARGET, MANIFEST_TARGET_PGDATA, MANIFEST_SUBKEY_PATH),
|
|
|
|
MANIFEST_FILE_PGCONTROL));
|
2015-07-16 17:12:48 +02:00
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
# Clean the restore paths
|
2015-01-07 17:59:43 +02:00
|
|
|
$self->clean($oManifest);
|
2014-12-23 22:03:06 +02:00
|
|
|
|
|
|
|
# Build paths/links in the restore paths
|
2015-01-07 17:59:43 +02:00
|
|
|
$self->build($oManifest);
|
2014-12-23 22:03:06 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Get variables required for restore
|
2015-08-29 20:20:46 +02:00
|
|
|
my $lCopyTimeBegin = $oManifest->numericGet(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_COPY_START);
|
|
|
|
my $bSourceCompression = $oManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS);
|
2015-04-07 13:34:37 +02:00
|
|
|
my $strCurrentUser = getpwuid($<);
|
|
|
|
my $strCurrentGroup = getgrgid($();
|
|
|
|
|
2016-05-11 15:21:39 +02:00
|
|
|
# Build an expression to match files that should be zeroed for filtered restores
|
|
|
|
my $strDbFilter;
|
|
|
|
|
|
|
|
if (optionTest(OPTION_DB_INCLUDE))
|
|
|
|
{
|
|
|
|
# Build a list of databases from the manifest since the db name/id mappings will not be available for an offline restore.
|
|
|
|
my %oDbList;
|
|
|
|
|
|
|
|
foreach my $strFile ($oManifest->keys(MANIFEST_SECTION_TARGET_FILE))
|
|
|
|
{
|
|
|
|
if ($strFile =~ ('^' . MANIFEST_TARGET_PGDATA . '\/base\/[0-9]+\/PG\_VERSION'))
|
|
|
|
{
|
|
|
|
my $lDbId = basename(dirname($strFile));
|
|
|
|
|
|
|
|
$oDbList{$lDbId} = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# If no databases where found then this backup does not contain a valid cluster
|
|
|
|
if (keys(%oDbList) == 0)
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, 'no databases for include/exclude -- does not look like a valid cluster');
|
|
|
|
}
|
|
|
|
|
|
|
|
# Log databases found
|
|
|
|
&log(DETAIL, 'databases for include/exclude (' . join(', ', sort(keys(%oDbList))) . ')');
|
|
|
|
|
|
|
|
# Remove included databases from the list
|
|
|
|
my $oDbInclude = optionGet(OPTION_DB_INCLUDE);
|
|
|
|
|
|
|
|
for my $strDbKey (sort(keys(%{$oDbInclude})))
|
|
|
|
{
|
|
|
|
# To be included the db must exist - first treat the key as an id and check for a match
|
|
|
|
if (!defined($oDbList{$strDbKey}))
|
|
|
|
{
|
|
|
|
# If the key does not match as an id then check for a name mapping
|
|
|
|
my $lDbId = $oManifest->get(MANIFEST_SECTION_DB, $strDbKey, MANIFEST_KEY_DB_ID, false);
|
|
|
|
|
|
|
|
if (!defined($lDbId) || !defined($oDbList{$lDbId}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "database to include '${strDbKey}' does not exist", ERROR_DB_MISSING);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set the key to the id if the name mapping was successful
|
|
|
|
$strDbKey = $lDbId;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Error if the db is a built-in db
|
|
|
|
if ($strDbKey < DB_USER_OBJECT_MINIMUM_ID)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "system databases (template0, postgres, etc.) are included by default", ERROR_DB_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Otherwise remove from list of DBs to zero
|
|
|
|
delete($oDbList{$strDbKey});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Construct regexp to identify files that should be zeroed
|
|
|
|
for my $strDbKey (sort(keys(%oDbList)))
|
|
|
|
{
|
|
|
|
# Only user created databases can be zeroed, never built-in databases
|
|
|
|
if ($strDbKey >= DB_USER_OBJECT_MINIMUM_ID)
|
|
|
|
{
|
|
|
|
# Filter files in base directory
|
|
|
|
$strDbFilter .= (defined($strDbFilter) ? '|' : '') .
|
|
|
|
'(^' . MANIFEST_TARGET_PGDATA . '\/base\/' . $strDbKey . '\/)';
|
|
|
|
|
|
|
|
# Filter files in tablespace directories
|
|
|
|
for my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))
|
|
|
|
{
|
|
|
|
if ($oManifest->isTargetTablespace($strTarget))
|
|
|
|
{
|
|
|
|
$strDbFilter .=
|
|
|
|
'|(^' . $strTarget . '\/' . $oManifest->tablespacePathGet() . '\/' . $strDbKey . '\/)';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output the generated filter for debugging
|
|
|
|
&log(DETAIL, "database filter: $strDbFilter");
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:56:56 +02:00
|
|
|
# Create hash containing files to restore
|
|
|
|
my %oRestoreHash;
|
|
|
|
my $lSizeTotal = 0;
|
|
|
|
my $lSizeCurrent = 0;
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
foreach my $strFile ($oManifest->keys(MANIFEST_SECTION_TARGET_FILE))
|
2014-12-23 22:03:06 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
# Skip the tablespace_map file in versions >= 9.5 so Postgres does not rewrite links in DB_PATH_PGTBLSPC.
|
|
|
|
# The tablespace links have already been created by Restore::build().
|
|
|
|
if ($strFile eq MANIFEST_FILE_TABLESPACEMAP &&
|
2016-05-14 16:33:12 +02:00
|
|
|
$oManifest->get(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_DB_VERSION) >= PG_VERSION_95)
|
2016-02-02 21:33:15 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
next;
|
2016-02-02 21:33:15 +02:00
|
|
|
}
|
2015-01-20 21:13:35 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
my $lSize = $oManifest->numericGet(MANIFEST_SECTION_TARGET_FILE, $strFile, MANIFEST_SUBKEY_SIZE);
|
|
|
|
|
|
|
|
# By default put everything into a single queue
|
|
|
|
my $strQueueKey = MANIFEST_TARGET_PGDATA;
|
|
|
|
|
|
|
|
# If multiple threads and the file belongs in a tablespace then put in a tablespace-specific queue
|
|
|
|
if (optionGet(OPTION_THREAD_MAX) > 1 && index($strFile, DB_PATH_PGTBLSPC . '/') == 0)
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
$strQueueKey = DB_PATH_PGTBLSPC . '/' . (split('\/', $strFile))[1];
|
|
|
|
}
|
2015-08-07 16:21:08 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Preface the file key with the size. This allows for sorting the files to restore by size.
|
|
|
|
my $strFileKey;
|
2015-05-07 23:56:56 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Skip this for global/pg_control since it will be copied as the last step and needs to named in a way that it
|
|
|
|
# can be found for the copy.
|
|
|
|
if ($strFile eq MANIFEST_FILE_PGCONTROL)
|
|
|
|
{
|
|
|
|
$strFileKey = $strFile;
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{skip} = true;
|
|
|
|
}
|
|
|
|
# Else continue normally
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strFileKey = sprintf("%016d-${strFile}", $lSize);
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{skip} = false;
|
2015-07-16 17:12:48 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
$lSizeTotal += $lSize;
|
|
|
|
}
|
2015-05-07 23:56:56 +02:00
|
|
|
|
2016-05-11 15:21:39 +02:00
|
|
|
# Zero files that should be filtered
|
|
|
|
if (defined($strDbFilter) && $strFile =~ $strDbFilter && $strFile !~ /\/PG\_VERSION$/)
|
|
|
|
{
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{zero} = true;
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Get restore information
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{repo_file} = $strFile;
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{db_file} = $oManifest->dbPathGet($self->{strDbClusterPath}, $strFile);
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{size} = $lSize;
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{reference} =
|
|
|
|
$oManifest->boolTest(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK, undef, true) ? undef :
|
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_FILE, $strFile, MANIFEST_SUBKEY_REFERENCE, false);
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{modification_time} =
|
|
|
|
$oManifest->numericGet(MANIFEST_SECTION_TARGET_FILE, $strFile, MANIFEST_SUBKEY_TIMESTAMP);
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{mode} =
|
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_FILE, $strFile, MANIFEST_SUBKEY_MODE);
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{user} =
|
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_FILE, $strFile, MANIFEST_SUBKEY_USER);
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{group} =
|
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_FILE, $strFile, MANIFEST_SUBKEY_GROUP);
|
|
|
|
|
|
|
|
# Checksum is only stored if size > 0
|
|
|
|
if ($lSize > 0)
|
|
|
|
{
|
|
|
|
$oRestoreHash{$strQueueKey}{$strFileKey}{checksum} =
|
|
|
|
$oManifest->get(MANIFEST_SECTION_TARGET_FILE, $strFile, MANIFEST_SUBKEY_CHECKSUM);
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# If multi-threaded then create threads to copy files
|
2015-08-29 20:20:46 +02:00
|
|
|
if (optionGet(OPTION_THREAD_MAX) > 1)
|
2015-02-28 17:23:33 +02:00
|
|
|
{
|
2015-09-08 13:31:24 +02:00
|
|
|
# Load module dynamically
|
2016-04-14 15:30:54 +02:00
|
|
|
require pgBackRest::Protocol::ThreadGroup;
|
|
|
|
pgBackRest::Protocol::ThreadGroup->import();
|
2015-09-08 13:31:24 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
logDebugMisc
|
|
|
|
(
|
|
|
|
$strOperation, 'restore with threads',
|
|
|
|
{name => 'iThreadTotal', value => optionGet(OPTION_THREAD_MAX)}
|
|
|
|
);
|
2015-05-07 23:56:56 +02:00
|
|
|
|
|
|
|
# Initialize the thread queues
|
|
|
|
my @oyRestoreQueue;
|
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
foreach my $strQueueKey (sort (keys %oRestoreHash))
|
2015-02-28 17:23:33 +02:00
|
|
|
{
|
2015-05-07 23:56:56 +02:00
|
|
|
push(@oyRestoreQueue, Thread::Queue->new());
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
foreach my $strFileKey (sort {$b cmp $a} (keys(%{$oRestoreHash{$strQueueKey}})))
|
2015-05-07 23:56:56 +02:00
|
|
|
{
|
2015-08-07 16:21:08 +02:00
|
|
|
# Skip files marked to be copied later
|
2016-04-15 04:50:02 +02:00
|
|
|
next if ($oRestoreHash{$strQueueKey}{$strFileKey}{skip});
|
2015-07-16 17:12:48 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
$oyRestoreQueue[@oyRestoreQueue - 1]->enqueue($oRestoreHash{$strQueueKey}{$strFileKey});
|
2015-05-07 23:56:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialize the param hash
|
|
|
|
my %oParam;
|
|
|
|
|
|
|
|
$oParam{copy_time_begin} = $lCopyTimeBegin;
|
|
|
|
$oParam{size_total} = $lSizeTotal;
|
2015-08-29 20:20:46 +02:00
|
|
|
$oParam{delta} = optionGet(OPTION_DELTA);
|
|
|
|
$oParam{force} = optionGet(OPTION_FORCE);
|
|
|
|
$oParam{backup_path} = $self->{strBackupSet};
|
2015-05-07 23:56:56 +02:00
|
|
|
$oParam{source_compression} = $bSourceCompression;
|
|
|
|
$oParam{current_user} = $strCurrentUser;
|
|
|
|
$oParam{current_group} = $strCurrentGroup;
|
|
|
|
$oParam{queue} = \@oyRestoreQueue;
|
|
|
|
|
|
|
|
# Run the threads
|
2015-08-29 20:20:46 +02:00
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < optionGet(OPTION_THREAD_MAX); $iThreadIdx++)
|
2015-05-07 23:56:56 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
threadGroupRun($iThreadIdx, 'restore', \%oParam);
|
2015-02-28 17:23:33 +02:00
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# Complete thread queues
|
2015-12-24 17:32:25 +02:00
|
|
|
while (!threadGroupComplete())
|
|
|
|
{
|
|
|
|
# Keep the protocol layer from timing out
|
2016-07-26 22:21:14 +02:00
|
|
|
$self->{oProtocol}->keepAlive();
|
2015-12-24 17:32:25 +02:00
|
|
|
};
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
2015-05-07 23:56:56 +02:00
|
|
|
else
|
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
logDebugMisc($strOperation, 'restore in main process');
|
2015-05-07 23:56:56 +02:00
|
|
|
|
|
|
|
# Restore file in main process
|
2016-04-15 04:50:02 +02:00
|
|
|
foreach my $strQueueKey (sort (keys %oRestoreHash))
|
2015-05-07 23:56:56 +02:00
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
foreach my $strFileKey (sort {$b cmp $a} (keys(%{$oRestoreHash{$strQueueKey}})))
|
2015-05-07 23:56:56 +02:00
|
|
|
{
|
2015-08-07 16:21:08 +02:00
|
|
|
# Skip files marked to be copied later
|
2016-04-15 04:50:02 +02:00
|
|
|
next if ($oRestoreHash{$strQueueKey}{$strFileKey}{skip});
|
2015-07-16 17:12:48 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
$lSizeCurrent = restoreFile($oRestoreHash{$strQueueKey}{$strFileKey}, $lCopyTimeBegin, optionGet(OPTION_DELTA),
|
2015-08-29 20:20:46 +02:00
|
|
|
optionGet(OPTION_FORCE), $self->{strBackupSet}, $bSourceCompression, $strCurrentUser,
|
2015-05-07 23:56:56 +02:00
|
|
|
$strCurrentGroup, $self->{oFile}, $lSizeTotal, $lSizeCurrent);
|
2016-06-24 19:16:07 +02:00
|
|
|
|
|
|
|
# Keep the protocol layer from timing out while checksumming
|
2016-07-26 22:21:14 +02:00
|
|
|
$self->{oProtocol}->keepAlive();
|
2015-05-07 23:56:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-01-26 21:59:58 +02:00
|
|
|
# Create recovery.conf file
|
2016-01-14 03:35:12 +02:00
|
|
|
$self->recovery($oManifest->get(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_DB_VERSION));
|
2015-05-09 00:34:27 +02:00
|
|
|
|
2015-07-16 17:12:48 +02:00
|
|
|
# Copy pg_control last
|
2016-04-15 04:50:02 +02:00
|
|
|
&log(INFO, 'restore ' . $oManifest->dbPathGet(undef, MANIFEST_FILE_PGCONTROL) .
|
|
|
|
' (copied last to ensure aborted restores cannot be started)');
|
2015-07-16 17:12:48 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
restoreFile($oRestoreHash{&MANIFEST_TARGET_PGDATA}{&MANIFEST_FILE_PGCONTROL}, $lCopyTimeBegin, optionGet(OPTION_DELTA),
|
2015-08-29 20:20:46 +02:00
|
|
|
optionGet(OPTION_FORCE), $self->{strBackupSet}, $bSourceCompression, $strCurrentUser, $strCurrentGroup,
|
2016-04-15 04:50:02 +02:00
|
|
|
$self->{oFile});
|
2015-07-16 17:12:48 +02:00
|
|
|
|
2016-02-06 06:03:29 +02:00
|
|
|
# Finally remove the manifest to indicate the restore is complete
|
|
|
|
$self->{oFile}->remove(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_MANIFEST, undef, false);
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 18:42:54 +02:00
|
|
|
1;
|