2015-04-07 13:34:37 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# RESTORE FILE MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::RestoreFile;
|
2015-04-07 13:34:37 +02:00
|
|
|
|
|
|
|
use threads;
|
|
|
|
use threads::shared;
|
|
|
|
use Thread::Queue;
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
use Exporter qw(import);
|
2015-08-29 20:20:46 +02:00
|
|
|
our @EXPORT = qw();
|
2016-05-11 15:21:39 +02:00
|
|
|
use Fcntl qw(O_WRONLY O_CREAT O_TRUNC);
|
2015-04-07 13:34:37 +02:00
|
|
|
use File::Basename qw(dirname);
|
|
|
|
use File::stat qw(lstat);
|
|
|
|
|
|
|
|
use lib dirname($0);
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Config::Config;
|
|
|
|
use pgBackRest::File;
|
2016-05-11 15:21:39 +02:00
|
|
|
use pgBackRest::FileCommon;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Manifest;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# restoreFile
|
|
|
|
#
|
|
|
|
# Restores a single file.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub restoreFile
|
|
|
|
{
|
2015-05-07 23:56:56 +02:00
|
|
|
my $oFileHash = shift; # File to restore
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$lCopyTimeBegin, # Time that the backup begain - used for size/timestamp deltas
|
|
|
|
$bDelta, # Is restore a delta?
|
|
|
|
$bForce, # Force flag
|
|
|
|
$strBackupPath, # Backup path
|
|
|
|
$bSourceCompression, # Is the source compressed?
|
|
|
|
$strCurrentUser, # Current OS user
|
|
|
|
$strCurrentGroup, # Current OS group
|
|
|
|
$oFile, # File object
|
|
|
|
$lSizeTotal, # Total size of files to be restored
|
|
|
|
$lSizeCurrent # Current size of files restored
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->restoreFile', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'lCopyTimeBegin', trace => true},
|
|
|
|
{name => 'bDelta', trace => true},
|
|
|
|
{name => 'bForce', trace => true},
|
|
|
|
{name => 'strBackupPath', trace => true},
|
|
|
|
{name => 'bSourceCompression', trace => true},
|
|
|
|
{name => 'strCurrentUser', trace => true},
|
|
|
|
{name => 'strCurrentGroup', trace => true},
|
|
|
|
{name => 'oFile', trace => true},
|
2016-04-15 04:50:02 +02:00
|
|
|
{name => 'lSizeTotal', default => 0, trace => true},
|
|
|
|
{name => 'lSizeCurrent', required => false, trace => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Add the size of the current file to keep track of percent complete
|
|
|
|
if ($lSizeTotal > 0)
|
|
|
|
{
|
|
|
|
$lSizeCurrent += $$oFileHash{size};
|
|
|
|
}
|
2015-05-07 23:56:56 +02:00
|
|
|
|
|
|
|
# Copy flag and log message
|
|
|
|
my $bCopy = true;
|
2016-05-11 15:21:39 +02:00
|
|
|
my $bZero = false;
|
2015-05-07 23:56:56 +02:00
|
|
|
my $strLog;
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2016-05-11 15:21:39 +02:00
|
|
|
if (defined($$oFileHash{zero}) && $$oFileHash{zero})
|
|
|
|
{
|
|
|
|
$bCopy = false;
|
|
|
|
$bZero = true;
|
|
|
|
|
|
|
|
# Open the file truncating to zero bytes in case it already exists
|
2016-05-26 21:04:18 +02:00
|
|
|
my $hFile = fileOpen($$oFileHash{db_file}, O_WRONLY | O_CREAT | O_TRUNC, $$oFileHash{mode});
|
2016-05-11 15:21:39 +02:00
|
|
|
|
|
|
|
# Now truncate to the original size. This will create a sparse file which is very efficient for this use case.
|
|
|
|
truncate($hFile, $$oFileHash{size});
|
|
|
|
|
|
|
|
# Sync the file
|
|
|
|
$hFile->sync()
|
|
|
|
or confess &log(ERROR, "unable to sync $$oFileHash{db_file}", ERROR_FILE_SYNC);
|
|
|
|
|
|
|
|
# Close the file
|
|
|
|
close($hFile)
|
|
|
|
or confess &log(ERROR, "unable to close $$oFileHash{db_file}", ERROR_FILE_CLOSE);
|
|
|
|
|
|
|
|
# Fix the timestamp - not really needed in this case but good for testing
|
|
|
|
utime($$oFileHash{modification_time}, $$oFileHash{modification_time}, $$oFileHash{db_file})
|
|
|
|
or confess &log(ERROR, "unable to set time for $$oFileHash{db_file}");
|
|
|
|
|
|
|
|
# Set file mode
|
|
|
|
chmod(oct($$oFileHash{mode}), $$oFileHash{db_file})
|
|
|
|
or confess &log(ERROR, "unable to set mode for $$oFileHash{db_file}");
|
|
|
|
|
|
|
|
# Set file ownership
|
|
|
|
$oFile->owner(PATH_DB_ABSOLUTE, $$oFileHash{db_file}, $$oFileHash{user}, $$oFileHash{group});
|
|
|
|
}
|
|
|
|
elsif ($oFile->exists(PATH_DB_ABSOLUTE, $$oFileHash{db_file}))
|
2015-04-07 13:34:37 +02:00
|
|
|
{
|
|
|
|
# Perform delta if requested
|
|
|
|
if ($bDelta)
|
|
|
|
{
|
|
|
|
# If force then use size/timestamp delta
|
|
|
|
if ($bForce)
|
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my $oStat = lstat($$oFileHash{db_file});
|
2015-04-07 13:34:37 +02:00
|
|
|
|
|
|
|
# Make sure that timestamp/size are equal and that timestamp is before the copy start time of the backup
|
2015-05-07 23:56:56 +02:00
|
|
|
if (defined($oStat) && $oStat->size == $$oFileHash{size} &&
|
|
|
|
$oStat->mtime == $$oFileHash{modification_time} && $oStat->mtime < $lCopyTimeBegin)
|
2015-04-07 13:34:37 +02:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
$strLog = 'exists and matches size ' . $oStat->size . ' and modification time ' . $oStat->mtime;
|
2015-05-07 23:56:56 +02:00
|
|
|
$bCopy = false;
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
my ($strChecksum, $lSize) = $oFile->hashSize(PATH_DB_ABSOLUTE, $$oFileHash{db_file});
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2015-05-07 23:56:56 +02:00
|
|
|
if ($lSize == $$oFileHash{size} && ($lSize == 0 || $strChecksum eq $$oFileHash{checksum}))
|
2015-04-07 13:34:37 +02:00
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
$strLog = 'exists and ' . ($lSize == 0 ? 'is zero size' : "matches backup");
|
2015-04-07 13:34:37 +02:00
|
|
|
|
|
|
|
# Even if hash is the same set the time back to backup time. This helps with unit testing, but also
|
2015-05-07 23:56:56 +02:00
|
|
|
# presents a pristine version of the database after restore.
|
2016-04-15 04:50:02 +02:00
|
|
|
utime($$oFileHash{modification_time}, $$oFileHash{modification_time}, $$oFileHash{db_file})
|
|
|
|
or confess &log(ERROR, "unable to set time for $$oFileHash{db_file}");
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2015-05-07 23:56:56 +02:00
|
|
|
$bCopy = false;
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copy the file from the backup to the database
|
2015-05-07 23:56:56 +02:00
|
|
|
if ($bCopy)
|
2015-04-07 13:34:37 +02:00
|
|
|
{
|
2015-05-07 23:56:56 +02:00
|
|
|
my ($bCopyResult, $strCopyChecksum, $lCopySize) =
|
|
|
|
$oFile->copy(PATH_BACKUP_CLUSTER, (defined($$oFileHash{reference}) ? $$oFileHash{reference} : $strBackupPath) .
|
2016-04-15 04:50:02 +02:00
|
|
|
"/$$oFileHash{repo_file}" .
|
2015-05-07 23:56:56 +02:00
|
|
|
($bSourceCompression ? '.' . $oFile->{strCompressExtension} : ''),
|
2016-04-15 04:50:02 +02:00
|
|
|
PATH_DB_ABSOLUTE, $$oFileHash{db_file},
|
2015-05-07 23:56:56 +02:00
|
|
|
$bSourceCompression, # Source is compressed based on backup settings
|
|
|
|
undef, undef,
|
|
|
|
$$oFileHash{modification_time},
|
|
|
|
$$oFileHash{mode},
|
|
|
|
undef,
|
|
|
|
$$oFileHash{user},
|
|
|
|
$$oFileHash{group});
|
|
|
|
|
|
|
|
if ($lCopySize != 0 && $strCopyChecksum ne $$oFileHash{checksum})
|
|
|
|
{
|
2016-04-15 04:50:02 +02:00
|
|
|
confess &log(ERROR, "error restoring $$oFileHash{db_file}: actual checksum ${strCopyChecksum} " .
|
2015-05-07 23:56:56 +02:00
|
|
|
"does not match expected checksum $$oFileHash{checksum}", ERROR_CHECKSUM);
|
|
|
|
}
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
2015-05-07 23:56:56 +02:00
|
|
|
|
2016-04-15 04:50:02 +02:00
|
|
|
# Log the restore
|
|
|
|
&log($bCopy ? INFO : DETAIL,
|
2016-05-11 15:21:39 +02:00
|
|
|
'restore' . ($bZero ? ' zeroed' : '') .
|
|
|
|
" file $$oFileHash{db_file}" . (defined($strLog) ? " - ${strLog}" : '') .
|
2016-04-15 04:50:02 +02:00
|
|
|
' (' . fileSizeFormat($$oFileHash{size}) .
|
|
|
|
($lSizeTotal > 0 ? ', ' . int($lSizeCurrent * 100 / $lSizeTotal) . '%' : '') . ')' .
|
2016-05-11 15:21:39 +02:00
|
|
|
($$oFileHash{size} != 0 && !$bZero ? " checksum $$oFileHash{checksum}" : ''));
|
2015-05-07 23:56:56 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'lSizeCurrent', value => $lSizeCurrent, trace => true}
|
|
|
|
);
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
push @EXPORT, qw(restoreFile);
|
2015-04-07 13:34:37 +02:00
|
|
|
|
|
|
|
1;
|