From fa668d50072542890f15d9ebda77bb2ed552e3e9 Mon Sep 17 00:00:00 2001 From: David Steele Date: Mon, 3 Feb 2014 14:50:23 -0500 Subject: [PATCH] Move file init code to file module --- pg_backrest.conf | 1 - pg_backrest.pl | 75 +++++++++++---------------------------------- pg_backrest_file.pm | 65 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 62 deletions(-) diff --git a/pg_backrest.conf b/pg_backrest.conf index fc129f178..544e79ea2 100644 --- a/pg_backrest.conf +++ b/pg_backrest.conf @@ -4,7 +4,6 @@ compress=/usr/bin/gzip --stdout %file% decompress=/usr/bin/gzip -dc %file% #checksum=sha1sum %file% | awk '{print \$1}' # Ubuntu Linux checksum=/usr/bin/shasum %file% | awk '{print $1}' -copy=/bin/cp %source% %destination% manifest=/opt/local/bin/gfind %path% -printf '%P\t%y\t%u\t%g\t%m\t%T@\t%i\t%s\t%l\n' psql=/Library/PostgreSQL/9.3/bin/psql --port=6001 diff --git a/pg_backrest.pl b/pg_backrest.pl index 06ea951b7..7d80002ba 100755 --- a/pg_backrest.pl +++ b/pg_backrest.pl @@ -648,26 +648,7 @@ if (!defined($strConfigFile)) } my %oConfig; -tie %oConfig, 'Config::IniFiles', (-file => $strConfigFile) or die "Unable to find config file"; - -# Load commands required for archive-push -$pg_backrest_file::strCommandChecksum = config_load(\%oConfig, "command", "checksum", !$pg_backrest_file::bNoChecksum); -$pg_backrest_file::strCommandCompress = config_load(\%oConfig, "command", "compress", !$pg_backrest_file::bNoCompression); -$pg_backrest_file::strCommandDecompress = config_load(\%oConfig, "command", "decompress", !$pg_backrest_file::bNoCompression); -$pg_backrest_file::strCommandCopy = config_load(\%oConfig, "command", "copy", $pg_backrest_file::bNoCompression); - -# Load and check the base backup path -$pg_backrest_file::strBackupPath = $oConfig{backup}{path}; - -if (!defined($pg_backrest_file::strBackupPath)) -{ - die &log(ERROR, "common:backup_path undefined"); -} - -unless (-e $pg_backrest_file::strBackupPath) -{ - die &log(ERROR, "base path ${pg_backrest_file::strBackupPath} does not exist"); -} +tie %oConfig, 'Config::IniFiles', (-file => $strConfigFile) or confess &log(ERROR, "unable to find config file ${strConfigFile}"); # Load and check the cluster if (!defined($strCluster)) @@ -675,34 +656,22 @@ if (!defined($strCluster)) $strCluster = "db"; #!!! Modify to load cluster from conf if there is only one, else error } -$pg_backrest_file::strBackupClusterPath = "${pg_backrest_file::strBackupPath}/${strCluster}"; - -unless (-e $pg_backrest_file::strBackupClusterPath) -{ - &log(INFO, "creating cluster path ${pg_backrest_file::strBackupClusterPath}"); - mkdir $pg_backrest_file::strBackupClusterPath or die &log(ERROR, "cluster backup path '${pg_backrest_file::strBackupClusterPath}' create failed"); -} - -# Load the backup host (if it exists) -$pg_backrest_file::strBackupHost = $oConfig{backup}{host}; - -if (defined($pg_backrest_file::strBackupHost)) -{ - &log(INFO, "connecting to backup ssh host ${pg_backrest_file::strBackupHost}"); - - # !!! This could be improved by redirecting stderr to a file to get a better error message - $pg_backrest_file::oBackupSSH = Net::OpenSSH->new($pg_backrest_file::strBackupHost, master_stderr_discard => true); - $pg_backrest_file::oBackupSSH->error and confess &log(ERROR, "unable to connect to ${pg_backrest_file::strBackupHost}: " . $pg_backrest_file::oBackupSSH->error); -} +# Run file_init_archive - this is the minimal config needed to run archiving +file_init_archive +( + config_load(\%oConfig, "command", "checksum", !$bNoChecksum), + config_load(\%oConfig, "command", "compress", !$pg_backrest_file::bNoCompression), + config_load(\%oConfig, "command", "decompress", !$pg_backrest_file::bNoCompression), + $oConfig{backup}{host}, + $oConfig{backup}{path}, + $strCluster, +); #################################################################################################################################### # ARCHIVE-PUSH Command #################################################################################################################################### if ($strOperation eq "archive-push") { - # Make sure that the db is local - - # archive-push command must have two arguments if (@ARGV != 2) { @@ -747,21 +716,13 @@ if ($strType ne "full" && $strType ne "differential" && $strType ne "incremental die &log(ERROR, "backup type must be full, differential (diff), incremental (incr)"); } -# Load commands required for backup -$pg_backrest_file::strCommandManifest = config_load(\%oConfig, "command", "manifest"); -$pg_backrest_file::strCommandPsql = config_load(\%oConfig, "command", "psql"); - -# Load the database host (if it exists) -$pg_backrest_file::strDbHost = $oConfig{"cluster:$strCluster"}{host}; - -if (defined($pg_backrest_file::strDbHost)) -{ - &log(INFO, "connecting to database ssh host ${pg_backrest_file::strDbHost}"); - - # !!! This could be improved by redirecting stderr to a file to get a better error message - $pg_backrest_file::oDbSSH = Net::OpenSSH->new($pg_backrest_file::strDbHost, master_stderr_discard => true); - $pg_backrest_file::oDbSSH->error and confess &log(ERROR, "unable to connect to ${pg_backrest_file::strDbHost}: " . $pg_backrest_file::oDbSSH->error); -} +# Run file_init_archive - this is the minimal config needed to run archiving +file_init_backup +( + config_load(\%oConfig, "command", "manifest"), + $pg_backrest_file::strCommandPsql = config_load(\%oConfig, "command", "psql"), + $oConfig{"cluster:$strCluster"}{host} +); #################################################################################################################################### # BACKUP diff --git a/pg_backrest_file.pm b/pg_backrest_file.pm index 1f250e586..ae6e6d1df 100644 --- a/pg_backrest_file.pm +++ b/pg_backrest_file.pm @@ -16,7 +16,8 @@ use pg_backrest_utility; use Exporter qw(import); -our @EXPORT = qw(path_get path_type_get link_create path_create file_copy file_list_get manifest_get file_hash_get +our @EXPORT = qw(file_init_archive file_init_backup + path_get path_type_get link_create path_create file_copy file_list_get manifest_get file_hash_get PATH_DB PATH_DB_ABSOLUTE PATH_BACKUP PATH_BACKUP_CLUSTER PATH_BACKUP_TMP PATH_BACKUP_ARCHIVE); # Extension and permissions @@ -27,7 +28,6 @@ our $strDefaultPathPermission = "0750"; our $strCommandChecksum; our $strCommandCompress; our $strCommandDecompress; -our $strCommandCopy; our $strCommandCat = "cat %file%"; our $strCommandManifest; our $strCommandPsql; @@ -46,11 +46,68 @@ our $strBackupClusterPath; # Backup cluster path our $bNoCompression; #################################################################################################################################### -# INIT +# FILE_INIT_ARCHIVE #################################################################################################################################### -sub init +sub file_init_archive { + my $strCommandChecksumParam = shift; + my $strCommandCompressParam = shift; + my $strCommandDecompressParam = shift; + my $strBackupHostParam = shift; + my $strBackupPathParam = shift; + my $strCluster = shift; + # Assign parameters to module variables + $strCommandChecksum = $strCommandChecksumParam; + $strCommandCompress = $strCommandCompressParam; + $strCommandDecompress = $strCommandDecompressParam; + $strBackupPath = $strBackupPathParam; + #$strCluster = $strClusterParam; + $strBackupHost = $strBackupHostParam; + + # Make sure the backup path is defined + if (!defined($pg_backrest_file::strBackupPath)) + { + die &log(ERROR, "common:backup_path undefined"); + } + + # Create the backup cluster path + $strBackupClusterPath = "${strBackupPath}/${strCluster}"; + +# unless (-e $pg_backrest_file::strBackupClusterPath) +# { +# &log(INFO, "creating cluster path ${pg_backrest_file::strBackupClusterPath}"); +# mkdir $pg_backrest_file::strBackupClusterPath or die &log(ERROR, "cluster backup path '${pg_backrest_file::strBackupClusterPath}' create failed"); +# } + + if (defined($pg_backrest_file::strBackupHost)) + { + &log(INFO, "connecting to backup ssh host ${pg_backrest_file::strBackupHost}"); + + # !!! This could be improved by redirecting stderr to a file to get a better error message + $pg_backrest_file::oBackupSSH = Net::OpenSSH->new($pg_backrest_file::strBackupHost, master_stderr_discard => true); + $pg_backrest_file::oBackupSSH->error and confess &log(ERROR, "unable to connect to ${pg_backrest_file::strBackupHost}: " . $pg_backrest_file::oBackupSSH->error); + } +} + +sub file_init_backup +{ + my $strCommandManifestParam = shift; + my $strCommandPsqlParam = shift; + my $strDbHostParam = shift; + + $strCommandManifest = $strCommandManifestParam; + $strCommandPsql = $strCommandPsqlParam; + $strDbHost = $strDbHostParam; + + if (defined($strDbHost)) + { + &log(INFO, "connecting to database ssh host ${strDbHost}"); + + # !!! This could be improved by redirecting stderr to a file to get a better error message + $pg_backrest_file::oDbSSH = Net::OpenSSH->new($pg_backrest_file::strDbHost, master_stderr_discard => true); + $pg_backrest_file::oDbSSH->error and confess &log(ERROR, "unable to connect to ${pg_backrest_file::strDbHost}: " . $pg_backrest_file::oDbSSH->error); + } } ####################################################################################################################################