2014-03-05 19:53:13 -05:00
|
|
|
#!/usr/bin/perl
|
2014-03-29 18:16:08 -04:00
|
|
|
####################################################################################################################################
|
2015-07-02 10:05:13 -04:00
|
|
|
# pgBackRest - Simple PostgreSQL Backup and Restore
|
2014-03-29 18:16:08 -04:00
|
|
|
####################################################################################################################################
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2014-03-29 18:16:08 -04:00
|
|
|
####################################################################################################################################
|
|
|
|
# Perl includes
|
|
|
|
####################################################################################################################################
|
2014-03-05 19:53:13 -05:00
|
|
|
use strict;
|
2015-03-03 00:57:20 -05:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2015-08-29 14:20:46 -04:00
|
|
|
# Convert die to confess to capture the stack trace
|
2015-06-13 18:25:49 -04:00
|
|
|
$SIG{__DIE__} = sub { Carp::confess @_ };
|
|
|
|
|
|
|
|
use File::Basename qw(dirname);
|
2015-06-21 12:06:13 -04:00
|
|
|
use Scalar::Util qw(blessed);
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2014-09-14 15:55:27 -04:00
|
|
|
use lib dirname($0) . '/../lib';
|
2015-06-13 18:25:49 -04:00
|
|
|
use BackRest::Archive;
|
2015-08-29 14:20:46 -04:00
|
|
|
use BackRest::Common::Exception;
|
2015-10-08 11:43:56 -04:00
|
|
|
use BackRest::Common::Exit;
|
|
|
|
use BackRest::Common::Lock;
|
2015-08-29 14:20:46 -04:00
|
|
|
use BackRest::Common::Log;
|
2015-09-08 07:31:24 -04:00
|
|
|
use BackRest::Config::Config;
|
2014-06-22 10:30:17 -04:00
|
|
|
use BackRest::File;
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2014-07-27 18:13:23 -04:00
|
|
|
####################################################################################################################################
|
|
|
|
# START EVAL BLOCK TO CATCH ERRORS AND STOP THREADS
|
|
|
|
####################################################################################################################################
|
2015-06-13 18:25:49 -04:00
|
|
|
eval
|
2014-07-12 19:03:39 -04:00
|
|
|
{
|
2015-06-13 18:25:49 -04:00
|
|
|
################################################################################################################################
|
|
|
|
# Load command line parameters and config
|
|
|
|
################################################################################################################################
|
2015-09-08 07:31:24 -04:00
|
|
|
my $bConfigResult = configLoad();
|
|
|
|
|
2015-09-09 15:40:54 -04:00
|
|
|
# Display help and version
|
2015-09-08 07:31:24 -04:00
|
|
|
if (commandTest(CMD_HELP) || commandTest(CMD_VERSION))
|
|
|
|
{
|
|
|
|
# Load module dynamically
|
2015-09-08 12:58:13 -04:00
|
|
|
require BackRest::Config::ConfigHelp;
|
|
|
|
BackRest::Config::ConfigHelp->import();
|
2015-09-08 07:31:24 -04:00
|
|
|
|
|
|
|
# Generate help and exit
|
2015-09-09 15:40:54 -04:00
|
|
|
configHelp($ARGV[1], $ARGV[2], commandTest(CMD_VERSION), $bConfigResult);
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe(0);
|
2015-09-08 07:31:24 -04:00
|
|
|
}
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
# Set test options
|
|
|
|
!optionGet(OPTION_TEST, false) or
|
|
|
|
testSet(optionGet(OPTION_TEST), optionGet(OPTION_TEST_DELAY), optionGet(OPTION_TEST_POINT, false));
|
|
|
|
|
2015-06-17 11:26:07 -04:00
|
|
|
################################################################################################################################
|
|
|
|
# Process remote commands
|
|
|
|
################################################################################################################################
|
2015-06-18 16:55:09 -04:00
|
|
|
if (commandTest(CMD_REMOTE))
|
2015-06-17 11:26:07 -04:00
|
|
|
{
|
2015-10-08 11:43:56 -04:00
|
|
|
# Set log levels
|
|
|
|
logLevelSet(OFF, REMOTE);
|
2015-06-18 15:39:30 -04:00
|
|
|
|
2015-09-08 07:31:24 -04:00
|
|
|
# Load module dynamically
|
|
|
|
require BackRest::Protocol::RemoteMinion;
|
|
|
|
BackRest::Protocol::RemoteMinion->import();
|
|
|
|
|
2015-06-18 15:39:30 -04:00
|
|
|
# Create the remote object
|
2015-08-05 08:43:41 -04:00
|
|
|
my $oRemote = new BackRest::Protocol::RemoteMinion
|
2015-06-18 15:39:30 -04:00
|
|
|
(
|
2015-10-08 11:43:56 -04:00
|
|
|
optionGet(OPTION_COMMAND),
|
2015-06-18 15:39:30 -04:00
|
|
|
optionGet(OPTION_BUFFER_SIZE),
|
|
|
|
optionGet(OPTION_COMPRESS_LEVEL),
|
2015-10-08 11:43:56 -04:00
|
|
|
optionGet(OPTION_COMPRESS_LEVEL_NETWORK),
|
|
|
|
protocolTimeoutGet()
|
2015-06-18 15:39:30 -04:00
|
|
|
);
|
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
# !!! Would like to remove this check and allow a test lock
|
|
|
|
if (optionGet(OPTION_COMMAND) ne 'test' && !optionTest(OPTION_PROCESS))
|
|
|
|
{
|
|
|
|
lockAcquire(optionGet(OPTION_COMMAND), undef, true, optionGet(OPTION_PROCESS, false));
|
|
|
|
}
|
|
|
|
|
2015-06-18 15:39:30 -04:00
|
|
|
# Process remote requests
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe($oRemote->process());
|
2015-06-17 11:26:07 -04:00
|
|
|
}
|
|
|
|
|
2015-06-13 18:25:49 -04:00
|
|
|
# Set the log levels
|
2015-08-29 14:20:46 -04:00
|
|
|
logLevelSet(optionGet(OPTION_LOG_LEVEL_FILE), optionGet(OPTION_LOG_LEVEL_CONSOLE));
|
2015-04-22 16:39:53 -04:00
|
|
|
|
2015-08-29 14:20:46 -04:00
|
|
|
# Log the command start
|
|
|
|
commandStart();
|
2015-04-07 07:34:37 -04:00
|
|
|
|
2015-06-13 18:25:49 -04:00
|
|
|
################################################################################################################################
|
|
|
|
# Process archive commands
|
|
|
|
################################################################################################################################
|
2015-06-18 16:55:09 -04:00
|
|
|
if (commandTest(CMD_ARCHIVE_PUSH) || commandTest(CMD_ARCHIVE_GET))
|
2015-06-13 18:25:49 -04:00
|
|
|
{
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe(new BackRest::Archive()->process());
|
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################################################################
|
|
|
|
# Process start/stop commands
|
|
|
|
################################################################################################################################
|
|
|
|
if (commandTest(CMD_START))
|
|
|
|
{
|
|
|
|
lockStart();
|
|
|
|
exitSafe(0);
|
|
|
|
}
|
|
|
|
elsif (commandTest(CMD_STOP))
|
|
|
|
{
|
|
|
|
lockStop();
|
|
|
|
exitSafe(0);
|
2015-06-13 18:25:49 -04:00
|
|
|
}
|
2014-12-19 17:49:56 +00:00
|
|
|
|
2015-06-13 18:25:49 -04:00
|
|
|
################################################################################################################################
|
|
|
|
# Process info command
|
|
|
|
################################################################################################################################
|
2015-06-18 16:55:09 -04:00
|
|
|
if (commandTest(CMD_INFO))
|
2014-12-19 17:49:56 +00:00
|
|
|
{
|
2015-09-08 07:31:24 -04:00
|
|
|
# Load module dynamically
|
|
|
|
require BackRest::Info;
|
|
|
|
BackRest::Info->import();
|
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe(new BackRest::Info()->process());
|
2014-12-19 17:49:56 +00:00
|
|
|
}
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2015-06-13 18:25:49 -04:00
|
|
|
################################################################################################################################
|
2015-06-18 16:55:09 -04:00
|
|
|
# Acquire the command lock
|
2015-06-13 18:25:49 -04:00
|
|
|
################################################################################################################################
|
2015-06-18 16:55:09 -04:00
|
|
|
lockAcquire(commandGet());
|
2015-06-13 18:25:49 -04:00
|
|
|
|
|
|
|
################################################################################################################################
|
|
|
|
# Open the log file
|
|
|
|
################################################################################################################################
|
2015-08-29 14:20:46 -04:00
|
|
|
logFileSet(optionGet(OPTION_REPO_PATH) . '/log/' . optionGet(OPTION_STANZA) . '-' . lc(commandGet()));
|
2015-06-13 18:25:49 -04:00
|
|
|
|
|
|
|
################################################################################################################################
|
|
|
|
# Create the thread group that will be used for parallel processing
|
|
|
|
################################################################################################################################
|
2015-09-08 07:31:24 -04:00
|
|
|
if (optionTest(OPTION_THREAD_MAX) && optionGet(OPTION_THREAD_MAX) > 1)
|
|
|
|
{
|
2015-10-08 11:43:56 -04:00
|
|
|
# Set local thread-max so exitSafe knows to stop them on exit
|
|
|
|
exitInit(optionGet(OPTION_THREAD_MAX));
|
2015-09-08 07:31:24 -04:00
|
|
|
|
|
|
|
# Load module dynamically
|
|
|
|
require BackRest::Protocol::ThreadGroup;
|
|
|
|
BackRest::Protocol::ThreadGroup->import();
|
|
|
|
|
|
|
|
threadGroupCreate();
|
|
|
|
}
|
2015-06-13 18:25:49 -04:00
|
|
|
|
|
|
|
################################################################################################################################
|
|
|
|
# RESTORE
|
|
|
|
################################################################################################################################
|
2015-06-18 16:55:09 -04:00
|
|
|
if (commandTest(CMD_RESTORE))
|
2015-06-13 18:25:49 -04:00
|
|
|
{
|
|
|
|
if (optionRemoteTypeTest(DB))
|
|
|
|
{
|
2015-06-18 16:55:09 -04:00
|
|
|
confess &log(ASSERT, 'restore command must be performed locally on the db server');
|
2015-06-13 18:25:49 -04:00
|
|
|
}
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2015-09-08 07:31:24 -04:00
|
|
|
# Load module dynamically
|
|
|
|
require BackRest::Restore;
|
|
|
|
BackRest::Restore->import();
|
|
|
|
|
2015-06-13 18:25:49 -04:00
|
|
|
# Do the restore
|
2015-10-08 11:43:56 -04:00
|
|
|
new BackRest::Restore()->process();
|
2015-08-29 14:20:46 -04:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe(0);
|
2015-06-13 18:25:49 -04:00
|
|
|
}
|
2015-10-08 11:43:56 -04:00
|
|
|
else
|
2015-06-13 18:25:49 -04:00
|
|
|
{
|
2015-10-08 11:43:56 -04:00
|
|
|
############################################################################################################################
|
|
|
|
# Make sure backup and expire commands happen on the backup side
|
|
|
|
############################################################################################################################
|
|
|
|
if (optionRemoteTypeTest(BACKUP))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'backup and expire commands must run on the backup host');
|
|
|
|
}
|
2014-09-19 17:51:51 -04:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
############################################################################################################################
|
|
|
|
# BACKUP
|
|
|
|
############################################################################################################################
|
|
|
|
if (commandTest(CMD_BACKUP))
|
|
|
|
{
|
|
|
|
# Load module dynamically
|
|
|
|
require BackRest::Backup;
|
|
|
|
BackRest::Backup->import();
|
2015-09-08 07:31:24 -04:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
new BackRest::Backup()->process();
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
commandSet(CMD_EXPIRE);
|
|
|
|
}
|
2014-03-05 19:53:13 -05:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
############################################################################################################################
|
|
|
|
# EXPIRE
|
|
|
|
############################################################################################################################
|
|
|
|
if (commandTest(CMD_EXPIRE))
|
|
|
|
{
|
|
|
|
# Load module dynamically
|
|
|
|
require BackRest::Expire;
|
|
|
|
BackRest::Expire->import();
|
2015-09-08 07:31:24 -04:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
new BackRest::Expire()->process();
|
|
|
|
}
|
2015-03-17 18:31:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-13 18:25:49 -04:00
|
|
|
lockRelease();
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe(0);
|
2014-08-10 15:02:14 -04:00
|
|
|
};
|
2014-07-27 18:13:23 -04:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-08-10 15:02:14 -04:00
|
|
|
# CHECK FOR ERRORS AND STOP THREADS
|
2014-07-27 18:13:23 -04:00
|
|
|
####################################################################################################################################
|
|
|
|
if ($@)
|
|
|
|
{
|
2015-01-23 18:28:39 -05:00
|
|
|
my $oMessage = $@;
|
|
|
|
|
|
|
|
# If a backrest exception then return the code - don't confess
|
2015-08-29 14:20:46 -04:00
|
|
|
if (blessed($oMessage) && $oMessage->isa('BackRest::Common::Exception'))
|
2015-01-23 18:28:39 -05:00
|
|
|
{
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe($oMessage->code());
|
2015-01-23 18:28:39 -05:00
|
|
|
}
|
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
exitSafe(-1);
|
2015-05-29 11:41:19 -04:00
|
|
|
confess $oMessage;
|
2014-07-27 18:13:23 -04:00
|
|
|
}
|