2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# DB MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Db;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
use strict;
|
2015-03-03 07:57:20 +02:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2014-07-28 01:13:23 +03:00
|
|
|
|
2015-08-09 00:22:38 +02:00
|
|
|
use DBD::Pg ':async';
|
2015-08-05 18:32:12 +02:00
|
|
|
use DBI;
|
2015-06-14 00:25:49 +02:00
|
|
|
use Exporter qw(import);
|
2015-08-08 23:11:20 +02:00
|
|
|
our @EXPORT = qw();
|
2015-06-14 00:25:49 +02:00
|
|
|
use Fcntl qw(O_RDONLY);
|
2015-06-30 04:07:42 +02:00
|
|
|
use File::Basename qw(dirname);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2016-08-12 04:35:24 +02:00
|
|
|
use pgBackRest::DbVersion;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Common::Wait;
|
|
|
|
use pgBackRest::Config::Config;
|
2016-05-11 15:21:39 +02:00
|
|
|
use pgBackRest::Manifest;
|
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;
|
|
|
|
use pgBackRest::Version;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
# Backup advisory lock
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
use constant DB_BACKUP_ADVISORY_LOCK => '12340078987004321';
|
|
|
|
push @EXPORT, qw(DB_BACKUP_ADVISORY_LOCK);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
# Map the control and catalog versions to PostgreSQL version.
|
|
|
|
#
|
|
|
|
# The control version can be found in src/include/catalog/pg_control.h and may not change with every version of PostgreSQL but is
|
|
|
|
# still checked to detect custom builds which change the structure. The catalog version can be found in
|
|
|
|
# src/include/catalog/catversion.h and should change with every release.
|
2015-08-08 23:11:20 +02:00
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
my $oPgControlVersionHash =
|
|
|
|
{
|
|
|
|
# iControlVersion => {iCatalogVersion => strDbVersion}
|
|
|
|
833 => {200711281 => PG_VERSION_83},
|
|
|
|
843 => {200904091 => PG_VERSION_84},
|
|
|
|
903 =>
|
|
|
|
{
|
|
|
|
201008051 => PG_VERSION_90,
|
|
|
|
201105231 => PG_VERSION_91,
|
|
|
|
},
|
|
|
|
922 => {201204301 => PG_VERSION_92},
|
|
|
|
937 => {201306121 => PG_VERSION_93},
|
|
|
|
942 =>
|
|
|
|
{
|
|
|
|
201409291 => PG_VERSION_94,
|
|
|
|
201510051 => PG_VERSION_95,
|
2016-07-26 22:39:01 +02:00
|
|
|
},
|
|
|
|
960 =>
|
|
|
|
{
|
2016-09-04 00:34:38 +02:00
|
|
|
201608131 => PG_VERSION_96,
|
2016-05-14 16:33:12 +02:00
|
|
|
},
|
|
|
|
};
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
2014-10-10 23:03:33 +03:00
|
|
|
sub new
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-01 21:58:33 +02:00
|
|
|
my $class = shift; # Class name
|
2014-10-10 23:03:33 +03:00
|
|
|
|
|
|
|
# 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-24 18:39:27 +02:00
|
|
|
(
|
|
|
|
my $strOperation,
|
|
|
|
$self->{iRemoteIdx},
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->new', \@_,
|
|
|
|
{name => 'iRemoteIdx', required => false},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (defined($self->{iRemoteIdx}))
|
|
|
|
{
|
|
|
|
$self->{strDbPath} = optionGet(optionIndex(OPTION_DB_PATH, $self->{iRemoteIdx}));
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
if (!isDbLocal({iRemoteIdx => $self->{iRemoteIdx}}))
|
|
|
|
{
|
|
|
|
$self->{oProtocol} = protocolGet(DB, $self->{iRemoteIdx});
|
|
|
|
}
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-08-05 18:32:12 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# DESTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub DESTROY
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
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__ . '->DESTROY');
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-08-05 18:32:12 +02:00
|
|
|
if (defined($self->{hDb}))
|
|
|
|
{
|
|
|
|
$self->{hDb}->disconnect();
|
|
|
|
undef($self->{hDb});
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2016-08-11 23:32:28 +02:00
|
|
|
return logDebugReturn($strOperation);
|
2015-08-05 18:32:12 +02:00
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2016-08-15 23:44:51 +02:00
|
|
|
# connect
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2016-08-15 23:44:51 +02:00
|
|
|
sub connect
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-06-30 04:07:42 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-08-15 23:44:51 +02:00
|
|
|
$bWarnOnError,
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-15 23:44:51 +02:00
|
|
|
__PACKAGE__ . '::connect', \@_,
|
|
|
|
{name => 'bWarnOnError', default => false},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2016-08-15 23:44:51 +02:00
|
|
|
# Only connect if not already connected
|
|
|
|
my $bResult = true;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Run remotely
|
2017-06-09 23:51:41 +02:00
|
|
|
if (defined($self->{oProtocol}))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
# Set bResult to false if undef is returned
|
|
|
|
$bResult = $self->{oProtocol}->cmdExecute(OP_DB_CONNECT, undef, false, $bWarnOnError) ? true : false;
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
# Else run locally
|
|
|
|
else
|
|
|
|
{
|
2015-08-05 18:32:12 +02:00
|
|
|
if (!defined($self->{hDb}))
|
|
|
|
{
|
|
|
|
# Connect to the db
|
|
|
|
my $strDbName = 'postgres';
|
|
|
|
my $strDbUser = getpwuid($<);
|
2016-08-24 18:39:27 +02:00
|
|
|
my $strDbSocketPath = optionGet(optionIndex(OPTION_DB_SOCKET_PATH, $self->{iRemoteIdx}), false);
|
2015-08-06 04:05:45 +02:00
|
|
|
|
2016-08-15 23:44:51 +02:00
|
|
|
# Make sure the socket path is absolute
|
2015-08-06 04:05:45 +02:00
|
|
|
if (defined($strDbSocketPath) && $strDbSocketPath !~ /^\//)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strDbSocketPath}' is not valid for '" . OPTION_DB_SOCKET_PATH . "' option:" .
|
|
|
|
" path must be absolute", ERROR_OPTION_INVALID_VALUE);
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
# Construct the URI
|
|
|
|
my $strDbUri = "dbi:Pg:dbname=${strDbName};port=" . optionGet(optionIndex(OPTION_DB_PORT, $self->{iRemoteIdx})) .
|
|
|
|
(defined($strDbSocketPath) ? ";host=${strDbSocketPath}" : '');
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
logDebugMisc
|
|
|
|
(
|
2016-08-15 23:44:51 +02:00
|
|
|
$strOperation, undef,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strDbUri', value => $strDbUri},
|
|
|
|
{name => 'strDbUser', value => $strDbUser}
|
|
|
|
);
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2015-09-08 20:01:19 +02:00
|
|
|
$self->{hDb} = DBI->connect($strDbUri, $strDbUser, undef,
|
|
|
|
{AutoCommit => 1, RaiseError => 0, PrintError => 0, Warn => 0});
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2016-08-15 23:44:51 +02:00
|
|
|
# If db handle is not valid then check error
|
2015-08-05 18:32:12 +02:00
|
|
|
if (!$self->{hDb})
|
|
|
|
{
|
2016-08-15 23:44:51 +02:00
|
|
|
# Throw an error unless a warning was requested
|
|
|
|
if (!$bWarnOnError)
|
|
|
|
{
|
2016-08-12 04:35:24 +02:00
|
|
|
confess &log(ERROR, $DBI::errstr, ERROR_DB_CONNECT);
|
|
|
|
}
|
2016-08-15 23:44:51 +02:00
|
|
|
|
|
|
|
# Log a warning
|
|
|
|
&log(WARN, $DBI::errstr);
|
|
|
|
|
|
|
|
$bResult = false;
|
|
|
|
undef($self->{hDb});
|
|
|
|
}
|
2016-10-06 03:15:10 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
my ($fDbVersion) = $self->versionGet();
|
|
|
|
|
|
|
|
if ($fDbVersion >= PG_VERSION_APPLICATION_NAME)
|
|
|
|
{
|
|
|
|
$self->{hDb}->do(
|
|
|
|
"set application_name = '" . BACKREST_NAME . ' [' .
|
|
|
|
(optionValid(OPTION_COMMAND) ? optionGet(OPTION_COMMAND) : commandGet()) . "]'")
|
|
|
|
or confess &log(ERROR, $self->{hDb}->errstr, ERROR_DB_QUERY);
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 23:44:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bResult', value => $bResult}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# executeSql
|
|
|
|
####################################################################################################################################
|
|
|
|
sub executeSql
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSql,
|
|
|
|
$bIgnoreError,
|
|
|
|
$bResult,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::executeSql', \@_,
|
|
|
|
{name => 'strSql'},
|
|
|
|
{name => 'bIgnoreError', default => false},
|
|
|
|
{name => 'bResult', default => true},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Get the user-defined command for psql
|
2016-12-04 00:34:51 +02:00
|
|
|
my @stryResult;
|
2016-08-15 23:44:51 +02:00
|
|
|
|
|
|
|
# Run remotely
|
2017-06-09 23:51:41 +02:00
|
|
|
if (defined($self->{oProtocol}))
|
2016-08-15 23:44:51 +02:00
|
|
|
{
|
|
|
|
# Execute the command
|
2016-12-04 00:34:51 +02:00
|
|
|
@stryResult = @{$self->{oProtocol}->cmdExecute(OP_DB_EXECUTE_SQL, [$strSql, $bIgnoreError, $bResult], $bResult)};
|
2016-08-12 04:35:24 +02:00
|
|
|
}
|
2016-08-15 23:44:51 +02:00
|
|
|
# Else run locally
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$self->connect();
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
# Prepare the query
|
2015-08-09 00:22:38 +02:00
|
|
|
my $hStatement = $self->{hDb}->prepare($strSql, {pg_async => PG_ASYNC})
|
2016-08-15 23:44:51 +02:00
|
|
|
or confess &log(ERROR, $DBI::errstr . ":\n${strSql}", ERROR_DB_QUERY);
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
# Execute the query
|
2016-08-15 23:44:51 +02:00
|
|
|
$hStatement->execute()
|
|
|
|
or confess &log(ERROR, $DBI::errstr. ":\n${strSql}", ERROR_DB_QUERY);
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2015-08-09 00:22:38 +02:00
|
|
|
# Wait for the query to return
|
2015-08-29 20:20:46 +02:00
|
|
|
my $oWait = waitInit(optionGet(OPTION_DB_TIMEOUT));
|
2015-08-09 00:22:38 +02:00
|
|
|
my $bTimeout = true;
|
2015-08-05 18:32:12 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-08-09 00:22:38 +02:00
|
|
|
# Is the statement done?
|
|
|
|
if ($hStatement->pg_ready())
|
2015-08-05 18:32:12 +02:00
|
|
|
{
|
2016-08-15 23:44:51 +02:00
|
|
|
# return now if there is no result expected
|
|
|
|
if (!$bResult)
|
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
return \@stryResult;
|
2016-08-15 23:44:51 +02:00
|
|
|
}
|
|
|
|
|
2015-08-09 00:22:38 +02:00
|
|
|
if (!$hStatement->pg_result())
|
|
|
|
{
|
|
|
|
# Return if the error should be ignored
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($bIgnoreError)
|
2015-08-09 00:22:38 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
return \@stryResult;
|
2015-08-09 00:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Else report it
|
|
|
|
confess &log(ERROR, $DBI::errstr . ":\n${strSql}", ERROR_DB_QUERY);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get rows and return them
|
2016-05-14 16:29:35 +02:00
|
|
|
my @stryRow;
|
2015-08-09 00:22:38 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-05-14 16:29:35 +02:00
|
|
|
# Get next row
|
|
|
|
@stryRow = $hStatement->fetchrow_array;
|
2015-08-09 00:22:38 +02:00
|
|
|
|
2016-05-14 16:29:35 +02:00
|
|
|
# If the row has data then add it to the result
|
|
|
|
if (@stryRow)
|
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
push(@{$stryResult[@stryResult]}, @stryRow);
|
2016-05-14 16:29:35 +02:00
|
|
|
}
|
|
|
|
# Else check for error
|
|
|
|
elsif ($hStatement->err)
|
2015-08-09 00:22:38 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, $DBI::errstr . ":\n${strSql}", ERROR_DB_QUERY);
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 16:29:35 +02:00
|
|
|
while (@stryRow);
|
2015-08-09 00:22:38 +02:00
|
|
|
|
|
|
|
$bTimeout = false;
|
2015-08-05 18:32:12 +02:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
} while ($bTimeout && waitMore($oWait));
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2015-08-09 00:22:38 +02:00
|
|
|
# If timeout then cancel the query and confess
|
|
|
|
if ($bTimeout)
|
|
|
|
{
|
|
|
|
$hStatement->pg_cancel();
|
2015-08-29 20:20:46 +02:00
|
|
|
confess &log(ERROR, 'statement timed out after ' . waitInterval($oWait) .
|
|
|
|
" second(s):\n${strSql}", ERROR_DB_TIMEOUT);
|
2015-08-05 18:32:12 +02:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'stryResult', value => \@stryResult, ref => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
# executeSqlRow
|
|
|
|
####################################################################################################################################
|
|
|
|
sub executeSqlRow
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSql
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->executeSqlRow', \@_,
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'strSql'}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'stryResult', value => @{$self->executeSql($strSql)}[0]}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-08 23:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# executeSqlOne
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
sub executeSqlOne
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSql
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->executeSqlOne', \@_,
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'strSql'}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'strResult', value => @{@{$self->executeSql($strSql)}[0]}[0]}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-08 23:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# tablespaceMapGet
|
|
|
|
#
|
|
|
|
# Get the mapping between oid and tablespace name.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub tablespaceMapGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
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__ . '->tablespaceMapGet');
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
my $hTablespaceMap = {};
|
|
|
|
|
|
|
|
for my $strRow (@{$self->executeSql('select oid, spcname from pg_tablespace')})
|
|
|
|
{
|
|
|
|
$hTablespaceMap->{@{$strRow}[0]} = @{$strRow}[1];
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-11-30 21:36:39 +02:00
|
|
|
{name => 'hTablespaceMap', value => $hTablespaceMap}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 15:21:39 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# databaseMapGet
|
|
|
|
#
|
|
|
|
# Get the mapping between oid and database name.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub databaseMapGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '->databaseMapGet');
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
my $hDatabaseMap = {};
|
|
|
|
|
|
|
|
for my $strRow (@{$self->executeSql('select datname, oid, datlastsysoid from pg_database')})
|
|
|
|
{
|
|
|
|
$hDatabaseMap->{@{$strRow}[0]}{&MANIFEST_KEY_DB_ID} = @{$strRow}[1];
|
|
|
|
$hDatabaseMap->{@{$strRow}[0]}{&MANIFEST_KEY_DB_LAST_SYSTEM_ID} = @{$strRow}[2];
|
|
|
|
}
|
2016-05-11 15:21:39 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-11-30 21:36:39 +02:00
|
|
|
{name => 'hDatabaseMap', value => $hDatabaseMap}
|
2016-05-11 15:21:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# info
|
|
|
|
####################################################################################################################################
|
|
|
|
sub info
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strDbPath
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->info', \@_,
|
2016-08-24 18:39:27 +02:00
|
|
|
{name => 'strDbPath', default => $self->{strDbPath}}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Get info if it is not cached
|
|
|
|
#-------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
if (!defined($self->{info}{$strDbPath}))
|
2015-08-06 04:05:45 +02:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
# Get info from remote
|
|
|
|
#---------------------------------------------------------------------------------------------------------------------------
|
2017-06-09 23:51:41 +02:00
|
|
|
if (defined($self->{oProtocol}))
|
2016-05-14 16:33:12 +02:00
|
|
|
{
|
|
|
|
# Execute the command
|
2016-12-04 00:34:51 +02:00
|
|
|
($self->{info}{$strDbPath}{strDbVersion}, $self->{info}{$strDbPath}{iDbControlVersion},
|
|
|
|
$self->{info}{$strDbPath}{iDbCatalogVersion}, $self->{info}{$strDbPath}{ullDbSysId}) =
|
2017-06-09 23:51:41 +02:00
|
|
|
$self->{oProtocol}->cmdExecute(OP_DB_INFO, [$strDbPath], true);
|
2016-05-14 16:33:12 +02:00
|
|
|
}
|
|
|
|
# Get info locally
|
|
|
|
#---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Open the control file and read system id and versions
|
|
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
my $strControlFile = "${strDbPath}/" . DB_FILE_PGCONTROL;
|
|
|
|
my $hFile;
|
|
|
|
my $tBlock;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
sysopen($hFile, $strControlFile, O_RDONLY)
|
|
|
|
or confess &log(ERROR, "unable to open ${strControlFile}", ERROR_FILE_OPEN);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Read system identifier
|
|
|
|
sysread($hFile, $tBlock, 8) == 8
|
|
|
|
or confess &log(ERROR, "unable to read database system identifier");
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
$self->{info}{$strDbPath}{ullDbSysId} = unpack('Q', $tBlock);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Read control version
|
|
|
|
sysread($hFile, $tBlock, 4) == 4
|
|
|
|
or confess &log(ERROR, "unable to read control version");
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{info}{$strDbPath}{iDbControlVersion} = unpack('L', $tBlock);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Read catalog version
|
|
|
|
sysread($hFile, $tBlock, 4) == 4
|
|
|
|
or confess &log(ERROR, "unable to read catalog version");
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{info}{$strDbPath}{iDbCatalogVersion} = unpack('L', $tBlock);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Close the control file
|
|
|
|
close($hFile);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Get PostgreSQL version
|
|
|
|
$self->{info}{$strDbPath}{strDbVersion} =
|
2016-12-04 00:34:51 +02:00
|
|
|
$oPgControlVersionHash->{$self->{info}{$strDbPath}{iDbControlVersion}}
|
|
|
|
{$self->{info}{$strDbPath}{iDbCatalogVersion}};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
if (!defined($self->{info}{$strDbPath}{strDbVersion}))
|
|
|
|
{
|
|
|
|
confess &log(
|
|
|
|
ERROR,
|
2016-12-04 00:34:51 +02:00
|
|
|
'unexpected control version = ' . $self->{info}{$strDbPath}{iDbControlVersion} .
|
|
|
|
' and catalog version = ' . $self->{info}{$strDbPath}{iDbCatalogVersion} . "\n" .
|
2016-05-14 16:36:35 +02:00
|
|
|
'HINT: is this version of PostgreSQL supported?',
|
2016-05-14 16:33:12 +02:00
|
|
|
ERROR_VERSION_NOT_SUPPORTED);
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-05-14 16:33:12 +02:00
|
|
|
{name => 'strDbVersion', value => $self->{info}{$strDbPath}{strDbVersion}},
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'iDbControlVersion', value => $self->{info}{$strDbPath}{iDbControlVersion}},
|
|
|
|
{name => 'iDbCatalogVersion', value => $self->{info}{$strDbPath}{iDbCatalogVersion}},
|
2016-05-14 16:33:12 +02:00
|
|
|
{name => 'ullDbSysId', value => $self->{info}{$strDbPath}{ullDbSysId}}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-06-30 04:07:42 +02:00
|
|
|
# versionGet
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-06-30 04:07:42 +02:00
|
|
|
sub versionGet
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-04 05:02:56 +03: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__ . '->versionGet');
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-08-06 04:05:45 +02:00
|
|
|
# Get data from the cache if possible
|
2016-05-14 16:33:12 +02:00
|
|
|
if (defined($self->{strDbVersion}) && defined($self->{strDbPath}))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
return $self->{strDbVersion}, $self->{strDbPath};
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 04:05:45 +02:00
|
|
|
# Get version and db-path from
|
2016-05-14 16:33:12 +02:00
|
|
|
($self->{strDbVersion}, $self->{strDbPath}) =
|
2015-08-08 23:11:20 +02:00
|
|
|
$self->executeSqlRow("select (regexp_matches(split_part(version(), ' ', 2), '^[0-9]+\.[0-9]+'))[1], setting" .
|
|
|
|
" from pg_settings where name = 'data_directory'");
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
my @stryVersionSupport = versionSupport();
|
2015-04-03 04:07:23 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->{strDbVersion} < $stryVersionSupport[0])
|
2015-04-03 04:07:23 +02:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
confess &log(ERROR, 'unsupported Postgres version' . $self->{strDbVersion}, ERROR_VERSION_NOT_SUPPORTED);
|
2015-04-03 04:07:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-05-14 16:33:12 +02:00
|
|
|
{name => 'strDbVersion', value => $self->{strDbVersion}},
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strDbPath', value => $self->{strDbPath}}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
# backupStart
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
sub backupStart
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strLabel,
|
|
|
|
$bStartFast
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->backupStart', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strLabel'},
|
|
|
|
{name => 'bStartFast'}
|
|
|
|
);
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2016-06-12 15:13:46 +02:00
|
|
|
# Validate the database configuration
|
2016-11-30 21:15:11 +02:00
|
|
|
$self->configValidate();
|
2015-04-01 21:58:33 +02:00
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
# Only allow start-fast option for version >= 8.4
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->{strDbVersion} < PG_VERSION_84 && $bStartFast)
|
2015-04-01 21:58:33 +02:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
&log(WARN, OPTION_START_FAST . ' option is only available in PostgreSQL >= ' . PG_VERSION_84);
|
2015-04-01 21:58:33 +02:00
|
|
|
$bStartFast = false;
|
|
|
|
}
|
|
|
|
|
2016-12-13 01:54:07 +02:00
|
|
|
# Determine if page checksums can be enabled
|
|
|
|
my $bChecksumPage =
|
|
|
|
$self->executeSqlOne("select count(*) = 1 from pg_settings where name = 'data_checksums' and setting = 'on'");
|
|
|
|
|
|
|
|
# If checksum page option is not explictly set then set it to whatever the database says
|
|
|
|
if (!optionTest(OPTION_CHECKSUM_PAGE))
|
|
|
|
{
|
|
|
|
optionSet(OPTION_CHECKSUM_PAGE, $bChecksumPage);
|
|
|
|
}
|
|
|
|
# Else if enabled make sure they are in the database as well, else throw a warning
|
|
|
|
elsif (optionGet(OPTION_CHECKSUM_PAGE) && !$bChecksumPage)
|
|
|
|
{
|
|
|
|
&log(WARN, 'unable to enable page checksums since they are not enabled in the database');
|
|
|
|
optionSet(OPTION_CHECKSUM_PAGE, false);
|
|
|
|
}
|
|
|
|
|
2015-09-16 22:23:19 +02:00
|
|
|
# Acquire the backup advisory lock to make sure that backups are not running from multiple backup servers against the same
|
|
|
|
# database cluster. This lock helps make the stop-auto option safe.
|
2015-08-08 23:11:20 +02:00
|
|
|
if (!$self->executeSqlOne('select pg_try_advisory_lock(' . DB_BACKUP_ADVISORY_LOCK . ')'))
|
|
|
|
{
|
2016-10-06 03:13:29 +02:00
|
|
|
confess &log(ERROR, 'unable to acquire ' . BACKREST_NAME . " advisory lock\n" .
|
|
|
|
'HINT: is another ' . BACKREST_NAME . ' backup already running on this cluster?', ERROR_LOCK_ACQUIRE);
|
2015-08-08 23:11:20 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 23:59:26 +02:00
|
|
|
# If stop-auto is enabled check for a running backup. This feature is not supported for PostgreSQL >= 9.6 since backups are
|
|
|
|
# run in non-exclusive mode.
|
|
|
|
if (optionGet(OPTION_STOP_AUTO) && $self->{strDbVersion} < PG_VERSION_96)
|
2015-08-08 23:11:20 +02:00
|
|
|
{
|
2015-09-16 22:23:19 +02:00
|
|
|
# Running backups can only be detected in PostgreSQL >= 9.3
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->{strDbVersion} >= PG_VERSION_93)
|
2015-08-08 23:11:20 +02:00
|
|
|
{
|
2015-09-16 22:23:19 +02:00
|
|
|
# If a backup is currently in progress emit a warning and then stop it
|
2015-08-08 23:11:20 +02:00
|
|
|
if ($self->executeSqlOne('select pg_is_in_backup()'))
|
|
|
|
{
|
2016-10-06 03:13:29 +02:00
|
|
|
&log(WARN, 'the cluster is already in backup mode but no ' . BACKREST_NAME . ' backup process is running.' .
|
2016-05-14 16:33:12 +02:00
|
|
|
' pg_stop_backup() will be called so a new backup can be started.');
|
2015-08-08 23:11:20 +02:00
|
|
|
$self->backupStop();
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 22:23:19 +02:00
|
|
|
# Else emit a warning that the feature is not supported and continue. If a backup is running then an error will be
|
|
|
|
# generated later on.
|
2015-08-08 23:11:20 +02:00
|
|
|
else
|
|
|
|
{
|
2017-01-23 02:29:56 +02:00
|
|
|
&log(WARN, OPTION_STOP_AUTO . ' option is only available in PostgreSQL >= ' . PG_VERSION_93);
|
2015-08-08 23:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 22:23:19 +02:00
|
|
|
# Start the backup
|
2016-05-16 23:59:26 +02:00
|
|
|
&log(INFO, 'execute ' . ($self->{strDbVersion} >= PG_VERSION_96 ? 'non-' : '') .
|
|
|
|
"exclusive pg_start_backup() with label \"${strLabel}\": backup begins after " .
|
2015-05-26 18:26:59 +02:00
|
|
|
($bStartFast ? "the requested immediate checkpoint" : "the next regular checkpoint") . " completes");
|
|
|
|
|
2016-09-07 13:20:07 +02:00
|
|
|
my ($strTimestampDbStart, $strArchiveStart, $strLsnStart) = $self->executeSqlRow(
|
|
|
|
"select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS.US TZ'), pg_xlogfile_name(lsn), lsn::text" .
|
2016-06-24 14:06:20 +02:00
|
|
|
" from pg_start_backup('${strLabel}'" .
|
|
|
|
($bStartFast ? ', true' : $self->{strDbVersion} >= PG_VERSION_84 ? ', false' : '') .
|
|
|
|
($self->{strDbVersion} >= PG_VERSION_96 ? ', false' : '') . ') as lsn');
|
2015-01-03 23:49:26 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strArchiveStart', value => $strArchiveStart},
|
2016-09-07 13:20:07 +02:00
|
|
|
{name => 'strLsnStart', value => $strLsnStart},
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strTimestampDbStart', value => $strTimestampDbStart}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
# backupStop
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
sub backupStop
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
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__ . '->backupStop');
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-09-16 22:23:19 +02:00
|
|
|
# Stop the backup
|
2016-05-16 23:59:26 +02:00
|
|
|
&log(INFO, 'execute ' . ($self->{strDbVersion} >= PG_VERSION_96 ? 'non-' : '') .
|
|
|
|
'exclusive pg_stop_backup() and wait for all WAL segments to archive');
|
|
|
|
|
2016-09-07 13:20:07 +02:00
|
|
|
my ($strTimestampDbStop, $strArchiveStop, $strLsnStop, $strLabel, $strTablespaceMap) =
|
2016-05-16 23:59:26 +02:00
|
|
|
$self->executeSqlRow(
|
2016-09-07 13:20:07 +02:00
|
|
|
"select to_char(clock_timestamp(), 'YYYY-MM-DD HH24:MI:SS.US TZ'), pg_xlogfile_name(lsn), lsn::text, " .
|
2016-12-04 00:34:51 +02:00
|
|
|
($self->{strDbVersion} >= PG_VERSION_96 ?
|
|
|
|
'labelfile, ' .
|
|
|
|
'case when length(trim(both \'\t\n \' from spcmapfile)) = 0 then null else spcmapfile end as spcmapfile' :
|
|
|
|
'null as labelfile, null as spcmapfile') .
|
2016-05-16 23:59:26 +02:00
|
|
|
' from pg_stop_backup(' .
|
|
|
|
($self->{strDbVersion} >= PG_VERSION_96 ? 'false)' : ') as lsn'));
|
|
|
|
|
|
|
|
# Build a hash of the files that need to be written to the backup
|
|
|
|
my $oFileHash =
|
|
|
|
{
|
|
|
|
&MANIFEST_FILE_BACKUPLABEL => $strLabel,
|
|
|
|
&MANIFEST_FILE_TABLESPACEMAP => $strTablespaceMap
|
|
|
|
};
|
2015-01-03 23:49:26 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strArchiveStop', value => $strArchiveStop},
|
2016-09-07 13:20:07 +02:00
|
|
|
{name => 'strLsnStop', value => $strLsnStop},
|
2016-05-16 23:59:26 +02:00
|
|
|
{name => 'strTimestampDbStop', value => $strTimestampDbStop},
|
|
|
|
{name => 'oFileHash', value => $oFileHash}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2016-06-12 15:13:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# configValidate
|
|
|
|
#
|
|
|
|
# Validate the database configuration and archiving.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub configValidate
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->configValidate', \@_,
|
|
|
|
);
|
|
|
|
|
|
|
|
# Get the version from the control file
|
2016-11-30 21:15:11 +02:00
|
|
|
my ($strDbVersion) = $self->info();
|
2016-06-12 15:13:46 +02:00
|
|
|
|
|
|
|
# Get version and db path from the database
|
|
|
|
my ($fCompareDbVersion, $strCompareDbPath) = $self->versionGet();
|
|
|
|
|
|
|
|
# Error if the version from the control file and the configured db-path do not match the values obtained from the database
|
2016-11-30 21:15:11 +02:00
|
|
|
if (!($strDbVersion == $fCompareDbVersion && $self->{strDbPath} eq $strCompareDbPath))
|
2016-06-12 15:13:46 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR,
|
|
|
|
"version '${fCompareDbVersion}' and db-path '${strCompareDbPath}' queried from cluster does not match" .
|
2016-11-30 21:15:11 +02:00
|
|
|
" version '${strDbVersion}' and db-path '$self->{strDbPath}' read from '$self->{strDbPath}/" .
|
|
|
|
DB_FILE_PGCONTROL . "'\n" . "HINT: the db-path and db-port settings likely reference different clusters",
|
|
|
|
ERROR_DB_MISMATCH);
|
2016-06-12 15:13:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-01 19:44:45 +02:00
|
|
|
# If cluster is not a standby and archive checking is enabled, then perform various validations
|
2016-10-14 13:21:47 +02:00
|
|
|
if (!$self->isStandby() && optionValid(OPTION_BACKUP_ARCHIVE_CHECK) && optionGet(OPTION_BACKUP_ARCHIVE_CHECK))
|
2016-06-12 15:13:46 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
my $strArchiveMode = $self->executeSqlOne('show archive_mode');
|
|
|
|
|
2016-09-29 01:45:33 +02:00
|
|
|
# Error if archive_mode = off since pg_start_backup () will fail
|
2016-12-04 00:34:51 +02:00
|
|
|
if ($strArchiveMode eq 'off')
|
2016-09-29 01:45:33 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, 'archive_mode must be enabled', ERROR_ARCHIVE_DISABLED);
|
|
|
|
}
|
2016-06-12 15:13:46 +02:00
|
|
|
|
2016-09-29 01:45:33 +02:00
|
|
|
# Error if archive_mode = always (support has not been added yet)
|
2016-12-04 00:34:51 +02:00
|
|
|
if ($strArchiveMode eq 'always')
|
2016-09-29 01:45:33 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "archive_mode=always not supported", ERROR_FEATURE_NOT_SUPPORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if archive_command is set
|
2016-12-04 00:34:51 +02:00
|
|
|
my $strArchiveCommand = $self->executeSqlOne('show archive_command');
|
2016-07-02 15:02:55 +02:00
|
|
|
|
|
|
|
if (index($strArchiveCommand, BACKREST_EXE) == -1)
|
|
|
|
{
|
|
|
|
confess &log(ERROR,
|
2016-09-29 01:45:33 +02:00
|
|
|
'archive_command ' . (defined($strArchiveCommand) ? "'${strArchiveCommand}'" : '[null]') . ' must contain \'' .
|
|
|
|
BACKREST_EXE . '\'', ERROR_ARCHIVE_COMMAND_INVALID);
|
2016-07-02 15:02:55 +02:00
|
|
|
}
|
2016-06-12 15:13:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# xlogSwitch
|
|
|
|
#
|
|
|
|
# Forces a switch to the next transaction log in order to archive the current log.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub xlogSwitch
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my $strOperation = logDebugParam(__PACKAGE__ . '->xlogSwitch');
|
|
|
|
|
|
|
|
# Create a restore point to ensure current xlog will be archived. For versions <= 9.0 activity will need to be generated by
|
|
|
|
# the user if there have been no writes since the last xlog switch.
|
|
|
|
if ($self->{strDbVersion} >= PG_VERSION_91)
|
|
|
|
{
|
2016-08-15 23:44:51 +02:00
|
|
|
$self->executeSql("select pg_create_restore_point('" . BACKREST_NAME . " Archive Check');");
|
2016-06-12 15:13:46 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
my $strWalFileName = $self->executeSqlOne('select pg_xlogfile_name from pg_xlogfile_name(pg_switch_xlog());');
|
2016-06-12 15:13:46 +02:00
|
|
|
|
|
|
|
&log(INFO, "switch xlog ${strWalFileName}");
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strXlogFileName', value => $strWalFileName}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-01 19:44:45 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# isStandby
|
|
|
|
#
|
|
|
|
# Determines if a database is a standby by testing if it is in recovery mode.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub isStandby
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '->isStandby');
|
|
|
|
|
|
|
|
if (!defined($self->{bStandby}))
|
|
|
|
{
|
|
|
|
my ($strDbVersion) = $self->versionGet();
|
|
|
|
|
|
|
|
if ($strDbVersion <= PG_VERSION_90)
|
|
|
|
{
|
|
|
|
$self->{bStandby} = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$self->{bStandby} = $self->executeSqlOne('select pg_is_in_recovery()') ? true : false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bStandby', value => $self->{bStandby}}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-25 17:25:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# replayWait
|
|
|
|
#
|
|
|
|
# Waits for replay on the standby to equal specified LSN
|
|
|
|
####################################################################################################################################
|
|
|
|
sub replayWait
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strTargetLSN,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->replayWait', \@_,
|
|
|
|
{name => 'strTargetLSN'}
|
|
|
|
);
|
|
|
|
|
|
|
|
# Load ArchiveCommon Module
|
2017-01-11 02:51:20 +02:00
|
|
|
require pgBackRest::Archive::ArchiveCommon;
|
|
|
|
pgBackRest::Archive::ArchiveCommon->import();
|
2016-08-25 17:25:46 +02:00
|
|
|
|
|
|
|
# Initialize working variables
|
|
|
|
my $oWait = waitInit(optionGet(OPTION_ARCHIVE_TIMEOUT));
|
|
|
|
my $bTimeout = true;
|
|
|
|
my $strReplayedLSN = undef;
|
|
|
|
|
|
|
|
# Monitor the replay location
|
|
|
|
do
|
|
|
|
{
|
|
|
|
# Get the replay location
|
|
|
|
my $strLastReplayedLSN = $self->executeSqlOne("select coalesce(pg_last_xlog_replay_location()::text, '<NONE>')");
|
|
|
|
|
|
|
|
# Error if the replay location could not be retrieved
|
|
|
|
if ($strLastReplayedLSN eq '<NONE>')
|
|
|
|
{
|
|
|
|
confess &log(
|
|
|
|
ERROR,
|
|
|
|
"unable to query replay location on the standby using pg_last_xlog_replay_location()\n" .
|
|
|
|
"Hint: Is this a standby?",
|
|
|
|
ERROR_ARCHIVE_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Is the replay lsn > target lsn? It needs to be greater because the checkpoint record is directly after the LSN returned
|
|
|
|
# by pg_start_backup().
|
|
|
|
if (lsnNormalize($strLastReplayedLSN) ge lsnNormalize($strTargetLSN))
|
|
|
|
{
|
|
|
|
$bTimeout = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Reset the timer if the LSN is advancing
|
|
|
|
if (defined($strReplayedLSN) &&
|
|
|
|
lsnNormalize($strLastReplayedLSN) gt lsnNormalize($strReplayedLSN) &&
|
|
|
|
!waitMore($oWait))
|
|
|
|
{
|
|
|
|
$oWait = waitInit(optionGet(OPTION_ARCHIVE_TIMEOUT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Assigned last replayed to replayed
|
|
|
|
$strReplayedLSN = $strLastReplayedLSN;
|
|
|
|
|
|
|
|
} while ($bTimeout && waitMore($oWait));
|
|
|
|
|
|
|
|
# Error if a timeout occurred before the target lsn was reached
|
|
|
|
if ($bTimeout == true)
|
|
|
|
{
|
|
|
|
confess &log(
|
|
|
|
ERROR, "timeout before standby replayed ${strTargetLSN} - only reached ${strReplayedLSN}", ERROR_ARCHIVE_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Perform a checkpoint
|
|
|
|
$self->executeSql('checkpoint', undef, false);
|
|
|
|
|
|
|
|
# On PostgreSQL >= 9.6 the checkpoint location can be verified
|
|
|
|
my $strCheckpointLSN = undef;
|
|
|
|
|
|
|
|
if ($self->{strDbVersion} >= PG_VERSION_96)
|
|
|
|
{
|
|
|
|
$strCheckpointLSN = $self->executeSqlOne('select checkpoint_location from pg_control_checkpoint()');
|
|
|
|
|
|
|
|
if (lsnNormalize($strCheckpointLSN) le lsnNormalize($strTargetLSN))
|
|
|
|
{
|
|
|
|
confess &log(
|
|
|
|
ERROR,
|
|
|
|
"the checkpoint location ${strCheckpointLSN} is less than the target location ${strTargetLSN} even though the" .
|
|
|
|
" replay location is ${strReplayedLSN}\n" .
|
|
|
|
"Hint: This should not be possible and may indicate a bug in PostgreSQL.",
|
|
|
|
ERROR_ARCHIVE_TIMEOUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strReplayedLSN', value => $strReplayedLSN},
|
|
|
|
{name => 'strCheckpointLSN', value => $strCheckpointLSN},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-30 21:15:11 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# dbObjectGet
|
|
|
|
#
|
|
|
|
# Gets the database objects(s) and indexes. The databases required for the backup type must be online. A connection to the available
|
|
|
|
# databases will be established to determine which is the master and which, if any, is the standby. If there is a master and a
|
|
|
|
# standby to which a connection can be established, it returns both, else just the master.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub dbObjectGet
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '::dbObjectGet');
|
|
|
|
|
|
|
|
my $iStandbyIdx = undef;
|
|
|
|
my $iMasterRemoteIdx = 1;
|
|
|
|
my $oDbMaster = undef;
|
|
|
|
my $oDbStandby = undef;
|
|
|
|
|
|
|
|
# Only iterate databases if online and more than one is defined. It might be better to check the version of each database but
|
|
|
|
# this is simple and works.
|
|
|
|
if (optionTest(OPTION_ONLINE) && optionGet(OPTION_ONLINE) && optionTest(optionIndex(OPTION_DB_PATH, 2)))
|
|
|
|
{
|
|
|
|
for (my $iRemoteIdx = 1; $iRemoteIdx <= 2; $iRemoteIdx++)
|
|
|
|
{
|
|
|
|
# Make sure a db is defined for this index
|
|
|
|
if (optionTest(optionIndex(OPTION_DB_PATH, $iRemoteIdx)) || optionTest(optionIndex(OPTION_DB_HOST, $iRemoteIdx)))
|
|
|
|
{
|
|
|
|
# Create the db object
|
|
|
|
my $oDb = new pgBackRest::Db($iRemoteIdx);
|
|
|
|
my $bAssigned = false;
|
|
|
|
|
|
|
|
# If able to connect then test if the database is a master or a standby. It's OK if some databases cannot be
|
|
|
|
# reached as long as the databases required for the backup type are present.
|
|
|
|
if ($oDb->connect(true))
|
|
|
|
{
|
|
|
|
# If this db is a standby
|
|
|
|
if ($oDb->isStandby())
|
|
|
|
{
|
|
|
|
# If standby backup is requested then use the first standby found
|
|
|
|
if (optionGet(OPTION_BACKUP_STANDBY) && !defined($oDbStandby))
|
|
|
|
{
|
|
|
|
$oDbStandby = $oDb;
|
|
|
|
$iStandbyIdx = $iRemoteIdx;
|
|
|
|
$bAssigned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else this db is a master
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Error if more than one master is found
|
|
|
|
if (defined($oDbMaster))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'more than one master database found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$oDbMaster = $oDb;
|
|
|
|
$iMasterRemoteIdx = $iRemoteIdx;
|
|
|
|
$bAssigned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# If the db was not used then destroy the protocol object underneath it
|
|
|
|
if (!$bAssigned)
|
|
|
|
{
|
|
|
|
protocolDestroy(DB, $iRemoteIdx, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure the standby database is defined when backup from standby requested
|
|
|
|
if (optionGet(OPTION_BACKUP_STANDBY) && !defined($oDbStandby))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'unable to find standby database - cannot proceed');
|
|
|
|
}
|
|
|
|
|
|
|
|
# A master database is always required
|
|
|
|
if (!defined($oDbMaster))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'unable to find master database - cannot proceed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# If master db is not already defined then set to default
|
|
|
|
if (!defined($oDbMaster))
|
|
|
|
{
|
|
|
|
$oDbMaster = new pgBackRest::Db($iMasterRemoteIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oDbMaster', value => $oDbMaster},
|
|
|
|
{name => 'iDbMasterIdx', value => $iMasterRemoteIdx},
|
|
|
|
{name => 'oDbStandby', value => $oDbStandby},
|
|
|
|
{name => 'iDbStandbyIdx', value => $iStandbyIdx},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(dbObjectGet);
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# dbMasterGet
|
|
|
|
#
|
|
|
|
# Usually only the master database is required so this function makes getting it simple. If in offline mode (which is true for a
|
|
|
|
# lot of archive operations) then the database returned is simply the first configured.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub dbMasterGet
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '::dbMasterGet');
|
|
|
|
|
|
|
|
my ($oDbMaster) = dbObjectGet();
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oDbMaster', value => $oDbMaster, trace => true},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(dbMasterGet);
|
|
|
|
|
2014-10-10 23:03:33 +03:00
|
|
|
1;
|