2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# DB MODULE
|
|
|
|
####################################################################################################################################
|
2014-06-08 00:29:11 +03:00
|
|
|
package BackRest::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
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
use Net::OpenSSH;
|
|
|
|
use File::Basename;
|
|
|
|
use IPC::System::Simple qw(capture);
|
2015-03-12 18:15:19 +02:00
|
|
|
use Exporter qw(import);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
use lib dirname($0);
|
2015-04-03 04:07:23 +02:00
|
|
|
use BackRest::Exception;
|
2014-06-22 17:30:17 +03:00
|
|
|
use BackRest::Utility;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Postmaster process Id file
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant FILE_POSTMASTER_PID => 'postmaster.pid';
|
|
|
|
|
|
|
|
our @EXPORT = qw(FILE_POSTMASTER_PID);
|
|
|
|
|
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
|
|
|
|
my $strDbPath = shift; # Database path
|
2014-10-10 23:03:33 +03:00
|
|
|
my $strCommandPsql = shift; # PSQL command
|
|
|
|
my $strDbHost = shift; # Database host name
|
|
|
|
my $strDbUser = shift; # Database user name (generally postgres)
|
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
|
|
|
# Initialize variables
|
|
|
|
$self->{strCommandPsql} = $strCommandPsql;
|
|
|
|
$self->{strDbHost} = $strDbHost;
|
|
|
|
$self->{strDbUser} = $strDbUser;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Connect SSH object if db host is defined
|
|
|
|
if (defined($self->{strDbHost}) && !defined($self->{oDbSSH}))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
my $strOptionSSHRequestTTY = 'RequestTTY=yes';
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
&log(TRACE, "connecting to database ssh host $self->{strDbHost}");
|
|
|
|
|
|
|
|
# !!! This could be improved by redirecting stderr to a file to get a better error message
|
2014-04-01 15:59:09 +03:00
|
|
|
$self->{oDbSSH} = Net::OpenSSH->new($self->{strDbHost}, user => $self->{strDbUser},
|
|
|
|
master_opts => [-o => $strOptionSSHRequestTTY]);
|
2014-03-06 03:53:13 +03:00
|
|
|
$self->{oDbSSH}->error and confess &log(ERROR, "unable to connect to $self->{strDbHost}: " . $self->{oDbSSH}->error);
|
|
|
|
}
|
2014-10-10 23:03:33 +03:00
|
|
|
|
|
|
|
return $self;
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# IS_REMOTE
|
|
|
|
#
|
|
|
|
# Determine whether database operations are remote.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub is_remote
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# If the SSH object is defined then db is remote
|
|
|
|
return defined($self->{oDbSSH}) ? true : false;
|
|
|
|
}
|
|
|
|
|
2015-04-03 04:07:23 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# versionSupport
|
|
|
|
#
|
|
|
|
# Returns an array of the supported Postgres versions.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub versionSupport
|
|
|
|
{
|
|
|
|
my @strySupportVersion = ('8.3', '8.4', '9.0', '9.1', '9.2', '9.3', '9.4');
|
|
|
|
|
|
|
|
return \@strySupportVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(versionSupport);
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# PSQL_EXECUTE
|
|
|
|
####################################################################################################################################
|
|
|
|
sub psql_execute
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strScript = shift; # psql script to execute
|
|
|
|
|
|
|
|
# Get the user-defined command for psql
|
|
|
|
my $strCommand = $self->{strCommandPsql} . " -c \"${strScript}\" postgres";
|
|
|
|
my $strResult;
|
|
|
|
|
|
|
|
# !!! Need to capture error output with open3 and log it
|
|
|
|
|
|
|
|
# Run remotely
|
|
|
|
if ($self->is_remote())
|
|
|
|
{
|
|
|
|
&log(TRACE, "psql execute: remote ${strScript}");
|
|
|
|
|
|
|
|
$strResult = $self->{oDbSSH}->capture($strCommand)
|
|
|
|
or confess &log(ERROR, "unable to execute remote psql command '${strCommand}'");
|
|
|
|
}
|
|
|
|
# Else run locally
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&log(TRACE, "psql execute: ${strScript}");
|
|
|
|
$strResult = capture($strCommand) or confess &log(ERROR, "unable to execute local psql command '${strCommand}'");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# TABLESPACE_MAP_GET - Get the mapping between oid and tablespace name
|
|
|
|
####################################################################################################################################
|
|
|
|
sub tablespace_map_get
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-05-22 20:49:14 +02:00
|
|
|
|
|
|
|
my $oHashRef = {};
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-06-22 23:05:00 +03:00
|
|
|
data_hash_build($oHashRef, "oid\tname\n" . $self->psql_execute(
|
2014-09-16 15:55:40 +03:00
|
|
|
'copy (select oid, spcname from pg_tablespace) to stdout'), "\t");
|
2015-05-22 20:49:14 +02:00
|
|
|
|
|
|
|
return $oHashRef;
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-08-10 01:35:55 +03:00
|
|
|
# DB_VERSION_GET
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2014-06-22 17:30:17 +03:00
|
|
|
sub db_version_get
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if (defined($self->{fVersion}))
|
|
|
|
{
|
|
|
|
return $self->{fVersion};
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:02:56 +03:00
|
|
|
$self->{fVersion} =
|
2014-03-06 03:53:13 +03:00
|
|
|
trim($self->psql_execute("copy (select (regexp_matches(split_part(version(), ' ', 2), '^[0-9]+\.[0-9]+'))[1]) to stdout"));
|
|
|
|
|
|
|
|
&log(DEBUG, "database version is $self->{fVersion}");
|
|
|
|
|
2015-04-03 04:07:23 +02:00
|
|
|
my $strVersionSupport = versionSupport();
|
|
|
|
|
|
|
|
if ($self->{fVersion} < ${$strVersionSupport}[0])
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unsupported Postgres version ${$strVersionSupport}[0]", ERROR_VERSION_NOT_SUPPORTED);
|
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
return $self->{fVersion};
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_START
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_start
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strLabel = shift;
|
2014-03-28 22:20:36 +03:00
|
|
|
my $bStartFast = shift;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-04-01 21:58:33 +02:00
|
|
|
$self->db_version_get();
|
|
|
|
|
|
|
|
if ($self->{fVersion} < 8.4 && $bStartFast)
|
|
|
|
{
|
|
|
|
&log(WARN, 'start-fast option is only available in PostgreSQL >= 8.4');
|
|
|
|
$bStartFast = false;
|
|
|
|
}
|
|
|
|
|
2015-05-26 18:26:59 +02:00
|
|
|
&log(INFO, "executing pg_start_backup() with label \"${strLabel}\": backup will begin after " .
|
|
|
|
($bStartFast ? "the requested immediate checkpoint" : "the next regular checkpoint") . " completes");
|
|
|
|
|
2015-01-03 23:49:26 +02:00
|
|
|
my @stryField = split("\t", trim($self->psql_execute("set client_min_messages = 'warning';" .
|
2015-05-26 18:26:59 +02:00
|
|
|
"copy (select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS.US TZ'), " .
|
|
|
|
"pg_xlogfile_name(xlog) from pg_start_backup('${strLabel}'" .
|
2015-01-03 23:49:26 +02:00
|
|
|
($bStartFast ? ', true' : '') . ') as xlog) to stdout')));
|
|
|
|
|
|
|
|
return $stryField[1], $stryField[0];
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_STOP
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_stop
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-05-26 18:26:59 +02:00
|
|
|
&log(INFO, 'executing pg_stop_backup() and waiting for all WAL segments to be archived');
|
|
|
|
|
2015-01-03 23:49:26 +02:00
|
|
|
my @stryField = split("\t", trim($self->psql_execute("set client_min_messages = 'warning';" .
|
|
|
|
"copy (select to_char(clock_timestamp(), 'YYYY-MM-DD HH24:MI:SS.US TZ'), pg_xlogfile_name(xlog) from pg_stop_backup() as xlog) to stdout")));
|
|
|
|
|
|
|
|
return $stryField[1], $stryField[0];
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:03:33 +03:00
|
|
|
1;
|