2017-01-11 02:54:51 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CHECK MODULE
|
|
|
|
####################################################################################################################################
|
|
|
|
package pgBackRest::Check::Check;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
use English '-no_match_vars';
|
|
|
|
|
2017-06-21 14:02:21 +02:00
|
|
|
use pgBackRest::Archive::Common;
|
|
|
|
use pgBackRest::Archive::Get::Get;
|
2017-05-15 22:01:00 +02:00
|
|
|
use pgBackRest::Backup::Info;
|
2017-01-11 02:54:51 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::Wait;
|
|
|
|
use pgBackRest::Config::Config;
|
|
|
|
use pgBackRest::Db;
|
2017-05-15 17:12:14 +02:00
|
|
|
use pgBackRest::Protocol::Helper;
|
2017-06-09 23:51:41 +02:00
|
|
|
use pgBackRest::Protocol::Storage::Helper;
|
2017-01-11 02:54:51 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# constructor
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '->new');
|
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# process
|
|
|
|
#
|
|
|
|
# Validates the database configuration and checks that the archive logs can be read by backup. This will alert the user to any
|
|
|
|
# misconfiguration, particularly of archiving, that would result in the inability of a backup to complete (e.g waiting at the end
|
|
|
|
# until it times out because it could not find the WAL file).
|
|
|
|
####################################################################################################################################
|
|
|
|
sub process
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my $strOperation = logDebugParam(__PACKAGE__ . '->process');
|
|
|
|
|
2017-12-13 18:16:27 +02:00
|
|
|
# Initialize the local variables
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iArchiveTimeout = cfgOption(CFGOPT_ARCHIVE_TIMEOUT);
|
2017-01-11 02:54:51 +02:00
|
|
|
|
|
|
|
my $iResult = 0;
|
|
|
|
my $strResultMessage = undef;
|
|
|
|
|
|
|
|
my $strArchiveId = undef;
|
|
|
|
my $strArchiveFile = undef;
|
|
|
|
my $strWalSegment = undef;
|
|
|
|
|
2017-12-13 18:16:27 +02:00
|
|
|
# Get the master database object to test to see if the manifest can be built
|
|
|
|
my ($oDb) = dbMasterGet();
|
|
|
|
|
|
|
|
# Get the databse version to pass to the manifest constructor and the system-id in the event of a failure
|
|
|
|
my ($strDbVersion, $iControlVersion, $iCatalogVersion, $ullDbSysId) = $oDb->info();
|
|
|
|
|
2017-01-11 02:54:51 +02:00
|
|
|
# Turn off console logging to control when to display the error
|
|
|
|
logLevelSet(undef, OFF);
|
|
|
|
|
2017-12-13 18:16:27 +02:00
|
|
|
# Loop through all defined databases and attempt to build a manifest
|
|
|
|
for (my $iRemoteIdx = 1; $iRemoteIdx <= cfgOptionIndexTotal(CFGOPT_DB_HOST); $iRemoteIdx++)
|
2017-01-11 02:54:51 +02:00
|
|
|
{
|
2017-12-13 18:16:27 +02:00
|
|
|
# Make sure a db is defined for this index
|
|
|
|
if (cfgOptionTest(cfgOptionIdFromIndex(CFGOPT_DB_PATH, $iRemoteIdx)) ||
|
|
|
|
cfgOptionTest(cfgOptionIdFromIndex(CFGOPT_DB_HOST, $iRemoteIdx)))
|
|
|
|
{
|
|
|
|
eval
|
|
|
|
{
|
|
|
|
# Passing file location dev/null so that the save will fail if it is ever attempted. Pass a miscellaneus value for
|
|
|
|
# encryption key since the file will not be saved.
|
|
|
|
my $oBackupManifest = new pgBackRest::Manifest("/dev/null/manifest.chk",
|
|
|
|
{bLoad => false, strDbVersion => $strDbVersion,
|
|
|
|
strCipherPass => 'x',
|
|
|
|
strCipherPassSub => 'x'});
|
|
|
|
|
|
|
|
$oBackupManifest->build(storageDb({iRemoteIdx => $iRemoteIdx}),
|
|
|
|
cfgOption(cfgOptionIdFromIndex(CFGOPT_DB_PATH, $iRemoteIdx)), undef, cfgOption(CFGOPT_ONLINE));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
or do
|
|
|
|
{
|
|
|
|
# Capture error information
|
|
|
|
$strResultMessage = "Database: ${strDbVersion} ${ullDbSysId} " . exceptionMessage($EVAL_ERROR) .
|
|
|
|
(($iResult != 0) ? "\n[$iResult] : $strResultMessage" : "");
|
|
|
|
$iResult = exceptionCode($EVAL_ERROR);
|
|
|
|
};
|
|
|
|
}
|
2017-01-11 02:54:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:16:27 +02:00
|
|
|
# Reset the console logging
|
|
|
|
logLevelSet(undef, cfgOption(CFGOPT_LOG_LEVEL_CONSOLE));
|
|
|
|
|
|
|
|
# If the manifest builds are ok, then proceed with the other checks
|
2017-01-11 02:54:51 +02:00
|
|
|
if ($iResult == 0)
|
|
|
|
{
|
2017-12-13 18:16:27 +02:00
|
|
|
# Reinitialize the database object in order to check the configured replicas. This will throw an error if at least one is not
|
|
|
|
# able to be connected to and warnings for any that cannot be properly connected to.
|
|
|
|
($oDb) = dbObjectGet();
|
|
|
|
|
|
|
|
# Validate the database configuration
|
|
|
|
$oDb->configValidate();
|
|
|
|
|
|
|
|
# Turn off console logging to control when to display the error
|
|
|
|
logLevelSet(undef, OFF);
|
|
|
|
|
|
|
|
# Check backup.info - if the archive check fails below (e.g --no-archive-check set) then at least know backup.info succeeded
|
2017-01-11 02:54:51 +02:00
|
|
|
eval
|
|
|
|
{
|
2017-12-13 18:16:27 +02:00
|
|
|
# Check that the backup info file is written and is valid for the current database of the stanza
|
|
|
|
$self->backupInfoCheck();
|
2017-01-11 02:54:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-12-13 18:16:27 +02:00
|
|
|
# If there is an unhandled error then confess
|
2017-01-11 02:54:51 +02:00
|
|
|
or do
|
|
|
|
{
|
2017-01-27 16:56:26 +02:00
|
|
|
# Capture error information
|
|
|
|
$iResult = exceptionCode($EVAL_ERROR);
|
2017-10-16 16:47:31 +02:00
|
|
|
$strResultMessage = exceptionMessage($EVAL_ERROR);
|
2017-01-11 02:54:51 +02:00
|
|
|
};
|
|
|
|
|
2017-12-13 18:16:27 +02:00
|
|
|
# Check archive.info
|
|
|
|
if ($iResult == 0)
|
2017-01-11 02:54:51 +02:00
|
|
|
{
|
2017-12-13 18:16:27 +02:00
|
|
|
eval
|
|
|
|
{
|
|
|
|
# Check that the archive info file is written and is valid for the current database of the stanza
|
|
|
|
($strArchiveId) = new pgBackRest::Archive::Get::Get()->getCheck();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
or do
|
|
|
|
{
|
|
|
|
# Capture error information
|
|
|
|
$iResult = exceptionCode($EVAL_ERROR);
|
|
|
|
$strResultMessage = exceptionMessage($EVAL_ERROR);
|
|
|
|
};
|
2017-01-11 02:54:51 +02:00
|
|
|
}
|
2017-12-13 18:16:27 +02:00
|
|
|
|
|
|
|
# If able to get the archive id then force archiving and check the arrival of the archived WAL file with the time specified
|
|
|
|
if ($iResult == 0 && !$oDb->isStandby())
|
2017-01-11 02:54:51 +02:00
|
|
|
{
|
2017-12-13 18:16:27 +02:00
|
|
|
$strWalSegment = $oDb->walSwitch();
|
|
|
|
|
|
|
|
eval
|
|
|
|
{
|
|
|
|
$strArchiveFile = walSegmentFind(storageRepo(), $strArchiveId, $strWalSegment, $iArchiveTimeout);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
# If this is a backrest error then capture the code and message else confess
|
|
|
|
or do
|
|
|
|
{
|
|
|
|
# Capture error information
|
|
|
|
$iResult = exceptionCode($EVAL_ERROR);
|
|
|
|
$strResultMessage = exceptionMessage($EVAL_ERROR);
|
|
|
|
};
|
|
|
|
}
|
2017-01-11 02:54:51 +02:00
|
|
|
|
2017-12-13 18:16:27 +02:00
|
|
|
# Reset the console logging
|
|
|
|
logLevelSet(undef, cfgOption(CFGOPT_LOG_LEVEL_CONSOLE));
|
|
|
|
}
|
2017-01-11 02:54:51 +02:00
|
|
|
|
|
|
|
# If the archiving was successful and backup.info check did not error in an unexpected way, then indicate success
|
|
|
|
# Else, log the error.
|
|
|
|
if ($iResult == 0)
|
|
|
|
{
|
|
|
|
if (!$oDb->isStandby())
|
|
|
|
{
|
|
|
|
&log(INFO,
|
|
|
|
"WAL segment ${strWalSegment} successfully stored in the archive at '" .
|
2017-06-09 23:51:41 +02:00
|
|
|
storageRepo()->pathGet(STORAGE_REPO_ARCHIVE . "/$strArchiveId/${strArchiveFile}") . "'");
|
2017-01-11 02:54:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-01 18:29:34 +02:00
|
|
|
&log(INFO, 'switch ' . $oDb->walId() . ' cannot be performed on the standby, all other checks passed successfully');
|
2017-01-11 02:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-13 18:16:27 +02:00
|
|
|
# Throw the captured error
|
2017-01-11 02:54:51 +02:00
|
|
|
&log(ERROR, $strResultMessage, $iResult);
|
|
|
|
|
2017-09-01 18:29:34 +02:00
|
|
|
# If a WAL switch was attempted, then alert the user that the WAL that did not reach the archive
|
2017-12-13 18:16:27 +02:00
|
|
|
if (defined($strWalSegment) && !defined($strArchiveFile))
|
2017-01-11 02:54:51 +02:00
|
|
|
{
|
|
|
|
&log(WARN,
|
|
|
|
"WAL segment ${strWalSegment} did not reach the archive:" . (defined($strArchiveId) ? $strArchiveId : '') . "\n" .
|
2017-11-06 19:51:12 +02:00
|
|
|
"HINT: Check the archive_command to ensure that all options are correct (especially --stanza).\n" .
|
2017-06-09 23:51:41 +02:00
|
|
|
"HINT: Check the PostgreSQL server log for errors.");
|
2017-01-11 02:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'iResult', value => $iResult, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# backupInfoCheck
|
|
|
|
#
|
|
|
|
# Check the backup.info file, if it exists, to confirm the DB version, system-id, control and catalog numbers match the database.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backupInfoCheck
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strDbVersion,
|
|
|
|
$iControlVersion,
|
|
|
|
$iCatalogVersion,
|
2017-06-09 23:51:41 +02:00
|
|
|
$ullDbSysId,
|
2017-01-11 02:54:51 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2017-04-03 16:42:55 +02:00
|
|
|
__PACKAGE__ . '->backupInfoCheck', \@_,
|
2017-01-11 02:54:51 +02:00
|
|
|
{name => 'strDbVersion', required => false},
|
|
|
|
{name => 'iControlVersion', required => false},
|
|
|
|
{name => 'iCatalogVersion', required => false},
|
|
|
|
{name => 'ullDbSysId', required => false}
|
|
|
|
);
|
|
|
|
|
|
|
|
# If the db info are not passed, then we need to retrieve the database information
|
|
|
|
my $iDbHistoryId;
|
|
|
|
|
|
|
|
if (!defined($strDbVersion) || !defined($iControlVersion) || !defined($iCatalogVersion) || !defined($ullDbSysId))
|
|
|
|
{
|
|
|
|
# get DB info for comparison
|
|
|
|
($strDbVersion, $iControlVersion, $iCatalogVersion, $ullDbSysId) = dbMasterGet()->info();
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
if (!isRepoLocal())
|
2017-01-11 02:54:51 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$iDbHistoryId = protocolGet(CFGOPTVAL_REMOTE_TYPE_BACKUP)->cmdExecute(
|
2017-01-11 02:54:51 +02:00
|
|
|
OP_CHECK_BACKUP_INFO_CHECK, [$strDbVersion, $iControlVersion, $iCatalogVersion, $ullDbSysId]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
$iDbHistoryId = (new pgBackRest::Backup::Info(storageRepo()->pathGet(STORAGE_REPO_BACKUP)))->check(
|
2017-01-11 02:54:51 +02:00
|
|
|
$strDbVersion, $iControlVersion, $iCatalogVersion, $ullDbSysId);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'iDbHistoryId', value => $iDbHistoryId, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|