1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-06-25 00:16:54 +02:00
Files
pgbackrest/pg_backrest.pl

255 lines
8.1 KiB
Perl
Raw Normal View History

2013-12-11 20:39:07 -05:00
#!/usr/bin/perl
2013-11-17 13:58:21 -05:00
2013-12-02 15:10:18 -05:00
use strict;
2013-12-11 20:39:07 -05:00
use warnings;
2014-02-02 19:03:05 -05:00
use File::Basename;
use Getopt::Long;
2013-12-04 21:37:45 -05:00
use Config::IniFiles;
2014-01-22 19:03:20 -05:00
use Carp;
2013-11-17 13:58:21 -05:00
2014-02-02 19:03:05 -05:00
use lib dirname($0);
use pg_backrest_utility;
use pg_backrest_file;
2014-02-03 19:57:21 -05:00
use pg_backrest_backup;
2014-02-06 15:54:22 -05:00
use pg_backrest_db;
2014-02-02 19:03:05 -05:00
2014-02-03 20:23:04 -05:00
# Command line parameters
2013-12-04 21:37:45 -05:00
my $strConfigFile;
my $strStanza;
2014-01-03 19:28:49 -05:00
my $strType = "incremental"; # Type of backup: full, differential (diff), incremental (incr)
2014-02-12 07:45:29 -05:00
GetOptions ("config=s" => \$strConfigFile,
"stanza=s" => \$strStanza,
2014-01-03 19:28:49 -05:00
"type=s" => \$strType)
or die("Error in command line arguments\n");
# Global variables
my %oConfig;
2013-12-11 20:39:07 -05:00
####################################################################################################################################
# CONFIG_LOAD - Get a value from the config and be sure that it is defined (unless bRequired is false)
####################################################################################################################################
sub config_load
{
my $strSection = shift;
my $strKey = shift;
my $bRequired = shift;
2014-02-12 07:39:42 -05:00
my $strDefault = shift;
2014-02-06 16:37:37 -05:00
# Default is that the key is not required
2013-12-11 20:39:07 -05:00
if (!defined($bRequired))
{
$bRequired = false;
}
2014-02-06 16:37:37 -05:00
my $strValue;
2014-02-06 16:37:37 -05:00
# Look in the default stanza section
if ($strSection eq "stanza")
{
$strValue = $oConfig{"${strStanza}"}{"${strKey}"};
}
# Else look in the supplied section
else
{
# First check the stanza section
$strValue = $oConfig{"${strStanza}:${strSection}"}{"${strKey}"};
# If the stanza section value is undefined then check global
if (!defined($strValue))
{
$strValue = $oConfig{"global:${strSection}"}{"${strKey}"};
}
2013-12-11 20:39:07 -05:00
}
2014-02-06 16:37:37 -05:00
if (!defined($strValue) && $bRequired)
{
2014-02-12 07:39:42 -05:00
if (defined($strDefault))
{
return $strDefault;
}
confess &log(ERROR, "config value " . (defined($strSection) ? $strSection : "[stanza]") . "->${strKey} is undefined");
}
2014-02-06 16:37:37 -05:00
if ($strSection eq "command")
2013-12-11 20:39:07 -05:00
{
my $strOption = config_load("command:option", $strKey);
if (defined($strOption))
{
$strValue =~ s/\%option\%/${strOption}/g;
}
2013-12-11 20:39:07 -05:00
}
2014-02-06 16:37:37 -05:00
2013-12-11 20:39:07 -05:00
return $strValue;
}
####################################################################################################################################
# START MAIN
####################################################################################################################################
# Error if no operation is specified
if (@ARGV < 1)
{
confess "operation my be specified (backup, expire, archive_push, ...) - show usage";
}
2013-11-17 21:48:53 -05:00
# Get the command
2013-12-04 22:30:26 -05:00
my $strOperation = $ARGV[0];
my $strLogFile = "";
# !!! Pick the log file name here (backup, restore, archive-YYYYMMDD)
#
if ($strOperation eq "archive-push")
{
2014-02-06 16:37:37 -05:00
}
2013-11-17 21:48:53 -05:00
2013-12-04 22:30:26 -05:00
####################################################################################################################################
# LOAD CONFIG FILE
####################################################################################################################################
2013-12-04 21:37:45 -05:00
if (!defined($strConfigFile))
{
$strConfigFile = "/etc/pg_backrest.conf";
}
2014-02-03 14:50:23 -05:00
tie %oConfig, 'Config::IniFiles', (-file => $strConfigFile) or confess &log(ERROR, "unable to find config file ${strConfigFile}");
2014-01-03 21:19:41 -05:00
# Load and check the cluster
if (!defined($strStanza))
2014-01-03 21:19:41 -05:00
{
confess "a backup stanza must be specified - show usage";
2014-01-03 21:19:41 -05:00
}
####################################################################################################################################
2013-12-10 19:11:54 -05:00
# ARCHIVE-PUSH Command
####################################################################################################################################
2013-12-10 19:11:54 -05:00
if ($strOperation eq "archive-push")
2013-11-17 21:48:53 -05:00
{
# archive-push command must have two arguments
if (@ARGV != 2)
{
confess "not enough arguments - show usage";
}
2014-02-12 07:39:42 -05:00
# If an archive section has been defined, use that instead of the backup section
my $strSection = defined(config_load("archive", "path")) ? "archive" : "backup";
# Get the operational flags
my $bCompress = config_load($strSection, "compress", true, "y") eq "y" ? true : false;
my $bChecksum = config_load($strSection, "checksum", true, "y") eq "y" ? true : false;
2014-02-05 21:39:08 -05:00
# Run file_init_archive - this is the minimal config needed to run archiving
my $oFile = pg_backrest_file->new
(
strStanza => $strStanza,
2014-02-12 07:39:42 -05:00
bNoCompression => !$bCompress,
strBackupUser => config_load($strSection, "user"),
strBackupHost => config_load($strSection, "host"),
strBackupPath => config_load($strSection, "path", true),
strCommandChecksum => config_load("command", "checksum", $bChecksum),
strCommandCompress => config_load("command", "compress", $bCompress),
strCommandDecompress => config_load("command", "decompress", $bCompress)
2014-02-05 21:39:08 -05:00
);
backup_init
(
2014-02-06 15:54:22 -05:00
undef,
2014-02-12 07:39:42 -05:00
$oFile,
undef,
undef,
!$bChecksum
2014-02-05 21:39:08 -05:00
);
2014-02-03 20:29:03 -05:00
# Call the archive function
archive_push($ARGV[1]);
2013-12-04 21:37:45 -05:00
2013-12-04 22:30:26 -05:00
exit 0;
}
####################################################################################################################################
# GET MORE CONFIG INFO
####################################################################################################################################
2014-01-03 19:28:49 -05:00
# Check the backup type
if ($strType eq "diff")
{
$strType = "differential";
}
if ($strType eq "incr")
{
$strType = "incremental";
}
if ($strType ne "full" && $strType ne "differential" && $strType ne "incremental")
{
2014-02-03 19:03:17 -05:00
confess &log(ERROR, "backup type must be full, differential (diff), incremental (incr)");
2014-01-03 19:28:49 -05:00
}
2014-02-12 07:39:42 -05:00
# Get the operational flags
my $bCompress = config_load("backup", "compress", true, "y") eq "y" ? true : false;
my $bChecksum = config_load("backup", "checksum", true, "y") eq "y" ? true : false;
2014-02-03 20:48:02 -05:00
# Run file_init_archive - the rest of the file config required for backup and restore
2014-02-05 21:39:08 -05:00
my $oFile = pg_backrest_file->new
2014-02-03 14:50:23 -05:00
(
strStanza => $strStanza,
2014-02-12 07:39:42 -05:00
bNoCompression => !$bCompress,
strBackupUser => config_load("backup", "user"),
strBackupHost => config_load("backup", "host"),
strBackupPath => config_load("backup", "path", true),
strDbUser => config_load("stanza", "user"),
strDbHost => config_load("stanza", "host"),
2014-02-12 07:39:42 -05:00
strCommandChecksum => config_load("command", "checksum", $bChecksum),
strCommandCompress => config_load("command", "compress", $bCompress),
strCommandDecompress => config_load("command", "decompress", $bCompress),
strCommandManifest => config_load("command", "manifest"),
strCommandPsql => config_load("command", "psql")
2014-02-03 14:50:23 -05:00
);
2014-02-01 11:04:33 -05:00
2014-02-06 15:54:22 -05:00
my $oDb = pg_backrest_db->new
(
strDbUser => config_load("stanza", "user"),
strDbHost => config_load("stanza", "host"),
strCommandPsql => config_load("command", "psql")
);
2014-02-03 19:57:21 -05:00
# Run backup_init - parameters required for backup and restore operations
backup_init
(
2014-02-06 15:54:22 -05:00
$oDb,
2014-02-05 21:39:08 -05:00
$oFile,
2014-02-03 19:57:21 -05:00
$strType,
2014-02-12 07:45:29 -05:00
config_load("backup", "hardlink", true, "n") eq "y" ? true : false,
2014-02-12 07:39:42 -05:00
!$bChecksum,
config_load("backup", "thread"),
config_load("backup", "archive_required", true, "y") eq "y" ? true : false
2014-02-03 19:57:21 -05:00
);
2013-12-04 21:37:45 -05:00
####################################################################################################################################
# BACKUP
####################################################################################################################################
2013-12-04 22:30:26 -05:00
if ($strOperation eq "backup")
2013-12-04 21:37:45 -05:00
{
backup(config_load("stanza", "path"));
$strOperation = "expire";
}
####################################################################################################################################
# EXPIRE
####################################################################################################################################
if ($strOperation eq "expire")
{
backup_expire
(
$oFile->path_get(PATH_BACKUP_CLUSTER),
config_load("retention", "full_retention"),
config_load("retention", "differential_retention"),
config_load("retention", "archive_retention_type"),
config_load("retention", "archive_retention")
);
2014-01-15 20:41:29 -05:00
exit 0;
2014-02-06 16:37:37 -05:00
}