2013-12-02 16:21:40 -05:00
|
|
|
#!/usr/bin/perl -w
|
2013-11-17 13:58:21 -05:00
|
|
|
|
2013-12-02 15:10:18 -05:00
|
|
|
use strict;
|
2013-12-02 21:23:10 -05:00
|
|
|
use File::Basename;
|
|
|
|
use Getopt::Long;
|
2013-12-04 21:37:45 -05:00
|
|
|
use Config::IniFiles;
|
2013-11-17 13:58:21 -05:00
|
|
|
|
2013-12-02 21:23:10 -05:00
|
|
|
# Process flags
|
|
|
|
my $bNoCompression;
|
|
|
|
my $bNoChecksum;
|
2013-12-04 21:37:45 -05:00
|
|
|
my $strConfigFile;
|
2013-12-02 21:23:10 -05:00
|
|
|
|
|
|
|
GetOptions ("no-compression" => \$bNoCompression,
|
2013-12-04 21:37:45 -05:00
|
|
|
"no-checksum" => \$bNoChecksum,
|
|
|
|
"config=s" => \$strConfigFile)
|
2013-12-02 21:23:10 -05:00
|
|
|
or die("Error in command line arguments\n");
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# TRIM - trim whitespace off strings
|
|
|
|
####################################################################################################################################
|
|
|
|
sub trim
|
|
|
|
{
|
|
|
|
my $strBuffer = shift;
|
|
|
|
|
|
|
|
$strBuffer =~ s/^\s+|\s+$//g;
|
|
|
|
|
|
|
|
return $strBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# EXECUTE - execute a command
|
|
|
|
####################################################################################################################################
|
2013-11-17 13:58:21 -05:00
|
|
|
sub execute
|
|
|
|
{
|
2013-12-02 15:10:18 -05:00
|
|
|
my $strCommand = shift;
|
|
|
|
|
2013-11-17 21:48:53 -05:00
|
|
|
# print("$strCommand\n");
|
2013-12-02 15:10:18 -05:00
|
|
|
my $strOutput = qx($strCommand) or return 0;
|
2013-11-17 21:48:53 -05:00
|
|
|
# print("$strOutput\n");
|
|
|
|
|
|
|
|
return($strOutput);
|
|
|
|
}
|
|
|
|
|
2013-12-02 21:23:10 -05:00
|
|
|
####################################################################################################################################
|
2013-11-23 19:16:09 -05:00
|
|
|
# FILE_HASH_GET - get the sha1 hash for a file
|
2013-12-02 21:23:10 -05:00
|
|
|
####################################################################################################################################
|
2013-11-17 21:48:53 -05:00
|
|
|
sub file_hash_get
|
|
|
|
{
|
2013-12-04 21:37:45 -05:00
|
|
|
my $strCommand = shift;
|
2013-12-02 15:10:18 -05:00
|
|
|
my $strFile = shift;
|
2013-11-17 21:48:53 -05:00
|
|
|
|
|
|
|
$strCommand =~ s/\%file\%/$strFile/g;
|
|
|
|
|
2013-12-02 21:23:10 -05:00
|
|
|
my $strHash = trim(execute($strCommand));
|
2013-11-17 21:48:53 -05:00
|
|
|
|
|
|
|
return($strHash);
|
|
|
|
}
|
|
|
|
|
2013-12-02 21:23:10 -05:00
|
|
|
####################################################################################################################################
|
2013-11-23 19:16:09 -05:00
|
|
|
# START MAIN
|
2013-12-02 21:23:10 -05:00
|
|
|
####################################################################################################################################
|
2013-11-17 21:48:53 -05:00
|
|
|
# Get the command
|
2013-12-02 15:10:18 -05:00
|
|
|
my $strCommand = $ARGV[0];
|
2013-11-17 21:48:53 -05:00
|
|
|
|
2013-12-04 21:37:45 -05:00
|
|
|
# Load the config file
|
|
|
|
if (!defined($strConfigFile))
|
|
|
|
{
|
|
|
|
$strConfigFile = "/etc/pg_backrest.conf";
|
|
|
|
}
|
|
|
|
|
|
|
|
my %oConfig;
|
|
|
|
tie %oConfig, 'Config::IniFiles', (-file => $strConfigFile) or die "Unable to find config file";
|
|
|
|
|
2013-12-02 21:23:10 -05:00
|
|
|
####################################################################################################################################
|
2013-12-02 21:37:01 -05:00
|
|
|
# ARCHIVE-LOCAL Command !!! This should become archive-push with no hostname
|
2013-12-02 21:23:10 -05:00
|
|
|
####################################################################################################################################
|
2013-11-17 21:48:53 -05:00
|
|
|
if ($strCommand eq "archive-local")
|
|
|
|
{
|
2013-12-02 21:23:10 -05:00
|
|
|
# archive-local command must have three arguments
|
|
|
|
if (@ARGV != 3)
|
|
|
|
{
|
|
|
|
die "not enough arguments - show usage";
|
|
|
|
}
|
|
|
|
|
2013-12-02 16:21:40 -05:00
|
|
|
# Get the source dir/file
|
2013-12-02 21:23:10 -05:00
|
|
|
my $strSourceFile = $ARGV[1];
|
2013-12-02 16:21:40 -05:00
|
|
|
|
2013-12-02 21:23:10 -05:00
|
|
|
unless (-e $strSourceFile)
|
|
|
|
{
|
|
|
|
die "source file does not exist - show usage";
|
|
|
|
}
|
2013-12-02 16:21:40 -05:00
|
|
|
|
|
|
|
# Get the destination dir/file
|
2013-12-02 21:23:10 -05:00
|
|
|
my $strDestinationFile = $ARGV[2];
|
|
|
|
|
|
|
|
# Make sure the destination directory exists
|
|
|
|
unless (-e dirname($strDestinationFile))
|
|
|
|
{
|
|
|
|
die "destination dir does not exist - show usage";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure the destination file does NOT exist - ignore checksum and extension in case they (or options) have changed
|
|
|
|
if (glob("$strDestinationFile*"))
|
|
|
|
{
|
|
|
|
die "destination file already exists";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Calculate sha1 hash for the file (unless disabled)
|
|
|
|
if (!$bNoChecksum)
|
|
|
|
{
|
2013-12-04 21:37:45 -05:00
|
|
|
$strDestinationFile .= "-" . file_hash_get($oConfig{command}{checksum}, $strSourceFile);
|
2013-12-02 21:23:10 -05:00
|
|
|
}
|
2013-12-02 16:21:40 -05:00
|
|
|
|
2013-12-02 21:26:32 -05:00
|
|
|
# Setup the copy command
|
2013-12-02 21:23:10 -05:00
|
|
|
my $strCommand = "";
|
|
|
|
|
|
|
|
if ($bNoCompression)
|
|
|
|
{
|
2013-12-04 21:37:45 -05:00
|
|
|
$strCommand = $oConfig{command}{copy};
|
2013-12-02 21:23:10 -05:00
|
|
|
$strCommand =~ s/\%source\%/$strSourceFile/g;
|
|
|
|
$strCommand =~ s/\%destination\%/$strDestinationFile/g;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-04 21:37:45 -05:00
|
|
|
$strCommand = $oConfig{command}{compress};
|
2013-12-02 21:23:10 -05:00
|
|
|
$strCommand =~ s/\%file\%/$strSourceFile/g;
|
|
|
|
$strCommand .= " > $strDestinationFile.gz";
|
|
|
|
}
|
2013-11-17 13:58:21 -05:00
|
|
|
|
2013-12-02 21:26:32 -05:00
|
|
|
# Execute the copy
|
2013-11-23 19:16:09 -05:00
|
|
|
execute($strCommand);
|
2013-12-04 21:37:45 -05:00
|
|
|
|
|
|
|
# print "$strCommandManifest\n";
|
|
|
|
# print execute($strCommandManifest) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP
|
|
|
|
####################################################################################################################################
|
|
|
|
if ($strCommand eq "backup")
|
|
|
|
{
|
2013-11-17 13:58:21 -05:00
|
|
|
}
|