1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00
pgbackrest/lib/pgBackRest/RestoreFile.pm

239 lines
8.4 KiB
Perl
Raw Normal View History

####################################################################################################################################
# RESTORE FILE MODULE
####################################################################################################################################
New simpler configuration and consistent project/exe/path naming. * The repo-path option now always refers to the repository where backups and archive are stored, whether local or remote, so the repo-remote-path option has been removed. The new spool-path option can be used to define a location for queueing WAL segments when archiving asynchronously. Otherwise, a local repository is no longer required. * Implemented a new config format which should be far simpler to use. See the User Guide and Configuration Reference for details but for a simple configuration all options can now be placed in the stanza section. Options that are shared between stanzas can be placed in the [global] section. More complex configurations can still make use of command sections though this should be a rare use case. * The default configuration filename is now pgbackrest.conf instead of pg_backrest.conf. This was done for consistency with other naming changes but also to prevent old config files from being loaded accidentally. * The default repository name was changed from /var/lib/backup to /var/lib/pgbackrest. * Lock files are now stored in /tmp/pgbackrest by default. These days /run/pgbackrest would be the preferred location but that would require init scripts which are not part of this release. The lock-path option can be used to configure the lock directory. * Log files are now stored in /var/log/pgbackrest by default and no longer have the date appended so they can be managed with logrotate. The log-path option can be used to configure the lock directory. * Executable filename changed from pg_backrest to pgbackrest.
2016-04-14 15:30:54 +02:00
package pgBackRest::RestoreFile;
use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use Exporter qw(import);
our @EXPORT = qw();
use Fcntl qw(O_WRONLY O_CREAT O_TRUNC);
use File::Basename qw(dirname);
use File::stat qw(lstat);
New simpler configuration and consistent project/exe/path naming. * The repo-path option now always refers to the repository where backups and archive are stored, whether local or remote, so the repo-remote-path option has been removed. The new spool-path option can be used to define a location for queueing WAL segments when archiving asynchronously. Otherwise, a local repository is no longer required. * Implemented a new config format which should be far simpler to use. See the User Guide and Configuration Reference for details but for a simple configuration all options can now be placed in the stanza section. Options that are shared between stanzas can be placed in the [global] section. More complex configurations can still make use of command sections though this should be a rare use case. * The default configuration filename is now pgbackrest.conf instead of pg_backrest.conf. This was done for consistency with other naming changes but also to prevent old config files from being loaded accidentally. * The default repository name was changed from /var/lib/backup to /var/lib/pgbackrest. * Lock files are now stored in /tmp/pgbackrest by default. These days /run/pgbackrest would be the preferred location but that would require init scripts which are not part of this release. The lock-path option can be used to configure the lock directory. * Log files are now stored in /var/log/pgbackrest by default and no longer have the date appended so they can be managed with logrotate. The log-path option can be used to configure the lock directory. * Executable filename changed from pg_backrest to pgbackrest.
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;
use pgBackRest::FileCommon;
New simpler configuration and consistent project/exe/path naming. * The repo-path option now always refers to the repository where backups and archive are stored, whether local or remote, so the repo-remote-path option has been removed. The new spool-path option can be used to define a location for queueing WAL segments when archiving asynchronously. Otherwise, a local repository is no longer required. * Implemented a new config format which should be far simpler to use. See the User Guide and Configuration Reference for details but for a simple configuration all options can now be placed in the stanza section. Options that are shared between stanzas can be placed in the [global] section. More complex configurations can still make use of command sections though this should be a rare use case. * The default configuration filename is now pgbackrest.conf instead of pg_backrest.conf. This was done for consistency with other naming changes but also to prevent old config files from being loaded accidentally. * The default repository name was changed from /var/lib/backup to /var/lib/pgbackrest. * Lock files are now stored in /tmp/pgbackrest by default. These days /run/pgbackrest would be the preferred location but that would require init scripts which are not part of this release. The lock-path option can be used to configure the lock directory. * Log files are now stored in /var/log/pgbackrest by default and no longer have the date appended so they can be managed with logrotate. The log-path option can be used to configure the lock directory. * Executable filename changed from pg_backrest to pgbackrest.
2016-04-14 15:30:54 +02:00
use pgBackRest::Manifest;
2016-09-06 15:35:02 +02:00
use pgBackRest::Protocol::Common;
####################################################################################################################################
# restoreFile
#
# Restores a single file.
####################################################################################################################################
sub restoreFile
{
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
2016-09-06 15:35:02 +02:00
$oFile, # File object
$strRepoFile,
$strDbFile,
$strReference,
$lSize,
$lModificationTime,
$strMode,
$strUser,
$strGroup,
$strChecksum,
$bZero,
$lCopyTimeStart, # Backup start time - used for size/timestamp deltas
$bDelta, # Is restore a delta?
$bForce, # Force flag
$strBackupPath, # Backup path
$bSourceCompression, # Is the source compressed?
) =
logDebugParam
(
2016-09-06 15:35:02 +02:00
__PACKAGE__ . '::restoreFile', \@_,
{name => 'oFile', trace => true},
2016-09-06 15:35:02 +02:00
{name => &OP_PARAM_REPO_FILE, trace => true},
{name => &OP_PARAM_DB_FILE, trace => true},
{name => &OP_PARAM_REFERENCE, required => false, trace => true},
{name => &OP_PARAM_SIZE, trace => true},
{name => &OP_PARAM_MODIFICATION_TIME, trace => true},
{name => &OP_PARAM_MODE, trace => true},
{name => &OP_PARAM_USER, trace => true},
{name => &OP_PARAM_GROUP, trace => true},
{name => &OP_PARAM_CHECKSUM, required => false, trace => true},
{name => &OP_PARAM_ZERO, required => false, default => false, trace => true},
{name => &OP_PARAM_COPY_TIME_START, trace => true},
{name => &OP_PARAM_DELTA, trace => true},
{name => &OP_PARAM_FORCE, trace => true},
{name => &OP_PARAM_BACKUP_PATH, trace => true},
{name => &OP_PARAM_SOURCE_COMPRESSION, trace => true},
);
# Copy flag and log message
my $bCopy = true;
2016-09-06 15:35:02 +02:00
if ($bZero)
{
$bCopy = false;
# Open the file truncating to zero bytes in case it already exists
2016-09-06 15:35:02 +02:00
my $hFile = fileOpen($strDbFile, O_WRONLY | O_CREAT | O_TRUNC, $strMode);
# Now truncate to the original size. This will create a sparse file which is very efficient for this use case.
2016-09-06 15:35:02 +02:00
truncate($hFile, $lSize);
# Sync the file
$hFile->sync()
2016-09-06 15:35:02 +02:00
or confess &log(ERROR, "unable to sync ${strDbFile}", ERROR_FILE_SYNC);
# Close the file
close($hFile)
2016-09-06 15:35:02 +02:00
or confess &log(ERROR, "unable to close ${strDbFile}", ERROR_FILE_CLOSE);
# Fix the timestamp - not really needed in this case but good for testing
2016-09-06 15:35:02 +02:00
utime($lModificationTime, $lModificationTime, $strDbFile)
or confess &log(ERROR, "unable to set time for ${strDbFile}");
# Set file ownership
2016-09-06 15:35:02 +02:00
$oFile->owner(PATH_DB_ABSOLUTE, $strDbFile, $strUser, $strGroup);
}
2016-09-06 15:35:02 +02:00
elsif ($oFile->exists(PATH_DB_ABSOLUTE, $strDbFile))
{
# Perform delta if requested
if ($bDelta)
{
# If force then use size/timestamp delta
if ($bForce)
{
2016-09-06 15:35:02 +02:00
my $oStat = lstat($strDbFile);
# Make sure that timestamp/size are equal and that timestamp is before the copy start time of the backup
2016-09-06 15:35:02 +02:00
if (defined($oStat) && $oStat->size == $lSize &&
$oStat->mtime == $lModificationTime && $oStat->mtime < $lCopyTimeStart)
{
$bCopy = false;
}
}
else
{
2016-09-06 15:35:02 +02:00
my ($strActualChecksum, $lActualSize) = $oFile->hashSize(PATH_DB_ABSOLUTE, $strDbFile);
2016-09-06 15:35:02 +02:00
if ($lActualSize == $lSize && ($lSize == 0 || $strActualChecksum eq $strChecksum))
{
# Even if hash is the same set the time back to backup time. This helps with unit testing, but also
# presents a pristine version of the database after restore.
2016-09-06 15:35:02 +02:00
utime($lModificationTime, $lModificationTime, $strDbFile)
or confess &log(ERROR, "unable to set time for ${strDbFile}");
$bCopy = false;
}
}
}
}
# Copy the file from the backup to the database
if ($bCopy)
{
2016-09-06 15:35:02 +02:00
my ($bCopyResult, $strCopyChecksum, $lCopySize) = $oFile->copy(
PATH_BACKUP_CLUSTER, (defined($strReference) ? $strReference : $strBackupPath) .
"/${strRepoFile}" . ($bSourceCompression ? '.' . $oFile->{strCompressExtension} : ''),
PATH_DB_ABSOLUTE, $strDbFile,
$bSourceCompression,
undef, undef,
$lModificationTime, $strMode,
undef,
$strUser, $strGroup);
if ($lCopySize != 0 && $strCopyChecksum ne $strChecksum)
{
confess &log(ERROR, "error restoring ${strDbFile}: actual checksum ${strCopyChecksum} " .
"does not match expected checksum ${strChecksum}", ERROR_CHECKSUM);
}
}
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'bCopy', value => $bCopy, trace => true}
);
}
push @EXPORT, qw(restoreFile);
####################################################################################################################################
# restoreLog
#
# Log a restored file.
####################################################################################################################################
sub restoreLog
{
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strDbFile,
$bCopy,
$lSize,
$lModificationTime,
$strChecksum,
$bZero,
$bForce,
$lSizeTotal,
$lSizeCurrent,
$iLocalId,
2016-09-06 15:35:02 +02:00
) =
logDebugParam
(
__PACKAGE__ . '::restoreLog', \@_,
{name => &OP_PARAM_DB_FILE},
{name => 'bCopy'},
{name => &OP_PARAM_SIZE},
{name => &OP_PARAM_MODIFICATION_TIME},
{name => &OP_PARAM_CHECKSUM, required => false},
{name => &OP_PARAM_ZERO, required => false, default => false},
{name => &OP_PARAM_FORCE},
{name => 'lSizeTotal'},
{name => 'lSizeCurrent'},
{name => 'iLocalId', required => false},
2016-09-06 15:35:02 +02:00
);
# If the file was not copied then create a log entry to explain why
my $strLog;
if (!$bCopy && !$bZero)
{
if ($bForce)
{
$strLog = 'exists and matches size ' . $lSize . ' and modification time ' . $lModificationTime;
}
else
{
2016-09-06 15:35:02 +02:00
$strLog = 'exists and ' . ($lSize == 0 ? 'is zero size' : 'matches backup');
}
}
# Log the restore
2016-09-06 15:35:02 +02:00
$lSizeCurrent += $lSize;
&log($bCopy ? INFO : DETAIL,
'restore' . ($bZero ? ' zeroed' : '') .
2016-09-06 15:35:02 +02:00
" file ${strDbFile}" . (defined($strLog) ? " - ${strLog}" : '') .
' (' . fileSizeFormat($lSize) .
($lSizeTotal > 0 ? ', ' . int($lSizeCurrent * 100 / $lSizeTotal) . '%' : '') . ')' .
($lSize != 0 && !$bZero ? " checksum ${strChecksum}" : ''), undef, undef, undef, $iLocalId);
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'lSizeCurrent', value => $lSizeCurrent, trace => true}
);
}
2016-09-06 15:35:02 +02:00
push @EXPORT, qw(restoreLog);
1;