You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-11-06 08:49:29 +02:00
Basic error checking and options for archive-local command
This commit is contained in:
109
pg_backrest.pl
109
pg_backrest.pl
@@ -1,16 +1,40 @@
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
use strict;
|
||||
#use Getopt::Long;
|
||||
use Cwd 'abs_path';
|
||||
use File::Basename;
|
||||
use Getopt::Long;
|
||||
|
||||
# Process flags
|
||||
my $bNoCompression;
|
||||
my $bNoChecksum;
|
||||
|
||||
GetOptions ("no-compression" => \$bNoCompression,
|
||||
"no-checksum" => \$bNoChecksum)
|
||||
or die("Error in command line arguments\n");
|
||||
|
||||
# Command strings - temporary until these are in the config file
|
||||
#my $strCommandCompress = "pigz --rsyncable --best --stdout %file%"; # Ubuntu Linux
|
||||
my $strCommandCompress = "gzip --stdout %file%"; # OSX
|
||||
my $strCommandCompress = "gzip --stdout %file%"; # OSX --best was removed for testing
|
||||
my $strCommandCopy = "cp %source% %destination%";
|
||||
#my $strCommandHash = "sha1sum %file% | awk '{print \$1}'"; # Ubuntu Linux
|
||||
my $strCommandHash = "shasum %file% | awk '{print \$1}'"; # OSX
|
||||
|
||||
################################################################################
|
||||
# EXECUTE A COMMAND
|
||||
################################################################################
|
||||
####################################################################################################################################
|
||||
# TRIM - trim whitespace off strings
|
||||
####################################################################################################################################
|
||||
sub trim
|
||||
{
|
||||
my $strBuffer = shift;
|
||||
|
||||
$strBuffer =~ s/^\s+|\s+$//g;
|
||||
|
||||
return $strBuffer;
|
||||
}
|
||||
|
||||
####################################################################################################################################
|
||||
# EXECUTE - execute a command
|
||||
####################################################################################################################################
|
||||
sub execute
|
||||
{
|
||||
my $strCommand = shift;
|
||||
@@ -22,9 +46,9 @@ sub execute
|
||||
return($strOutput);
|
||||
}
|
||||
|
||||
################################################################################
|
||||
####################################################################################################################################
|
||||
# FILE_HASH_GET - get the sha1 hash for a file
|
||||
################################################################################
|
||||
####################################################################################################################################
|
||||
sub file_hash_get
|
||||
{
|
||||
my $strFile = shift;
|
||||
@@ -32,47 +56,74 @@ sub file_hash_get
|
||||
my $strCommand = $strCommandHash;
|
||||
$strCommand =~ s/\%file\%/$strFile/g;
|
||||
|
||||
my $strHash = execute($strCommand);
|
||||
$strHash =~ s/^\s+|\s+$//g;
|
||||
my $strHash = trim(execute($strCommand));
|
||||
|
||||
return($strHash);
|
||||
}
|
||||
|
||||
################################################################################
|
||||
####################################################################################################################################
|
||||
# START MAIN
|
||||
################################################################################
|
||||
####################################################################################################################################
|
||||
# Get the command
|
||||
my $strCommand = $ARGV[0];
|
||||
|
||||
################################################################################
|
||||
####################################################################################################################################
|
||||
# ARCHIVE-LOCAL COMMAND
|
||||
################################################################################
|
||||
####################################################################################################################################
|
||||
if ($strCommand eq "archive-local")
|
||||
{
|
||||
# Get the source dir/file
|
||||
my $strSource = $ARGV[1];
|
||||
# archive-local command must have three arguments
|
||||
if (@ARGV != 3)
|
||||
{
|
||||
die "not enough arguments - show usage";
|
||||
}
|
||||
|
||||
# !!! Make sure this is defined
|
||||
# !!! Make sure that the file exists
|
||||
# Get the source dir/file
|
||||
my $strSourceFile = $ARGV[1];
|
||||
|
||||
unless (-e $strSourceFile)
|
||||
{
|
||||
die "source file does not exist - show usage";
|
||||
}
|
||||
|
||||
# Get the destination dir/file
|
||||
my $strDestination = $ARGV[2];
|
||||
my $strDestinationFile = $ARGV[2];
|
||||
|
||||
# !!! Make sure this is defined
|
||||
# !!! Make sure that the destination dir exists
|
||||
# Make sure the destination directory exists
|
||||
unless (-e dirname($strDestinationFile))
|
||||
{
|
||||
die "destination dir does not exist - show usage";
|
||||
}
|
||||
|
||||
# Calculate sha1 hash for the file
|
||||
my $strHash = file_hash_get($strSource);
|
||||
# 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";
|
||||
}
|
||||
|
||||
# !!! Make sure the file does not already exist!
|
||||
# !!! check only the prefix - not with the checksum in case it changed "file-*.gz"
|
||||
# Calculate sha1 hash for the file (unless disabled)
|
||||
if (!$bNoChecksum)
|
||||
{
|
||||
$strDestinationFile .= "-" . file_hash_get($strSourceFile);
|
||||
}
|
||||
|
||||
# Setup the compression string
|
||||
my $strCommand = $strCommandCompress;
|
||||
$strCommand =~ s/\%file\%/$strSource/g;
|
||||
$strCommand .= " > $strDestination-$strHash.gz";
|
||||
# Setup the copy string
|
||||
my $strCommand = "";
|
||||
|
||||
if ($bNoCompression)
|
||||
{
|
||||
$strCommand = $strCommandCopy;
|
||||
$strCommand =~ s/\%source\%/$strSourceFile/g;
|
||||
$strCommand =~ s/\%destination\%/$strDestinationFile/g;
|
||||
}
|
||||
else
|
||||
{
|
||||
$strCommand = $strCommandCompress;
|
||||
$strCommand =~ s/\%file\%/$strSourceFile/g;
|
||||
$strCommand .= " > $strDestinationFile.gz";
|
||||
}
|
||||
|
||||
# Execute the compression
|
||||
print("$strCommand\n");
|
||||
print("copy: $strCommand\n");
|
||||
execute($strCommand);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user