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

More work on file_copy

This commit is contained in:
David Steele
2014-02-01 12:57:27 -05:00
parent 52ba8801ef
commit de972ef5d3
2 changed files with 50 additions and 23 deletions

View File

@ -9,6 +9,7 @@ manifest=/opt/local/bin/gfind %path% -printf '%P\t%y\t%u\t%g\t%m\t%T@\t%i\t%s\t%
psql=/Library/PostgreSQL/9.3/bin/psql --port=6001 psql=/Library/PostgreSQL/9.3/bin/psql --port=6001
[backup] [backup]
#host=localhost
path=/Users/dsteele/test/backup path=/Users/dsteele/test/backup
[retention] [retention]
@ -18,5 +19,4 @@ wal_retention_type=full,diff,incr
wal_retention=2 # 0 means since the last type wal_retention=2 # 0 means since the last type
[cluster:db] [cluster:db]
host=localhost
path=/Users/dsteele/test/db/common path=/Users/dsteele/test/db/common

View File

@ -12,6 +12,7 @@ use File::Copy;
use File::Remove; use File::Remove;
use Carp; use Carp;
use Net::OpenSSH; use Net::OpenSSH;
use IPC::Open3;
# Process flags # Process flags
my $bNoCompression; my $bNoCompression;
@ -391,6 +392,7 @@ sub file_copy
my $strDestinationFile = shift; my $strDestinationFile = shift;
my $bNoCompressionOverride = shift; my $bNoCompressionOverride = shift;
# Generate source, destination and tmp filenames
my $strSource = path_get($strSourcePathType, $strSourceFile); my $strSource = path_get($strSourcePathType, $strSourceFile);
my $strDestination = path_get($strDestinationPathType, $strDestinationFile); my $strDestination = path_get($strDestinationPathType, $strDestinationFile);
my $strDestinationTmp = path_get($strDestinationPathType, $strDestinationFile, true); my $strDestinationTmp = path_get($strDestinationPathType, $strDestinationFile, true);
@ -408,6 +410,9 @@ sub file_copy
(defined($bNoCompressionOverride) && $bNoCompressionOverride) || (defined($bNoCompressionOverride) && $bNoCompressionOverride) ||
(!defined($bNoCompressionOverride) && $bNoCompression)); (!defined($bNoCompressionOverride) && $bNoCompression));
# If the destination file is to be compressed add the extenstion
$strDestination .= $bCompress ? ".gz" : "";
# If the destination path is backup and does not exist, create it # If the destination path is backup and does not exist, create it
unless ($strDestinationPathType =~ /^backup\:.*/ and -e dirname($strDestination)) unless ($strDestinationPathType =~ /^backup\:.*/ and -e dirname($strDestination))
{ {
@ -424,13 +429,14 @@ sub file_copy
# If this is a remote command # If this is a remote command
if (remote_get()) if (remote_get())
{ {
my $hFile; # File handle for source or destination # Generate the command string depending on compression/copy
my $strCommand; # Command string my $strCommand = $bCompress ? $strCommandCompress : $strCommandCat;
$strCommand =~ s/\%file\%/${strSourceFile}/g;
# If the source and destination are remote # If the source and destination are remote
if (remote_get($strSourcePathType) && remote_get($strDestinationPathType)) if (remote_get($strSourcePathType) && remote_get($strDestinationPathType))
{ {
&log(ASSERT, "remote source and destination not supported"); confess &log(ASSERT, "remote source and destination not supported");
} }
# Else if the source is remote # Else if the source is remote
elsif (remote_get($strSourcePathType)) elsif (remote_get($strSourcePathType))
@ -438,28 +444,36 @@ sub file_copy
&log(DEBUG, " file_copy: remote ${strSource} to local ${strDestination}"); &log(DEBUG, " file_copy: remote ${strSource} to local ${strDestination}");
# Open the destination file for writing (will be streamed from the ssh session) # Open the destination file for writing (will be streamed from the ssh session)
my $hFile;
open($hFile, ">", $strDestinationTmp) or confess &log(ERROR, "cannot open ${strDestination}"); open($hFile, ">", $strDestinationTmp) or confess &log(ERROR, "cannot open ${strDestination}");
# Generate the command string depending on compression/copy # Execute the command through ssh
$strCommand = $bCompress ? $strCommandCompress : $strCommandCat;
$strCommand =~ s/\%file\%/${strSourceFile}/g;
# If the destination file is to be compressed add the extenstion
$strDestination .= $bCompress ? ".gz" : "";
# Execute the command
$oSSH->system({stdout_fh => $hFile}, $strCommand) or confess &log(ERROR, "unable to execute ssh '$strCommand'"); $oSSH->system({stdout_fh => $hFile}, $strCommand) or confess &log(ERROR, "unable to execute ssh '$strCommand'");
# Close the destination file handle
close($hFile) or confess &log(ERROR, "cannot close file");
} }
# Else if the destination is remote # Else if the destination is remote
elsif (remote_get($strDestinationPathType)) elsif (remote_get($strDestinationPathType))
{ {
&log(DEBUG, " file_copy: local ${strSource} to remote ${strDestination}"); &log(DEBUG, " file_copy: local ${strSource} ($strCommand) to remote ${strDestination}");
open($hFile, "<", $strSource) or confess &log(ERROR, "cannot open ${strSource}");
&log(DEBUG, " HERE");
}
# Close the source or destination file that was being streamed # Open the input command as a stream
close($hFile) or confess &log(ERROR, "cannot close file"); my $hOut;
my $pId = open3(undef, $hOut, undef, $strCommand) or confess(ERROR, "unable to execute '${strCommand}'");
# Execute the command though ssh
$oSSH->system({stdin_fh => $hOut}, "cat > ${strDestinationTmp}") or confess &log(ERROR, "unable to execute ssh 'cat'");
# Wait for the stream process to finish
waitpid($pId, 0);
my $iExitStatus = ${^CHILD_ERROR_NATIVE} >> 8;
if ($iExitStatus != 0)
{
confess &log(ERROR, "command '${strCommand}' returned", $iExitStatus);
}
}
} }
# Else this is a local command # Else this is a local command
else else
@ -472,7 +486,6 @@ sub file_copy
$strCommand = $strCommandCompress; $strCommand = $strCommandCompress;
$strCommand =~ s/\%file\%/${strSourceFile}/g; $strCommand =~ s/\%file\%/${strSourceFile}/g;
$strCommand .= " > ${strDestinationTmp}"; $strCommand .= " > ${strDestinationTmp}";
$strDestination .= ".${strCompressExtension}";
} }
else else
{ {
@ -1232,6 +1245,16 @@ unless (-e $strBackupClusterPath)
mkdir $strBackupClusterPath or die &log(ERROR, "cluster backup path '${strBackupClusterPath}' create failed"); mkdir $strBackupClusterPath or die &log(ERROR, "cluster backup path '${strBackupClusterPath}' create failed");
} }
# Load the backup host (if it exists)
$strBackupHost = $oConfig{backup}{host};
if (defined($strBackupHost))
{
&log(INFO, "connecting to database ssh host ${strBackupHost}");
$oSSH = Net::OpenSSH->new($strBackupHost);
# !!! ERROR HANDLING HERE
}
#################################################################################################################################### ####################################################################################################################################
# ARCHIVE-PUSH Command # ARCHIVE-PUSH Command
#################################################################################################################################### ####################################################################################################################################
@ -1296,10 +1319,14 @@ if (defined($strDbHost))
$oSSH = Net::OpenSSH->new($strDbHost); $oSSH = Net::OpenSSH->new($strDbHost);
} }
unlink("/Users/dsteele/test/backup/postgresql.conf"); #unlink("/Users/dsteele/test/db/postgresql.conf");
unlink("/Users/dsteele/test/backup/postgresql.conf.gz"); #unlink("/Users/dsteele/test/db/postgresql.conf.gz");
file_copy(PATH_DB_ABSOLUTE, "/Users/dsteele/test/db/common/postgresql.conf", PATH_BACKUP_TMP, "postgresql.conf"); #file_copy(PATH_BACKUP_TMP, "postgresql.conf", PATH_DB_ABSOLUTE, "/Users/dsteele/test/db/postgresql.conf");
exit 0;
#unlink("/Users/dsteele/test/backup/postgresql.conf");
#unlink("/Users/dsteele/test/backup/postgresql.conf.gz");
#file_copy(PATH_DB_ABSOLUTE, "/Users/dsteele/test/db/common/postgresql.conf", PATH_BACKUP_TMP, "postgresql.conf");
#exit 0;
#################################################################################################################################### ####################################################################################################################################
# BACKUP # BACKUP