1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2026-06-20 01:17:49 +02:00

Converted File::compress

This commit is contained in:
David Steele
2014-06-02 17:48:07 -04:00
parent 0249d3516b
commit 816c67edfd
4 changed files with 130 additions and 28 deletions
+1
View File
@@ -35,6 +35,7 @@ Net::OpenSSH
JSON
IPC::Open3
Digest::SHA
IO::Compress::Gzip
## release notes
+21 -10
View File
@@ -27,7 +27,8 @@ use constant
OP_EXISTS => "exists",
OP_HASH => "hash",
OP_REMOVE => "remove",
OP_MANIFEST => "manifest"
OP_MANIFEST => "manifest",
OP_COMPRESS => "compress"
};
####################################################################################################################################
@@ -57,15 +58,6 @@ if (!defined($strOperation))
confess &log(ERROR, "operation is not defined");
}
if ($strOperation ne OP_LIST &&
$strOperation ne OP_EXISTS &&
$strOperation ne OP_HASH &&
$strOperation ne OP_REMOVE &&
$strOperation ne OP_MANIFEST)
{
confess &log(ERROR, "invalid operation ${strOperation}");
}
# Create the file object
my $oFile = pg_backrest_file->new();
@@ -178,3 +170,22 @@ if ($strOperation eq OP_MANIFEST)
exit 0;
}
####################################################################################################################################
# COMPRESS Command
####################################################################################################################################
if ($strOperation eq OP_COMPRESS)
{
my $strFile = $ARGV[1];
if (!defined($strFile))
{
confess "file must be specified for compress operation";
}
$oFile->compress(PATH_ABSOLUTE, $strFile);
exit 0;
}
confess &log(ERROR, "invalid operation ${strOperation}");
+44 -16
View File
@@ -16,6 +16,7 @@ use IPC::System::Simple qw(capture);
use Digest::SHA;
use File::stat;
use Fcntl ':mode';
use IO::Compress::Gzip qw(gzip $GzipError);
use lib dirname($0);
use pg_backrest_utility;
@@ -847,33 +848,60 @@ sub hash
}
####################################################################################################################################
# FILE_COMPRESS
# COMPRESS
####################################################################################################################################
sub file_compress
sub compress
{
my $self = shift;
my $strPathType = shift;
my $strFile = shift;
# For now this operation is not supported remotely. Not currently needed.
# Get the root path for the file list
my $strErrorPrefix = "File->compress";
my $bRemote = $self->is_remote($strPathType);
my $strPathOp = $self->path_get($strPathType, $strFile);
&log(TRACE, "${strErrorPrefix}: " . ($bRemote ? "remote" : "local") . " ${strPathType}:${strPathOp}");
# Run remotely
if ($self->is_remote($strPathType))
{
confess &log(ASSERT, "remote operation not supported");
}
my $strCommand = $self->{strCommand} .
" compress ${strPathOp}";
if (!defined($self->{strCommandCompress}))
# Run via SSH
my $oSSH = $self->remote_get($strPathType);
my $strOutput = $oSSH->capture($strCommand);
# Handle any errors
if ($oSSH->error)
{
confess &log(ERROR, "${strErrorPrefix} remote (${strCommand}): " . (defined($strOutput) ? $strOutput : $oSSH->error));
}
}
# Run locally
else
{
confess &log(ASSERT, "\$strCommandCompress not defined");
if (!gzip($strPathOp => "${strPathOp}.gz"))
{
my $strError = "${strPathOp} could not be compressed:" . $!;
my $iErrorCode = 2;
unless (-e $strPathOp)
{
$strError = "${strPathOp} does not exist";
$iErrorCode = 1;
}
if ($strPathType eq PATH_ABSOLUTE)
{
print $strError;
exit ($iErrorCode);
}
confess &log(ERROR, "${strErrorPrefix}: " . $strError);
}
}
my $strPath = $self->path_get($strPathType, $strFile);
# Build the command
my $strCommand = $self->{strCommandCompress};
$strCommand =~ s/\%file\%/${strPath}/g;
$strCommand =~ s/\ \-\-stdout//g;
system($strCommand) == 0 or confess &log(ERROR, "unable to compress ${strPath}: ${strCommand}");
}
####################################################################################################################################
+64 -2
View File
@@ -39,7 +39,61 @@ sub BackRestTestFile
# log_level_set(TRACE, TRACE);
#-------------------------------------------------------------------------------------------------------------------------------
# Test hash()
#-------------------------------------------------------------------------------------------------------------------------------
$iRun = 0;
print "\ntest File->compress()\n";
for (my $bRemote = 0; $bRemote <= 1; $bRemote++)
{
my $oFile = pg_backrest_file->new
(
strStanza => $strStanza,
bNoCompression => true,
strCommand => $strCommand,
strBackupClusterPath => ${strTestPath},
strBackupPath => ${strTestPath},
strBackupHost => $bRemote ? $strHost : undef,
strBackupUser => $bRemote ? $strUser : undef
);
# Loop through exists
for (my $bExists = 0; $bExists <= 1; $bExists++)
{
$iRun++;
print "run ${iRun} - " .
"remote $bRemote, exists $bExists\n";
# Drop the old test directory and create a new one
system("rm -rf test");
system("mkdir test") == 0 or confess "Unable to create test directory";
my $strFile = "${strTestPath}/test.txt";
if ($bExists)
{
system("echo 'TESTDATA' > ${strFile}");
}
# Execute in eval in case of error
eval
{
$oFile->compress(PATH_BACKUP_ABSOLUTE, $strFile);
};
if ($@ && $bExists)
{
confess "error raised: " . $@ . "\n";
}
}
}
#-------------------------------------------------------------------------------------------------------------------------------
# Test manifest()
#-------------------------------------------------------------------------------------------------------------------------------
$iRun = 0;
print "\ntest File->manifest()\n";
@@ -166,9 +220,9 @@ sub BackRestTestFile
}
}
return;
#-------------------------------------------------------------------------------------------------------------------------------
# Test list()
#-------------------------------------------------------------------------------------------------------------------------------
$iRun = 0;
print "\ntest File->list()\n";
@@ -265,7 +319,9 @@ sub BackRestTestFile
}
}
#-------------------------------------------------------------------------------------------------------------------------------
# Test remove()
#-------------------------------------------------------------------------------------------------------------------------------
$iRun = 0;
print "test File->remove()\n";
@@ -333,7 +389,9 @@ sub BackRestTestFile
}
}
#-------------------------------------------------------------------------------------------------------------------------------
# Test hash()
#-------------------------------------------------------------------------------------------------------------------------------
$iRun = 0;
print "\ntest File->hash()\n";
@@ -389,7 +447,9 @@ sub BackRestTestFile
}
}
#-------------------------------------------------------------------------------------------------------------------------------
# Test exists()
#-------------------------------------------------------------------------------------------------------------------------------
$iRun = 0;
print "\ntest File->exists()\n";
@@ -444,7 +504,9 @@ sub BackRestTestFile
return;
#-------------------------------------------------------------------------------------------------------------------------------
# Test copy()
#-------------------------------------------------------------------------------------------------------------------------------
$iRun = 0;
system("rm -rf lock");