2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
2017-06-09 23:51:41 +02:00
|
|
|
# Create and manage protocol objects.
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
2017-05-15 17:12:14 +02:00
|
|
|
package pgBackRest::Protocol::Helper;
|
2016-08-24 18:39:27 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
|
|
|
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Config::Config;
|
2017-05-15 17:12:14 +02:00
|
|
|
use pgBackRest::Protocol::Remote::Master;
|
2016-09-06 15:15:22 +02:00
|
|
|
use pgBackRest::Version;
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Operation constants
|
|
|
|
####################################################################################################################################
|
|
|
|
# Backup module
|
|
|
|
use constant OP_BACKUP_FILE => 'backupFile';
|
|
|
|
push @EXPORT, qw(OP_BACKUP_FILE);
|
|
|
|
|
|
|
|
# Archive Module
|
|
|
|
use constant OP_ARCHIVE_GET_CHECK => 'archiveCheck';
|
|
|
|
push @EXPORT, qw(OP_ARCHIVE_GET_CHECK);
|
|
|
|
use constant OP_ARCHIVE_PUSH_CHECK => 'archivePushCheck';
|
|
|
|
push @EXPORT, qw(OP_ARCHIVE_PUSH_CHECK);
|
|
|
|
|
|
|
|
# Archive File Module
|
2018-04-30 23:27:39 +02:00
|
|
|
use constant OP_ARCHIVE_GET_FILE => 'archiveGetFile';
|
|
|
|
push @EXPORT, qw(OP_ARCHIVE_GET_FILE);
|
2017-06-09 23:51:41 +02:00
|
|
|
use constant OP_ARCHIVE_PUSH_FILE => 'archivePushFile';
|
|
|
|
push @EXPORT, qw(OP_ARCHIVE_PUSH_FILE);
|
|
|
|
|
|
|
|
# Check Module
|
|
|
|
use constant OP_CHECK_BACKUP_INFO_CHECK => 'backupInfoCheck';
|
|
|
|
push @EXPORT, qw(OP_CHECK_BACKUP_INFO_CHECK);
|
|
|
|
|
|
|
|
# Db Module
|
|
|
|
use constant OP_DB_CONNECT => 'dbConnect';
|
|
|
|
push @EXPORT, qw(OP_DB_CONNECT);
|
|
|
|
use constant OP_DB_EXECUTE_SQL => 'dbExecSql';
|
|
|
|
push @EXPORT, qw(OP_DB_EXECUTE_SQL);
|
|
|
|
use constant OP_DB_INFO => 'dbInfo';
|
|
|
|
push @EXPORT, qw(OP_DB_INFO);
|
|
|
|
|
|
|
|
# Storage Module
|
|
|
|
use constant OP_STORAGE_OPEN_READ => 'storageOpenRead';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_OPEN_READ);
|
|
|
|
use constant OP_STORAGE_OPEN_WRITE => 'storageOpenWrite';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_OPEN_WRITE);
|
2017-11-06 19:51:12 +02:00
|
|
|
use constant OP_STORAGE_CIPHER_PASS_USER => 'storageCipherPassUser';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_CIPHER_PASS_USER);
|
2017-06-09 23:51:41 +02:00
|
|
|
use constant OP_STORAGE_EXISTS => 'storageExists';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_EXISTS);
|
|
|
|
use constant OP_STORAGE_LIST => 'storageList';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_LIST);
|
|
|
|
use constant OP_STORAGE_MANIFEST => 'storageManifest';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_MANIFEST);
|
|
|
|
use constant OP_STORAGE_MOVE => 'storageMove';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_MOVE);
|
|
|
|
use constant OP_STORAGE_PATH_GET => 'storagePathGet';
|
|
|
|
push @EXPORT, qw(OP_STORAGE_PATH_GET);
|
|
|
|
|
|
|
|
# Info module
|
|
|
|
use constant OP_INFO_STANZA_LIST => 'infoStanzList';
|
|
|
|
push @EXPORT, qw(OP_INFO_STANZA_LIST);
|
|
|
|
|
|
|
|
# Restore module
|
|
|
|
use constant OP_RESTORE_FILE => 'restoreFile';
|
|
|
|
push @EXPORT, qw(OP_RESTORE_FILE);
|
|
|
|
|
|
|
|
# Wait
|
|
|
|
use constant OP_WAIT => 'wait';
|
|
|
|
push @EXPORT, qw(OP_WAIT);
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Module variables
|
|
|
|
####################################################################################################################################
|
2016-09-06 15:35:02 +02:00
|
|
|
my $hProtocol = {}; # Global remote hash that is created on first request
|
2016-08-24 18:39:27 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# isRepoLocal
|
|
|
|
#
|
|
|
|
# Is the backup/archive repository local? This does not take into account the spool path.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub isRepoLocal
|
|
|
|
{
|
|
|
|
# Not valid for remote
|
2017-08-25 22:47:47 +02:00
|
|
|
if (cfgCommandTest(CFGCMD_REMOTE) && !cfgOptionTest(CFGOPT_TYPE, CFGOPTVAL_REMOTE_TYPE_BACKUP))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
confess &log(ASSERT, 'isRepoLocal() not valid on ' . cfgOption(CFGOPT_TYPE) . ' remote');
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 01:27:38 +02:00
|
|
|
return cfgOptionTest(CFGOPT_REPO_HOST) ? false : true;
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(isRepoLocal);
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-06-09 23:51:41 +02:00
|
|
|
# isDbLocal - is the database local?
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub isDbLocal
|
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$iRemoteIdx,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::isDbLocal', \@_,
|
2017-08-25 22:47:47 +02:00
|
|
|
{name => 'iRemoteIdx', optional => true, default => cfgOptionValid(CFGOPT_HOST_ID) ? cfgOption(CFGOPT_HOST_ID) : 1,
|
2017-06-09 23:51:41 +02:00
|
|
|
trace => true},
|
|
|
|
);
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
# Not valid for remote
|
2017-08-25 22:47:47 +02:00
|
|
|
if (cfgCommandTest(CFGCMD_REMOTE) && !cfgOptionTest(CFGOPT_TYPE, CFGOPTVAL_REMOTE_TYPE_DB))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
confess &log(ASSERT, 'isDbLocal() not valid on ' . cfgOption(CFGOPT_TYPE) . ' remote');
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 01:27:38 +02:00
|
|
|
my $bLocal = cfgOptionTest(cfgOptionIdFromIndex(CFGOPT_PG_HOST, $iRemoteIdx)) ? false : true;
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bLocal', value => $bLocal, trace => true}
|
|
|
|
);
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(isDbLocal);
|
|
|
|
|
2017-11-21 19:57:00 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Gets the parameters required to setup the protocol
|
|
|
|
####################################################################################################################################
|
|
|
|
sub protocolParam
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strCommand,
|
|
|
|
$strRemoteType,
|
|
|
|
$iRemoteIdx,
|
|
|
|
$strBackRestBin,
|
|
|
|
$iProcessIdx,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::protocolParam', \@_,
|
|
|
|
{name => 'strCommand'},
|
|
|
|
{name => 'strRemoteType'},
|
|
|
|
{name => 'iRemoteIdx', default => cfgOptionValid(CFGOPT_HOST_ID) ? cfgOption(CFGOPT_HOST_ID) : 1},
|
|
|
|
{name => 'strBackRestBin', optional => true},
|
2018-08-31 17:31:13 +02:00
|
|
|
{name => 'iProcessIdx', optional => true,
|
|
|
|
default => cfgOptionValid(CFGOPT_PROCESS) ? cfgOption(CFGOPT_PROCESS, false) : undef},
|
2017-11-21 19:57:00 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Return the remote when required
|
2018-02-04 01:27:38 +02:00
|
|
|
my $iOptionIdCmd = CFGOPT_REPO_HOST_CMD;
|
|
|
|
my $iOptionIdConfig = CFGOPT_REPO_HOST_CONFIG;
|
2018-04-19 17:09:39 +02:00
|
|
|
my $iOptionIdConfigIncludePath = CFGOPT_REPO_HOST_CONFIG_INCLUDE_PATH;
|
|
|
|
my $iOptionIdConfigPath = CFGOPT_REPO_HOST_CONFIG_PATH;
|
2018-02-04 01:27:38 +02:00
|
|
|
my $iOptionIdHost = CFGOPT_REPO_HOST;
|
|
|
|
my $iOptionIdUser = CFGOPT_REPO_HOST_USER;
|
2017-11-21 19:57:00 +02:00
|
|
|
my $strOptionDbPath = undef;
|
|
|
|
my $strOptionDbPort = undef;
|
|
|
|
my $strOptionDbSocketPath = undef;
|
2018-02-04 01:27:38 +02:00
|
|
|
my $strOptionSshPort = CFGOPT_REPO_HOST_PORT;
|
2017-11-21 19:57:00 +02:00
|
|
|
|
|
|
|
if ($strRemoteType eq CFGOPTVAL_REMOTE_TYPE_DB)
|
|
|
|
{
|
2018-02-04 01:27:38 +02:00
|
|
|
$iOptionIdCmd = cfgOptionIdFromIndex(CFGOPT_PG_HOST_CMD, $iRemoteIdx);
|
|
|
|
$iOptionIdConfig = cfgOptionIdFromIndex(CFGOPT_PG_HOST_CONFIG, $iRemoteIdx);
|
2018-04-19 17:09:39 +02:00
|
|
|
$iOptionIdConfigIncludePath = cfgOptionIdFromIndex(CFGOPT_PG_HOST_CONFIG_INCLUDE_PATH, $iRemoteIdx);
|
|
|
|
$iOptionIdConfigPath = cfgOptionIdFromIndex(CFGOPT_PG_HOST_CONFIG_PATH, $iRemoteIdx);
|
2018-02-04 01:27:38 +02:00
|
|
|
$iOptionIdHost = cfgOptionIdFromIndex(CFGOPT_PG_HOST, $iRemoteIdx);
|
|
|
|
$iOptionIdUser = cfgOptionIdFromIndex(CFGOPT_PG_HOST_USER, $iRemoteIdx);
|
|
|
|
$strOptionSshPort = cfgOptionIdFromIndex(CFGOPT_PG_HOST_PORT, $iRemoteIdx);
|
2017-11-21 19:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Db path is not valid in all contexts (restore, for instance)
|
2018-02-04 01:27:38 +02:00
|
|
|
if (cfgOptionValid(cfgOptionIdFromIndex(CFGOPT_PG_PATH, $iRemoteIdx)))
|
2017-11-21 19:57:00 +02:00
|
|
|
{
|
|
|
|
$strOptionDbPath =
|
2018-02-04 01:27:38 +02:00
|
|
|
cfgOptionSource(cfgOptionIdFromIndex(CFGOPT_PG_PATH, $iRemoteIdx)) eq CFGDEF_SOURCE_DEFAULT ?
|
|
|
|
undef : cfgOption(cfgOptionIdFromIndex(CFGOPT_PG_PATH, $iRemoteIdx));
|
2017-11-21 19:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Db port is not valid in all contexts (restore, for instance)
|
2018-02-04 01:27:38 +02:00
|
|
|
if (cfgOptionValid(cfgOptionIdFromIndex(CFGOPT_PG_PORT, $iRemoteIdx)))
|
2017-11-21 19:57:00 +02:00
|
|
|
{
|
|
|
|
$strOptionDbPort =
|
2018-02-04 01:27:38 +02:00
|
|
|
cfgOptionSource(cfgOptionIdFromIndex(CFGOPT_PG_PORT, $iRemoteIdx)) eq CFGDEF_SOURCE_DEFAULT ?
|
|
|
|
undef : cfgOption(cfgOptionIdFromIndex(CFGOPT_PG_PORT, $iRemoteIdx));
|
2017-11-21 19:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Db socket is not valid in all contexts (restore, for instance)
|
2018-02-04 01:27:38 +02:00
|
|
|
if (cfgOptionValid(cfgOptionIdFromIndex(CFGOPT_PG_SOCKET_PATH, $iRemoteIdx)))
|
2017-11-21 19:57:00 +02:00
|
|
|
{
|
|
|
|
$strOptionDbSocketPath =
|
2018-02-04 01:27:38 +02:00
|
|
|
cfgOptionSource(cfgOptionIdFromIndex(CFGOPT_PG_SOCKET_PATH, $iRemoteIdx)) eq CFGDEF_SOURCE_DEFAULT ?
|
|
|
|
undef : cfgOption(cfgOptionIdFromIndex(CFGOPT_PG_SOCKET_PATH, $iRemoteIdx));
|
2017-11-21 19:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Build hash to set and override command options
|
|
|
|
my $rhCommandOption =
|
|
|
|
{
|
|
|
|
&CFGOPT_COMMAND => {value => $strCommand},
|
2018-08-31 17:31:13 +02:00
|
|
|
&CFGOPT_PROCESS => {value => defined($iProcessIdx) ? $iProcessIdx : 0},
|
2017-11-21 19:57:00 +02:00
|
|
|
&CFGOPT_CONFIG =>
|
|
|
|
{value => cfgOptionValid($iOptionIdConfig) && cfgOptionSource($iOptionIdConfig) eq CFGDEF_SOURCE_DEFAULT ?
|
|
|
|
undef : cfgOption($iOptionIdConfig)},
|
2018-04-19 17:09:39 +02:00
|
|
|
&CFGOPT_CONFIG_INCLUDE_PATH =>
|
|
|
|
{value => cfgOptionValid($iOptionIdConfigIncludePath) &&
|
|
|
|
cfgOptionSource($iOptionIdConfigIncludePath) eq CFGDEF_SOURCE_DEFAULT ?
|
|
|
|
undef : cfgOption($iOptionIdConfigIncludePath)},
|
|
|
|
&CFGOPT_CONFIG_PATH =>
|
|
|
|
{value => cfgOptionValid($iOptionIdConfigPath) && cfgOptionSource($iOptionIdConfigPath) eq CFGDEF_SOURCE_DEFAULT ?
|
|
|
|
undef : cfgOption($iOptionIdConfigPath)},
|
2017-11-21 19:57:00 +02:00
|
|
|
&CFGOPT_TYPE => {value => $strRemoteType},
|
|
|
|
&CFGOPT_LOG_PATH => {},
|
|
|
|
&CFGOPT_LOCK_PATH => {},
|
|
|
|
|
2018-08-23 02:05:49 +02:00
|
|
|
# Only enable file logging on the remote when requested
|
|
|
|
&CFGOPT_LOG_LEVEL_FILE => {value => cfgOption(CFGOPT_LOG_SUBPROCESS) ? cfgOption(CFGOPT_LOG_LEVEL_FILE) : lc(OFF)},
|
|
|
|
|
2017-11-21 19:57:00 +02:00
|
|
|
# Don't pass CFGOPT_LOG_LEVEL_STDERR because in the case of the local process calling the remote process the
|
|
|
|
# option will be set to 'protocol' which is not a valid value from the command line.
|
|
|
|
&CFGOPT_LOG_LEVEL_STDERR => {},
|
|
|
|
|
2018-02-04 01:27:38 +02:00
|
|
|
cfgOptionIdFromIndex(CFGOPT_PG_PATH, 1) => {value => $strOptionDbPath},
|
|
|
|
cfgOptionIdFromIndex(CFGOPT_PG_PORT, 1) => {value => $strOptionDbPort},
|
|
|
|
cfgOptionIdFromIndex(CFGOPT_PG_SOCKET_PATH, 1) => {value => $strOptionDbSocketPath},
|
2017-11-21 19:57:00 +02:00
|
|
|
|
|
|
|
# Set protocol options explicitly so values are not picked up from remote config files
|
|
|
|
&CFGOPT_BUFFER_SIZE => {value => cfgOption(CFGOPT_BUFFER_SIZE)},
|
|
|
|
&CFGOPT_COMPRESS_LEVEL => {value => cfgOption(CFGOPT_COMPRESS_LEVEL)},
|
|
|
|
&CFGOPT_COMPRESS_LEVEL_NETWORK => {value => cfgOption(CFGOPT_COMPRESS_LEVEL_NETWORK)},
|
|
|
|
&CFGOPT_PROTOCOL_TIMEOUT => {value => cfgOption(CFGOPT_PROTOCOL_TIMEOUT)}
|
|
|
|
};
|
|
|
|
|
|
|
|
# Override some per-db options that shouldn't be passed to the command. ??? This could be done better as a new define
|
|
|
|
# for these options which would then be implemented in cfgCommandWrite().
|
2018-02-04 01:27:38 +02:00
|
|
|
for (my $iOptionIdx = 1; $iOptionIdx <= cfgOptionIndexTotal(CFGOPT_PG_HOST); $iOptionIdx++)
|
2017-11-21 19:57:00 +02:00
|
|
|
{
|
|
|
|
if ($iOptionIdx != 1)
|
|
|
|
{
|
2018-02-04 01:27:38 +02:00
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_HOST_CONFIG, $iOptionIdx)} = {};
|
2018-04-19 17:09:39 +02:00
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_HOST_CONFIG_INCLUDE_PATH, $iOptionIdx)} = {};
|
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_HOST_CONFIG_PATH, $iOptionIdx)} = {};
|
2018-02-04 01:27:38 +02:00
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_HOST, $iOptionIdx)} = {};
|
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_PATH, $iOptionIdx)} = {};
|
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_PORT, $iOptionIdx)} = {};
|
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_SOCKET_PATH, $iOptionIdx)} = {};
|
2017-11-21 19:57:00 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 01:27:38 +02:00
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_HOST_CMD, $iOptionIdx)} = {};
|
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_HOST_USER, $iOptionIdx)} = {};
|
|
|
|
$rhCommandOption->{cfgOptionIdFromIndex(CFGOPT_PG_HOST_PORT, $iOptionIdx)} = {};
|
2017-11-21 19:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Generate the remote command
|
|
|
|
my $strRemoteCommand = cfgCommandWrite(
|
|
|
|
CFGCMD_REMOTE, true, defined($strBackRestBin) ? $strBackRestBin : cfgOption($iOptionIdCmd), undef, $rhCommandOption);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strRemoteHost', value => cfgOption($iOptionIdHost)},
|
|
|
|
{name => 'strRemoteHostUser', value => cfgOption($iOptionIdUser)},
|
|
|
|
{name => 'strRemoteHostSshPort', value => cfgOption($strOptionSshPort, false)},
|
|
|
|
{name => 'strRemoteCommand', value => $strRemoteCommand},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# protocolGet
|
|
|
|
#
|
|
|
|
# Get the protocol object or create it if does not exist. Shared protocol objects are used because they create an SSH connection
|
2016-09-06 15:15:22 +02:00
|
|
|
# to the remote host and the number of these connections should be minimized.
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub protocolGet
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strRemoteType,
|
|
|
|
$iRemoteIdx,
|
2017-04-07 16:31:49 +02:00
|
|
|
$bCache,
|
|
|
|
$strBackRestBin,
|
|
|
|
$iProcessIdx,
|
|
|
|
$strCommand,
|
2016-08-24 18:39:27 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::protocolGet', \@_,
|
|
|
|
{name => 'strRemoteType'},
|
2017-08-25 22:47:47 +02:00
|
|
|
{name => 'iRemoteIdx', default => cfgOptionValid(CFGOPT_HOST_ID) ? cfgOption(CFGOPT_HOST_ID) : 1},
|
2017-04-07 16:31:49 +02:00
|
|
|
{name => 'bCache', optional => true, default => true},
|
|
|
|
{name => 'strBackRestBin', optional => true},
|
|
|
|
{name => 'iProcessIdx', optional => true},
|
2018-08-31 17:31:13 +02:00
|
|
|
{name => 'strCommand', optional => true,
|
|
|
|
default => cfgOptionValid(CFGOPT_COMMAND) ? cfgOption(CFGOPT_COMMAND) : cfgCommandName(cfgCommandGet())},
|
2016-08-24 18:39:27 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Protocol object
|
|
|
|
my $oProtocol;
|
|
|
|
|
|
|
|
# If no remote requested or if the requested remote type is local then return a local protocol object
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!cfgOptionTest(
|
2018-02-04 01:27:38 +02:00
|
|
|
cfgOptionIdFromIndex($strRemoteType eq CFGOPTVAL_REMOTE_TYPE_DB ? CFGOPT_PG_HOST : CFGOPT_REPO_HOST, $iRemoteIdx)))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
confess &log(ASSERT, 'protocol cannot be created when remote host is not specified');
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
# Else create the remote protocol
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Set protocol to cached value
|
|
|
|
$oProtocol =
|
|
|
|
$bCache && defined($$hProtocol{$strRemoteType}{$iRemoteIdx}) ? $$hProtocol{$strRemoteType}{$iRemoteIdx} : undef;
|
|
|
|
|
|
|
|
if ($bCache && $$hProtocol{$strRemoteType}{$iRemoteIdx})
|
|
|
|
{
|
|
|
|
$oProtocol = $$hProtocol{$strRemoteType}{$iRemoteIdx};
|
|
|
|
logDebugMisc($strOperation, 'found cached protocol');
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
# Issue a noop on protocol pulled from the cache to be sure it is still functioning. It's better to get an error at
|
|
|
|
# request time than somewhere randomly later.
|
|
|
|
$oProtocol->noOp();
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# If protocol was not returned from cache then create it
|
|
|
|
if (!defined($oProtocol))
|
|
|
|
{
|
|
|
|
logDebugMisc($strOperation, 'create (' . ($bCache ? '' : 'un') . 'cached) remote protocol');
|
|
|
|
|
2017-11-21 19:57:00 +02:00
|
|
|
my ($strRemoteHost, $strRemoteHostUser, $strRemoteHostSshPort, $strRemoteCommand) = protocolParam(
|
|
|
|
$strCommand, $strRemoteType, $iRemoteIdx, {strBackRestBin => $strBackRestBin, iProcessIdx => $iProcessIdx});
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2017-05-15 17:12:14 +02:00
|
|
|
$oProtocol = new pgBackRest::Protocol::Remote::Master
|
2016-08-24 18:39:27 +02:00
|
|
|
(
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOption(CFGOPT_CMD_SSH),
|
2017-11-21 19:57:00 +02:00
|
|
|
$strRemoteCommand,
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOption(CFGOPT_BUFFER_SIZE),
|
|
|
|
cfgOption(CFGOPT_COMPRESS_LEVEL),
|
|
|
|
cfgOption(CFGOPT_COMPRESS_LEVEL_NETWORK),
|
2017-11-21 19:57:00 +02:00
|
|
|
$strRemoteHost,
|
|
|
|
$strRemoteHostUser,
|
|
|
|
$strRemoteHostSshPort,
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOption(CFGOPT_PROTOCOL_TIMEOUT)
|
2016-08-24 18:39:27 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Cache the protocol
|
|
|
|
if ($bCache)
|
|
|
|
{
|
|
|
|
$$hProtocol{$strRemoteType}{$iRemoteIdx} = $oProtocol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oProtocol', value => $oProtocol, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(protocolGet);
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# protocolList - list all active protocols
|
|
|
|
####################################################################################################################################
|
|
|
|
sub protocolList
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strRemoteType,
|
|
|
|
$iRemoteIdx,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::protocolList', \@_,
|
|
|
|
{name => 'strRemoteType', required => false, trace => true},
|
|
|
|
{name => 'iRemoteIdx', required => false, trace => true},
|
|
|
|
);
|
|
|
|
|
|
|
|
my @oyProtocol;
|
|
|
|
|
|
|
|
if (!defined($strRemoteType))
|
|
|
|
{
|
|
|
|
foreach my $strRemoteType (sort(keys(%{$hProtocol})))
|
|
|
|
{
|
|
|
|
push(@oyProtocol, protocolList($strRemoteType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif (!defined($iRemoteIdx))
|
|
|
|
{
|
|
|
|
foreach my $iRemoteIdx (sort(keys(%{$hProtocol->{$strRemoteType}})))
|
|
|
|
{
|
|
|
|
push(@oyProtocol, protocolList($strRemoteType, $iRemoteIdx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif (defined($hProtocol->{$strRemoteType}{$iRemoteIdx}))
|
|
|
|
{
|
|
|
|
push(@oyProtocol, {strRemoteType => $strRemoteType, iRemoteIdx => $iRemoteIdx});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oyProtocol', value => \@oyProtocol, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# protocolDestroy
|
|
|
|
#
|
|
|
|
# Undefine the protocol if it is stored locally.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub protocolDestroy
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strRemoteType,
|
|
|
|
$iRemoteIdx,
|
2016-09-14 23:37:07 +02:00
|
|
|
$bComplete,
|
2016-08-24 18:39:27 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::protocolDestroy', \@_,
|
|
|
|
{name => 'strRemoteType', required => false},
|
|
|
|
{name => 'iRemoteIdx', required => false},
|
2016-09-14 23:37:07 +02:00
|
|
|
{name => 'bComplete', default => false},
|
2016-08-24 18:39:27 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
my $iExitStatus = 0;
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
foreach my $rhProtocol (protocolList($strRemoteType, $iRemoteIdx))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
logDebugMisc(
|
|
|
|
$strOperation, 'found cached protocol',
|
|
|
|
{name => 'strRemoteType', value => $rhProtocol->{strRemoteType}},
|
|
|
|
{name => 'iRemoteIdx', value => $rhProtocol->{iRemoteIdx}});
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
$iExitStatus = $hProtocol->{$rhProtocol->{strRemoteType}}{$rhProtocol->{iRemoteIdx}}->close($bComplete);
|
|
|
|
delete($hProtocol->{$rhProtocol->{strRemoteType}}{$rhProtocol->{iRemoteIdx}});
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'iExitStatus', value => $iExitStatus}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(protocolDestroy);
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# protocolKeepAlive - call keepAlive() on all protocols
|
|
|
|
####################################################################################################################################
|
|
|
|
sub protocolKeepAlive
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strRemoteType,
|
|
|
|
$iRemoteIdx,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::protocolDestroy', \@_,
|
|
|
|
{name => 'strRemoteType', required => false, trace => true},
|
|
|
|
{name => 'iRemoteIdx', required => false, trace => true},
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach my $rhProtocol (protocolList($strRemoteType, $iRemoteIdx))
|
|
|
|
{
|
|
|
|
$hProtocol->{$rhProtocol->{strRemoteType}}{$rhProtocol->{iRemoteIdx}}->keepAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(protocolKeepAlive);
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
1;
|