1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-05 15:05:48 +02:00
pgbackrest/src/main.c

264 lines
9.2 KiB
C
Raw Normal View History

/***********************************************************************************************************************************
Main
***********************************************************************************************************************************/
#include "build.auto.h"
2017-11-28 21:44:05 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2017-11-28 21:44:05 -05:00
#include "command/archive/get/get.h"
#include "command/archive/push/push.h"
#include "command/check/check.h"
#include "command/command.h"
#include "command/control/start.h"
#include "command/control/stop.h"
#include "command/expire/expire.h"
2018-01-23 13:34:24 -05:00
#include "command/help/help.h"
#include "command/info/info.h"
#include "command/local/local.h"
#include "command/remote/remote.h"
#include "command/stanza/create.h"
#include "command/stanza/delete.h"
#include "command/stanza/upgrade.h"
#include "command/storage/list.h"
#include "common/debug.h"
2017-11-28 21:44:05 -05:00
#include "common/error.h"
2018-01-17 09:15:51 -05:00
#include "common/exit.h"
2017-11-28 21:44:05 -05:00
#include "config/config.h"
2018-01-16 13:52:20 -05:00
#include "config/load.h"
#include "postgres/interface.h"
#include "perl/exec.h"
#include "storage/helper.h"
2017-11-28 21:44:05 -05:00
#include "version.h"
int
main(int argListSize, const char *argList[])
{
#ifdef WITH_BACKTRACE
stackTraceInit(argList[0]);
#endif
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(INT, argListSize);
FUNCTION_LOG_PARAM(CHARPY, argList);
FUNCTION_LOG_END();
// Initialize command with the start time
cmdInit();
volatile bool result = 0;
volatile bool error = false;
// Initialize exit handler
exitInit();
2017-11-28 21:44:05 -05:00
TRY_BEGIN()
{
2018-01-16 13:52:20 -05:00
// Load the configuration
// -------------------------------------------------------------------------------------------------------------------------
cfgLoad((unsigned int)argListSize, argList);
2017-11-28 21:44:05 -05:00
2018-01-23 13:34:24 -05:00
// Display help
// -------------------------------------------------------------------------------------------------------------------------
if (cfgCommandHelp())
{
cmdHelp();
}
else
{
switch (cfgCommand())
{
// Archive get command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdArchiveGet:
{
result = cmdArchiveGet();
break;
}
// Archive get async command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdArchiveGetAsync:
{
cmdArchiveGetAsync();
break;
}
// Archive push command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdArchivePush:
{
cmdArchivePush();
break;
}
// Archive push async command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdArchivePushAsync:
{
cmdArchivePushAsync();
break;
}
// Backup command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdBackup:
{
#ifdef DEBUG
// Check pg_control during testing so errors are more obvious. Otherwise errors only happen in
// archive-get/archive-push and end up in the PostgreSQL log which is not output in CI. This can be removed
// once backup is written in C.
if (cfgOptionBool(cfgOptOnline) && !cfgOptionBool(cfgOptBackupStandby) && !cfgOptionTest(cfgOptPgHost))
pgControlFromFile(storagePg(), cfgOptionStr(cfgOptPgPath));
#endif
// Run backup
perlExec();
// Switch to expire command
cmdEnd(0, NULL);
cfgCommandSet(cfgCmdExpire);
cfgLoadLogFile();
cmdBegin(true);
// Run expire
perlExec();
cmdExpire();
break;
}
// Check command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdCheck:
{
// Functionality is currently split between Perl and C
perlExec();
cmdCheck();
break;
}
// Expire command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdExpire:
{
perlExec();
cmdExpire();
break;
}
// Help command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp:
case cfgCmdNone:
{
THROW(AssertError, "'help' and 'none' commands should have been handled already");
}
// Info command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdInfo:
{
cmdInfo();
break;
}
// Local command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdLocal:
{
cmdLocal(STDIN_FILENO, STDOUT_FILENO);
break;
}
// Remote command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdRemote:
{
if (cfgOptionBool(cfgOptC))
{
cmdRemote(STDIN_FILENO, STDOUT_FILENO);
}
else
perlExec();
break;
}
// Restore command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdRestore:
{
perlExec();
break;
}
// Stanza create command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdStanzaCreate:
{
cmdStanzaCreate();
break;
}
// Stanza delete command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdStanzaDelete:
{
cmdStanzaDelete();
break;
}
// Stanza upgrade command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdStanzaUpgrade:
{
cmdStanzaUpgrade();
break;
}
// Start command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdStart:
{
cmdStart();
break;
}
// Stop command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdStop:
{
cmdStop();
break;
}
// Storage list command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdLs:
{
cmdStorageList();
break;
}
// Display version
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdVersion:
{
printf(PROJECT_NAME " " PROJECT_VERSION "\n");
fflush(stdout);
break;
}
}
}
2017-11-28 21:44:05 -05:00
}
CATCH_ANY()
{
error = true;
2017-11-28 21:44:05 -05:00
}
TRY_END();
2018-01-17 09:15:51 -05:00
FUNCTION_LOG_RETURN(INT, exitSafe(result, error, 0));
}