2014-12-18 18:42:54 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# RESTORE MODULE
|
|
|
|
####################################################################################################################################
|
|
|
|
package BackRest::Restore;
|
|
|
|
|
|
|
|
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;
|
|
|
|
use warnings;
|
|
|
|
use Carp;
|
|
|
|
|
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
use lib dirname($0);
|
|
|
|
use BackRest::Utility;
|
2014-12-22 18:24:32 +02:00
|
|
|
use BackRest::ThreadGroup;
|
2014-12-18 18:42:54 +02:00
|
|
|
use BackRest::Config;
|
|
|
|
use BackRest::File;
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2014-12-18 18:42:54 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
my $strDbClusterPath = shift; # Database cluster path
|
2014-12-23 18:48:51 +02:00
|
|
|
my $strBackupPath = shift; # Backup to restore
|
2014-12-23 22:03:06 +02:00
|
|
|
my $oRemapRef = shift; # Tablespace remaps
|
2014-12-23 19:48:25 +02:00
|
|
|
my $oFile = shift; # Default file object
|
2014-12-22 18:24:32 +02:00
|
|
|
my $iThreadTotal = shift; # Total threads to run for restore
|
2014-12-19 19:49:56 +02:00
|
|
|
my $bForce = shift; # Force the restore even if files are present
|
2014-12-18 18:42:54 +02:00
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
|
|
|
# Initialize variables
|
|
|
|
$self->{strDbClusterPath} = $strDbClusterPath;
|
2014-12-23 19:48:25 +02:00
|
|
|
$self->{oFile} = $oFile;
|
2014-12-30 22:41:43 +02:00
|
|
|
$self->{iThreadTotal} = defined($iThreadTotal) ? $iThreadTotal : 1;
|
2014-12-19 19:49:56 +02:00
|
|
|
$self->{bForce} = $bForce;
|
2014-12-23 22:03:06 +02:00
|
|
|
$self->{oRemapRef} = $oRemapRef;
|
2014-12-18 18:42:54 +02:00
|
|
|
|
2014-12-19 19:49:56 +02:00
|
|
|
# If backup path is not specified then default to latest
|
2014-12-23 18:48:51 +02:00
|
|
|
if (defined($strBackupPath))
|
2014-12-18 18:42:54 +02:00
|
|
|
{
|
2014-12-23 18:48:51 +02:00
|
|
|
$self->{strBackupPath} = $strBackupPath;
|
2014-12-18 18:42:54 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-23 18:48:51 +02:00
|
|
|
$self->{strBackupPath} = PATH_LATEST;
|
2014-12-18 18:42:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2014-12-30 18:59:57 +02:00
|
|
|
####################################################################################################################################
|
2014-12-31 18:20:46 +02:00
|
|
|
# MANIFEST_OWNERSHIP_CHECK
|
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.
|
|
|
|
####################################################################################################################################
|
2014-12-31 18:20:46 +02:00
|
|
|
sub manifest_ownership_check
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
|
|
|
my $self = shift; # Class hash
|
|
|
|
my $oManifestRef = shift; # Backup manifest
|
|
|
|
|
|
|
|
# Create hashes to track valid/invalid users/groups
|
|
|
|
my %oOwnerHash = ();
|
|
|
|
|
|
|
|
# Create hash for each type and owner to be checked
|
2014-12-30 22:41:43 +02:00
|
|
|
my $strDefaultUser = getpwuid($<);
|
|
|
|
my $strDefaultGroup = getgrgid($();
|
|
|
|
|
2014-12-30 18:59:57 +02:00
|
|
|
my %oFileTypeHash = ('path' => true, 'link' => true, 'file' => true);
|
2014-12-30 22:41:43 +02:00
|
|
|
my %oOwnerTypeHash = ('user' => $strDefaultUser, 'group' => $strDefaultGroup);
|
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
|
|
|
{
|
2014-12-31 18:20:46 +02:00
|
|
|
# Loop through all backup paths (base and tablespaces)
|
2014-12-30 22:41:43 +02:00
|
|
|
foreach my $strPathKey (sort(keys ${$oManifestRef}{'backup:path'}))
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2014-12-31 18:20:46 +02:00
|
|
|
# Loop through types (path, link, file)
|
2014-12-30 18:59:57 +02:00
|
|
|
foreach my $strFileType (sort (keys %oFileTypeHash))
|
|
|
|
{
|
|
|
|
# Get users and groups for paths
|
2014-12-31 19:28:16 +02:00
|
|
|
if (defined(${$oManifestRef}{"${strPathKey}:${strFileType}"}))
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2014-12-31 19:28:16 +02:00
|
|
|
foreach my $strName (sort (keys ${$oManifestRef}{"${strPathKey}:${strFileType}"}))
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2014-12-31 19:28:16 +02:00
|
|
|
my $strOwner = ${$oManifestRef}{"${strPathKey}:${strFileType}"}{$strName}{$strOwnerType};
|
|
|
|
|
|
|
|
# If root then test to see if the user/group is valid
|
|
|
|
if ($< == 0)
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2014-12-31 19:28:16 +02:00
|
|
|
# If the owner has not been tested yet then test it
|
|
|
|
if (!defined($oOwnerHash{$strOwnerType}{$strOwner}))
|
2014-12-31 18:20:46 +02:00
|
|
|
{
|
2014-12-31 19:28:16 +02:00
|
|
|
my $strOwnerId;
|
|
|
|
|
|
|
|
if ($strOwnerType eq 'user')
|
|
|
|
{
|
|
|
|
$strOwnerId = getpwnam($strOwner);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strOwnerId = getgrnam($strOwner);
|
|
|
|
}
|
|
|
|
|
|
|
|
$oOwnerHash{$strOwnerType}{$strOwner} = defined($strOwnerId) ? true : false;
|
2014-12-31 18:20:46 +02:00
|
|
|
}
|
2014-12-31 19:28:16 +02:00
|
|
|
|
|
|
|
if (!$oOwnerHash{$strOwnerType}{$strOwner})
|
2014-12-31 18:20:46 +02:00
|
|
|
{
|
2014-12-31 19:28:16 +02:00
|
|
|
${$oManifestRef}{"${strPathKey}:${strFileType}"}{$strName}{$strOwnerType} =
|
|
|
|
$oOwnerTypeHash{$strOwnerType};
|
2014-12-31 18:20:46 +02:00
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
2014-12-31 19:28:16 +02:00
|
|
|
# Else set user/group to current user/group
|
|
|
|
else
|
2014-12-30 18:59:57 +02:00
|
|
|
{
|
2014-12-31 19:28:16 +02:00
|
|
|
if ($strOwner ne $oOwnerTypeHash{$strOwnerType})
|
|
|
|
{
|
|
|
|
$oOwnerHash{$strOwnerType}{$strOwner} = false;
|
|
|
|
${$oManifestRef}{"${strPathKey}:${strFileType}"}{$strName}{$strOwnerType} =
|
|
|
|
$oOwnerTypeHash{$strOwnerType};
|
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2014-12-31 19:28:16 +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})
|
|
|
|
{
|
|
|
|
&log(WARN, "${strOwnerType} ${strOwner} " . ($< == 0 ? "does not exist" : "cannot be set") .
|
|
|
|
", changed to $oOwnerTypeHash{$strOwnerType}");
|
|
|
|
}
|
2014-12-31 18:20:46 +02:00
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 18:42:54 +02:00
|
|
|
####################################################################################################################################
|
2014-12-23 22:03:06 +02:00
|
|
|
# MANIFEST_LOAD
|
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
|
|
|
####################################################################################################################################
|
2014-12-23 22:03:06 +02:00
|
|
|
sub manifest_load
|
2014-12-18 18:42:54 +02:00
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
my $self = shift; # Class hash
|
|
|
|
my $oManifestRef = shift; # Backup manifest
|
2014-12-23 19:48:25 +02:00
|
|
|
|
|
|
|
if ($self->{oFile}->exists(PATH_BACKUP_CLUSTER, $self->{strBackupPath}))
|
2014-12-18 18:42:54 +02:00
|
|
|
{
|
|
|
|
# Copy the backup manifest to the db cluster path
|
2014-12-23 19:48:25 +02:00
|
|
|
$self->{oFile}->copy(PATH_BACKUP_CLUSTER, $self->{strBackupPath} . '/' . FILE_MANIFEST,
|
2014-12-19 00:05:06 +02:00
|
|
|
PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_MANIFEST);
|
2014-12-18 18:42:54 +02:00
|
|
|
|
2014-12-24 01:52:38 +02:00
|
|
|
# Load the manifest into a hash
|
|
|
|
ini_load($self->{oFile}->path_get(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_MANIFEST), $oManifestRef);
|
|
|
|
|
|
|
|
# Remove the manifest now that it is in memory
|
|
|
|
$self->{oFile}->remove(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_MANIFEST);
|
2014-12-31 19:28:16 +02:00
|
|
|
|
2014-12-30 22:41:43 +02:00
|
|
|
# If backup is latest then set it equal to backup label, else verify that requested backup and label match
|
|
|
|
if ($self->{strBackupPath} eq PATH_LATEST)
|
|
|
|
{
|
|
|
|
$self->{strBackupPath} = ${$oManifestRef}{'backup'}{label};
|
|
|
|
}
|
|
|
|
elsif ($self->{strBackupPath} ne ${$oManifestRef}{'backup'}{label})
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "request backup $self->{strBackupPath} and label ${$oManifestRef}{'backup'}{label} do not match " .
|
|
|
|
" - this indicates some sort of corruption (at the very least paths have been renamed.");
|
|
|
|
}
|
2014-12-24 01:52:38 +02:00
|
|
|
|
|
|
|
# If tablespaces have been remapped, update the manifest
|
|
|
|
if (defined($self->{oRemapRef}))
|
|
|
|
{
|
|
|
|
foreach my $strPathKey (sort(keys $self->{oRemapRef}))
|
|
|
|
{
|
|
|
|
my $strRemapPath = ${$self->{oRemapRef}}{$strPathKey};
|
|
|
|
|
|
|
|
if ($strPathKey eq 'base')
|
|
|
|
{
|
|
|
|
&log(INFO, "remapping base to ${strRemapPath}");
|
|
|
|
${$oManifestRef}{'backup:path'}{$strPathKey} = $strRemapPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# If the tablespace beigns with prefix 'tablespace:' then strip the prefix. This only needs to be used in
|
|
|
|
# the case that there is a tablespace called 'base'
|
|
|
|
if (index($strPathKey, 'tablespace:') == 0)
|
|
|
|
{
|
|
|
|
$strPathKey = substr($strPathKey, length('tablespace:'));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure that the tablespace exists in the manifest
|
|
|
|
if (!defined(${$oManifestRef}{'backup:tablespace'}{$strPathKey}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "cannot remap invalid tablespace ${strPathKey} to ${strRemapPath}");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remap the tablespace in the manifest
|
|
|
|
&log(INFO, "remapping tablespace to ${strRemapPath}");
|
|
|
|
|
|
|
|
my $strTablespaceLink = ${$oManifestRef}{'backup:tablespace'}{$strPathKey}{link};
|
|
|
|
|
|
|
|
${$oManifestRef}{'backup:path'}{"tablespace:${strPathKey}"} = $strRemapPath;
|
|
|
|
${$oManifestRef}{'backup:tablespace'}{$strPathKey}{path} = $strRemapPath;
|
|
|
|
${$oManifestRef}{'base:link'}{"pg_tblspc/${strTablespaceLink}"}{link_destination} = $strRemapPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 18:42:54 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-23 18:48:51 +02:00
|
|
|
confess &log(ERROR, 'backup ' . $self->{strBackupPath} . ' does not exist');
|
2014-12-18 18:42:54 +02:00
|
|
|
}
|
2014-12-30 18:59:57 +02:00
|
|
|
|
2014-12-31 18:20:46 +02:00
|
|
|
$self->manifest_ownership_check($oManifestRef);
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CLEAN
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
my $oManifestRef = shift; # Backup manifest
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2014-12-19 19:49:56 +02:00
|
|
|
# Check each restore directory in the manifest and make sure that it exists and is empty.
|
|
|
|
# The --force option can be used to override the empty requirement.
|
2014-12-23 22:03:06 +02:00
|
|
|
foreach my $strPathKey (sort(keys ${$oManifestRef}{'backup:path'}))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
my $strPath = ${$oManifestRef}{'backup:path'}{$strPathKey};
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
&log(INFO, "checking/cleaning db path ${strPath}");
|
2014-12-21 17:11:17 +02:00
|
|
|
|
2014-12-23 19:48:25 +02:00
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, $strPath))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "required db path '${strPath}' does not exist");
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
# Load path manifest so it can be compared to deleted files/paths/links that are not in the backup
|
2014-12-19 19:49:56 +02:00
|
|
|
my %oPathManifest;
|
2014-12-23 19:48:25 +02:00
|
|
|
$self->{oFile}->manifest(PATH_DB_ABSOLUTE, $strPath, \%oPathManifest);
|
2014-12-19 19:49:56 +02:00
|
|
|
|
|
|
|
foreach my $strName (sort {$b cmp $a} (keys $oPathManifest{name}))
|
|
|
|
{
|
|
|
|
# Skip the root path
|
|
|
|
if ($strName eq '.')
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# If force was not specified then error if any file is found
|
|
|
|
if (!$self->{bForce})
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "db path '${strPath}' contains files");
|
|
|
|
}
|
|
|
|
|
2014-12-21 17:11:17 +02:00
|
|
|
my $strFile = "${strPath}/${strName}";
|
|
|
|
|
2014-12-19 19:49:56 +02:00
|
|
|
# Determine the file/path/link type
|
|
|
|
my $strType = 'file';
|
|
|
|
|
|
|
|
if ($oPathManifest{name}{$strName}{type} eq 'd')
|
|
|
|
{
|
|
|
|
$strType = 'path';
|
|
|
|
}
|
|
|
|
elsif ($oPathManifest{name}{$strName}{type} eq 'l')
|
|
|
|
{
|
|
|
|
$strType = 'link';
|
|
|
|
}
|
|
|
|
|
2014-12-21 17:11:17 +02:00
|
|
|
# Check to see if the file/path/link exists in the manifest
|
2014-12-23 22:03:06 +02:00
|
|
|
if (defined(${$oManifestRef}{"${strPathKey}:${strType}"}{$strName}))
|
2014-12-19 19:49:56 +02:00
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
my $strMode = ${$oManifestRef}{"${strPathKey}:${strType}"}{$strName}{permission};
|
2014-12-21 17:11:17 +02:00
|
|
|
|
|
|
|
# If file/path mode does not match, fix it
|
|
|
|
if ($strType ne 'link' && $strMode ne $oPathManifest{name}{$strName}{permission})
|
|
|
|
{
|
|
|
|
&log(DEBUG, "setting ${strFile} mode to ${strMode}");
|
|
|
|
|
|
|
|
chmod(oct($strMode), $strFile)
|
|
|
|
or confess 'unable to set mode ${strMode} for ${strFile}';
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
my $strUser = ${$oManifestRef}{"${strPathKey}:${strType}"}{$strName}{user};
|
|
|
|
my $strGroup = ${$oManifestRef}{"${strPathKey}:${strType}"}{$strName}{group};
|
2014-12-21 17:11:17 +02:00
|
|
|
|
|
|
|
# If ownership does not match, fix it
|
|
|
|
if ($strUser ne $oPathManifest{name}{$strName}{user} ||
|
|
|
|
$strGroup ne $oPathManifest{name}{$strName}{group})
|
|
|
|
{
|
|
|
|
&log(DEBUG, "setting ${strFile} ownership to ${strUser}:${strGroup}");
|
|
|
|
|
|
|
|
# !!! Need to decide if it makes sense to set the user to anything other than the db owner
|
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
|
2014-12-21 17:11:17 +02:00
|
|
|
# If a link does not have the same destination, then delete it (it will be recreated later)
|
2014-12-23 22:03:06 +02:00
|
|
|
if ($strType eq 'link' && ${$oManifestRef}{"${strPathKey}:${strType}"}{$strName}{link_destination} ne
|
2014-12-21 17:11:17 +02:00
|
|
|
$oPathManifest{name}{$strName}{link_destination})
|
|
|
|
{
|
|
|
|
&log(DEBUG, "removing link ${strFile} - destination changed");
|
|
|
|
unlink($strFile) or confess &log(ERROR, "unable to delete file ${strFile}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# If it does not then remove it
|
|
|
|
else
|
|
|
|
{
|
2014-12-19 19:49:56 +02:00
|
|
|
# If a path then remove it, all the files should have already been deleted since we are going in reverse order
|
|
|
|
if ($strType eq 'path')
|
|
|
|
{
|
2014-12-21 17:11:17 +02:00
|
|
|
&log(DEBUG, "removing path ${strFile}");
|
|
|
|
rmdir($strFile) or confess &log(ERROR, "unable to delete path ${strFile}, is it empty?");
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
2014-12-21 17:11:17 +02:00
|
|
|
# Else delete a file/link
|
2014-12-19 19:49:56 +02:00
|
|
|
else
|
|
|
|
{
|
2014-12-30 22:41:43 +02:00
|
|
|
&log(DEBUG, "removing file/link ${strFile}");
|
|
|
|
unlink($strFile) or confess &log(ERROR, "unable to delete file/link ${strFile}");
|
2014-12-21 17:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BUILD
|
|
|
|
#
|
|
|
|
# Creates missing paths and links and corrects ownership/mode on existing paths and links.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub build
|
|
|
|
{
|
|
|
|
my $self = shift; # Class hash
|
|
|
|
my $oManifestRef = shift; # Backup manifest
|
|
|
|
|
|
|
|
# Build paths/links in each restore path
|
|
|
|
foreach my $strPathKey (sort(keys ${$oManifestRef}{'backup:path'}))
|
|
|
|
{
|
|
|
|
my $strPath = ${$oManifestRef}{'backup:path'}{$strPathKey};
|
2014-12-21 17:11:17 +02:00
|
|
|
|
|
|
|
# Create all paths in the manifest that do not already exist
|
2014-12-23 22:03:06 +02:00
|
|
|
foreach my $strName (sort (keys ${$oManifestRef}{"${strPathKey}:path"}))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
|
|
|
# Skip the root path
|
|
|
|
if ($strName eq '.')
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create the Path
|
2014-12-30 22:41:43 +02:00
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, "${strPath}/${strName}"))
|
|
|
|
{
|
|
|
|
$self->{oFile}->path_create(PATH_DB_ABSOLUTE, "${strPath}/${strName}",
|
|
|
|
${$oManifestRef}{"${strPathKey}:path"}{$strName}{permission});
|
|
|
|
}
|
2014-12-21 17:11:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create all links in the manifest that do not already exist
|
2014-12-23 22:03:06 +02:00
|
|
|
if (defined(${$oManifestRef}{"${strPathKey}:link"}))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
foreach my $strName (sort (keys ${$oManifestRef}{"${strPathKey}:link"}))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2014-12-23 19:48:25 +02:00
|
|
|
if (!$self->{oFile}->exists(PATH_DB_ABSOLUTE, "${strPath}/${strName}"))
|
2014-12-21 17:11:17 +02:00
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
$self->{oFile}->link_create(PATH_DB_ABSOLUTE,
|
|
|
|
${$oManifestRef}{"${strPathKey}:link"}{$strName}{link_destination},
|
2014-12-23 19:48:25 +02:00
|
|
|
PATH_DB_ABSOLUTE, "${strPath}/${strName}");
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 22:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# RESTORE
|
|
|
|
#
|
|
|
|
# Takes a backup and restores it back to the original or a remapped location.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub restore
|
|
|
|
{
|
|
|
|
my $self = shift; # Class hash
|
|
|
|
|
|
|
|
# Make sure that Postgres is not running
|
|
|
|
if ($self->{oFile}->exists(PATH_DB_ABSOLUTE, $self->{strDbClusterPath} . '/' . FILE_POSTMASTER_PID))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'unable to restore while Postgres is running');
|
|
|
|
}
|
|
|
|
|
2014-12-24 01:52:38 +02:00
|
|
|
# Log the backup set to restore
|
|
|
|
&log(INFO, "Restoring backup set " . $self->{strBackupPath});
|
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
# Make sure the backup path is valid and load the manifest
|
|
|
|
my %oManifest;
|
|
|
|
$self->manifest_load(\%oManifest);
|
|
|
|
|
|
|
|
# Clean the restore paths
|
|
|
|
$self->clean(\%oManifest);
|
|
|
|
|
|
|
|
# Build paths/links in the restore paths
|
|
|
|
$self->build(\%oManifest);
|
|
|
|
|
|
|
|
# Assign the files in each path to a thread queue
|
|
|
|
my @oyRestoreQueue;
|
|
|
|
|
|
|
|
foreach my $strPathKey (sort(keys $oManifest{'backup:path'}))
|
|
|
|
{
|
2014-12-22 18:24:32 +02:00
|
|
|
if (defined($oManifest{"${strPathKey}:file"}))
|
|
|
|
{
|
2014-12-23 19:48:25 +02:00
|
|
|
$oyRestoreQueue[@oyRestoreQueue] = Thread::Queue->new();
|
2014-12-22 18:24:32 +02:00
|
|
|
|
|
|
|
foreach my $strName (sort (keys $oManifest{"${strPathKey}:file"}))
|
|
|
|
{
|
2014-12-23 19:48:25 +02:00
|
|
|
$oyRestoreQueue[@oyRestoreQueue - 1]->enqueue("${strPathKey}|${strName}");
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 19:48:25 +02:00
|
|
|
$oyRestoreQueue[@oyRestoreQueue - 1]->end();
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create threads to process the thread queues
|
|
|
|
my $oThreadGroup = new BackRest::ThreadGroup();
|
|
|
|
|
2014-12-30 22:41:43 +02:00
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $self->{iThreadTotal}; $iThreadIdx++)
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2014-12-23 19:48:25 +02:00
|
|
|
$oThreadGroup->add(threads->create(\&restore_thread, $self, $iThreadIdx, \@oyRestoreQueue, \%oManifest));
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$oThreadGroup->complete();
|
|
|
|
}
|
|
|
|
|
2014-12-23 19:48:25 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# RESTORE_THREAD
|
|
|
|
#
|
|
|
|
# Worker threads for the restore process.
|
|
|
|
####################################################################################################################################
|
2014-12-22 18:24:32 +02:00
|
|
|
sub restore_thread
|
|
|
|
{
|
2014-12-23 19:48:25 +02:00
|
|
|
my $self = shift; # Class hash
|
|
|
|
my $iThreadIdx = shift; # Defines the index of this thread
|
2014-12-23 22:03:06 +02:00
|
|
|
my $oyRestoreQueueRef = shift; # Restore queues
|
|
|
|
my $oManifestRef = shift; # Backup manifest
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2014-12-23 19:48:25 +02:00
|
|
|
my $iDirection = $iThreadIdx % 2 == 0 ? 1 : -1; # Size of files currently copied by this thread
|
|
|
|
my $oFileThread = $self->{oFile}->clone($iThreadIdx); # Thread local file object
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2014-12-23 18:48:51 +02:00
|
|
|
# Initialize the starting and current queue index based in the total number of threads in relation to this thread
|
2014-12-30 22:41:43 +02:00
|
|
|
my $iQueueStartIdx = int((@{$oyRestoreQueueRef} / $self->{iThreadTotal}) * $iThreadIdx);
|
2014-12-22 18:24:32 +02:00
|
|
|
my $iQueueIdx = $iQueueStartIdx;
|
|
|
|
|
2014-12-23 22:03:06 +02:00
|
|
|
# Set source compression
|
2014-12-30 22:41:43 +02:00
|
|
|
my $bSourceCompression = ${$oManifestRef}{'backup:option'}{compress} eq 'y' ? true : false;
|
2014-12-23 22:03:06 +02:00
|
|
|
|
2014-12-22 18:24:32 +02:00
|
|
|
# When a KILL signal is received, immediately abort
|
|
|
|
$SIG{'KILL'} = sub {threads->exit();};
|
|
|
|
|
2014-12-30 18:59:57 +02:00
|
|
|
# Get the current user and group to compare with stored permissions
|
|
|
|
my $strCurrentUser = getpwuid($<);
|
|
|
|
my $strCurrentGroup = getgrgid($();
|
|
|
|
|
2014-12-23 18:48:51 +02:00
|
|
|
# Loop through all the queues to restore files (exit when the original queue is reached
|
2014-12-22 18:24:32 +02:00
|
|
|
do
|
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
while (my $strMessage = ${$oyRestoreQueueRef}[$iQueueIdx]->dequeue())
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2014-12-30 18:59:57 +02:00
|
|
|
my $strSourcePath = (split(/\|/, $strMessage))[0]; # Source path from backup
|
|
|
|
my $strSection = "${strSourcePath}:file"; # Backup section with file info
|
2014-12-23 22:03:06 +02:00
|
|
|
my $strDestinationPath = ${$oManifestRef}{'backup:path'}{$strSourcePath}; # Destination path stored in manifest
|
2014-12-30 18:59:57 +02:00
|
|
|
$strSourcePath =~ s/\:/\//g; # Replace : with / in source path
|
|
|
|
my $strName = (split(/\|/, $strMessage))[1]; # Name of file to be restored
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2014-12-30 22:41:43 +02:00
|
|
|
# If the file is a reference to a previous backup and hardlinks are off, then fetch it from that backup
|
|
|
|
my $strReference = ${$oManifestRef}{'backup:option'}{hardlink} eq 'y' ? undef :
|
|
|
|
${$oManifestRef}{$strSection}{$strName}{reference};
|
|
|
|
|
2014-12-22 18:24:32 +02:00
|
|
|
# Generate destination file name
|
|
|
|
my $strDestinationFile = $oFileThread->path_get(PATH_DB_ABSOLUTE, "${strDestinationPath}/${strName}");
|
|
|
|
|
|
|
|
# If checksum is set the destination file already exists, try a checksum before copying
|
2014-12-23 22:03:06 +02:00
|
|
|
my $strChecksum = ${$oManifestRef}{$strSection}{$strName}{checksum};
|
2014-12-22 18:24:32 +02:00
|
|
|
|
|
|
|
if ($oFileThread->exists(PATH_DB_ABSOLUTE, $strDestinationFile))
|
|
|
|
{
|
|
|
|
if (defined($strChecksum) && $oFileThread->hash(PATH_DB_ABSOLUTE, $strDestinationFile) eq $strChecksum)
|
|
|
|
{
|
|
|
|
&log(DEBUG, "${strDestinationFile} exists and matches backup checksum ${strChecksum}");
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oFileThread->remove(PATH_DB_ABSOLUTE, $strDestinationFile);
|
|
|
|
}
|
|
|
|
|
2014-12-30 18:59:57 +02:00
|
|
|
# Set user and group if running as root (otherwise current user and group will be used for restore)
|
2014-12-22 18:24:32 +02:00
|
|
|
# Copy the file from the backup to the database
|
2014-12-30 22:41:43 +02:00
|
|
|
$oFileThread->copy(PATH_BACKUP_CLUSTER, (defined($strReference) ? $strReference : $self->{strBackupPath}) .
|
|
|
|
"/${strSourcePath}/${strName}" .
|
2014-12-23 22:03:06 +02:00
|
|
|
($bSourceCompression ? '.' . $oFileThread->{strCompressExtension} : ''),
|
2014-12-22 18:24:32 +02:00
|
|
|
PATH_DB_ABSOLUTE, $strDestinationFile,
|
2014-12-23 22:03:06 +02:00
|
|
|
$bSourceCompression, # Source is compressed based on backup settings
|
2014-12-23 18:48:51 +02:00
|
|
|
undef, undef,
|
2014-12-23 22:03:06 +02:00
|
|
|
${$oManifestRef}{$strSection}{$strName}{modification_time},
|
2014-12-30 18:59:57 +02:00
|
|
|
${$oManifestRef}{$strSection}{$strName}{permission},
|
2014-12-31 18:20:46 +02:00
|
|
|
undef,
|
|
|
|
${$oManifestRef}{$strSection}{$strName}{user},
|
|
|
|
${$oManifestRef}{$strSection}{$strName}{group});
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 18:48:51 +02:00
|
|
|
# Even number threads move up when they have finished a queue, odd numbered threads move down
|
2014-12-22 18:24:32 +02:00
|
|
|
$iQueueIdx += $iDirection;
|
|
|
|
|
2014-12-23 18:48:51 +02:00
|
|
|
# Reset the queue index when it goes over or under the number of queues
|
2014-12-22 18:24:32 +02:00
|
|
|
if ($iQueueIdx < 0)
|
|
|
|
{
|
2014-12-23 22:03:06 +02:00
|
|
|
$iQueueIdx = @{$oyRestoreQueueRef} - 1;
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
2014-12-23 22:03:06 +02:00
|
|
|
elsif ($iQueueIdx >= @{$oyRestoreQueueRef})
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
|
|
|
$iQueueIdx = 0;
|
|
|
|
}
|
2014-12-19 19:49:56 +02:00
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
while ($iQueueIdx != $iQueueStartIdx);
|
|
|
|
|
|
|
|
&log(DEBUG, "thread ${iThreadIdx} exiting");
|
2014-12-18 18:42:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|