You've already forked pgbackrest
							
							
				mirror of
				https://github.com/pgbackrest/pgbackrest.git
				synced 2025-10-30 23:37:45 +02:00 
			
		
		
		
	Each option is assigned a source to designate where it came from (param, config, default).
operationWrite() created to easily pass parameters on to a new process.
This commit is contained in:
		| @@ -19,7 +19,8 @@ use BackRest::Utility; | |||||||
| # Export functions | # Export functions | ||||||
| #################################################################################################################################### | #################################################################################################################################### | ||||||
| our @EXPORT = qw(configLoad optionGet optionTest optionRuleGet optionRequired optionDefault operationGet operationTest | our @EXPORT = qw(configLoad optionGet optionTest optionRuleGet optionRequired optionDefault operationGet operationTest | ||||||
|                  operationSet optionRemoteType optionRemoteTypeTest optionRemote optionRemoteTest remoteDestroy); |                  operationSet operationWrite optionRemoteType optionRemoteTypeTest optionRemote optionRemoteTest | ||||||
|  |                  remoteDestroy); | ||||||
|  |  | ||||||
| #################################################################################################################################### | #################################################################################################################################### | ||||||
| # DB/BACKUP Constants | # DB/BACKUP Constants | ||||||
| @@ -59,6 +60,17 @@ use constant | |||||||
|  |  | ||||||
| push @EXPORT, qw(BACKUP_TYPE_FULL BACKUP_TYPE_DIFF BACKUP_TYPE_INCR); | push @EXPORT, qw(BACKUP_TYPE_FULL BACKUP_TYPE_DIFF BACKUP_TYPE_INCR); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #################################################################################################################################### | ||||||
|  | # SOURCE Constants | ||||||
|  | #################################################################################################################################### | ||||||
|  | use constant | ||||||
|  | { | ||||||
|  |     SOURCE_CONFIG       => 'config', | ||||||
|  |     SOURCE_PARAM        => 'param', | ||||||
|  |     SOURCE_DEFAULT      => 'default' | ||||||
|  | }; | ||||||
|  |  | ||||||
| #################################################################################################################################### | #################################################################################################################################### | ||||||
| # RECOVERY Type Constants | # RECOVERY Type Constants | ||||||
| #################################################################################################################################### | #################################################################################################################################### | ||||||
| @@ -360,7 +372,7 @@ my %oOptionRule = | |||||||
|         { |         { | ||||||
|             &OP_RESTORE => |             &OP_RESTORE => | ||||||
|             { |             { | ||||||
|                 &OPTION_RULE_DEFAULT => OPTION_DEFAULT_RESTORE_TYPE, |                 &OPTION_RULE_DEFAULT => OPTION_DEFAULT_RESTORE_SET, | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     }, |     }, | ||||||
| @@ -1035,13 +1047,13 @@ sub configLoad | |||||||
|     # Replace command psql options if set |     # Replace command psql options if set | ||||||
|     if (optionTest(OPTION_COMMAND_PSQL) && optionTest(OPTION_COMMAND_PSQL_OPTION)) |     if (optionTest(OPTION_COMMAND_PSQL) && optionTest(OPTION_COMMAND_PSQL_OPTION)) | ||||||
|     { |     { | ||||||
|         $oOption{&OPTION_COMMAND_PSQL} =~ s/\%option\%/$oOption{&OPTION_COMMAND_PSQL_OPTION}/g; |         $oOption{&OPTION_COMMAND_PSQL}{value} =~ s/\%option\%/$oOption{&OPTION_COMMAND_PSQL_OPTION}{value}/g; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     # Set repo-remote-path to repo-path if it is not set |     # Set repo-remote-path to repo-path if it is not set | ||||||
|     if (optionTest(OPTION_REPO_PATH) && !optionTest(OPTION_REPO_REMOTE_PATH)) |     if (optionTest(OPTION_REPO_PATH) && !optionTest(OPTION_REPO_REMOTE_PATH)) | ||||||
|     { |     { | ||||||
|         $oOption{&OPTION_REPO_REMOTE_PATH} = optionGet(OPTION_REPO_PATH); |         $oOption{&OPTION_REPO_REMOTE_PATH}{value} = optionGet(OPTION_REPO_PATH); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     # Check if the backup host is remote |     # Check if the backup host is remote | ||||||
| @@ -1163,7 +1175,7 @@ sub optionValid | |||||||
|             { |             { | ||||||
|                 # Check if the depend option has a value |                 # Check if the depend option has a value | ||||||
|                 $strDependOption = $$oDepend{&OPTION_RULE_DEPEND_OPTION}; |                 $strDependOption = $$oDepend{&OPTION_RULE_DEPEND_OPTION}; | ||||||
|                 $strDependValue = $oOption{$strDependOption}; |                 $strDependValue = $oOption{$strDependOption}{value}; | ||||||
|  |  | ||||||
|                 # Make sure the depend option has been resolved, otherwise skip this option for now |                 # Make sure the depend option has been resolved, otherwise skip this option for now | ||||||
|                 if (!defined($oOptionResolved{$strDependOption})) |                 if (!defined($oOptionResolved{$strDependOption})) | ||||||
| @@ -1207,12 +1219,12 @@ sub optionValid | |||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 # If the config option is defined try to get the option from the config file |                 # If the config option is defined try to get the option from the config file | ||||||
|                 if ($bConfigExists && defined($oOption{&OPTION_CONFIG})) |                 if ($bConfigExists && defined($oOption{&OPTION_CONFIG}{value})) | ||||||
|                 { |                 { | ||||||
|                     # Attempt to load the config file if it has not been loaded |                     # Attempt to load the config file if it has not been loaded | ||||||
|                     if (!defined($oConfig)) |                     if (!defined($oConfig)) | ||||||
|                     { |                     { | ||||||
|                         my $strConfigFile = $oOption{&OPTION_CONFIG}; |                         my $strConfigFile = $oOption{&OPTION_CONFIG}{value}; | ||||||
|                         $bConfigExists = -e $strConfigFile; |                         $bConfigExists = -e $strConfigFile; | ||||||
|  |  | ||||||
|                         if ($bConfigExists) |                         if ($bConfigExists) | ||||||
| @@ -1295,6 +1307,8 @@ sub optionValid | |||||||
|                                              ERROR_OPTION_INVALID_VALUE); |                                              ERROR_OPTION_INVALID_VALUE); | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
|  |                         $oOption{$strOption}{source} = SOURCE_CONFIG; | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| @@ -1404,18 +1418,24 @@ sub optionValid | |||||||
|                         # Check that the key has not already been set |                         # Check that the key has not already been set | ||||||
|                         my $strKey = substr($strItem, 0, $iEqualPos); |                         my $strKey = substr($strItem, 0, $iEqualPos); | ||||||
|  |  | ||||||
|                         if (defined($oOption{$strOption}{$strKey})) |                         if (defined($oOption{$strOption}{$strKey}{value})) | ||||||
|                         { |                         { | ||||||
|                             confess &log(ERROR, "'${$strItem}' already defined for '${strOption}' option", |                             confess &log(ERROR, "'${$strItem}' already defined for '${strOption}' option", | ||||||
|                                                 ERROR_OPTION_DUPLICATE_KEY); |                                                 ERROR_OPTION_DUPLICATE_KEY); | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
|                         $oOption{$strOption}{$strKey} = substr($strItem, $iEqualPos + 1); |                         $oOption{$strOption}{value}{$strKey} = substr($strItem, $iEqualPos + 1); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|                 else |                 else | ||||||
|                 { |                 { | ||||||
|                     $oOption{$strOption} = $strValue; |                     $oOption{$strOption}{value} = $strValue; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 # If not config sourced then it must be a param | ||||||
|  |                 if (!defined($oOption{$strOption}{source})) | ||||||
|  |                 { | ||||||
|  |                     $oOption{$strOption}{source} = SOURCE_PARAM; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             # Else try to set a default |             # Else try to set a default | ||||||
| @@ -1423,6 +1443,9 @@ sub optionValid | |||||||
|                    (!defined($oOptionRule{$strOption}{&OPTION_RULE_OPERATION}) || |                    (!defined($oOptionRule{$strOption}{&OPTION_RULE_OPERATION}) || | ||||||
|                     defined($oOptionRule{$strOption}{&OPTION_RULE_OPERATION}{$strOperation}))) |                     defined($oOptionRule{$strOption}{&OPTION_RULE_OPERATION}{$strOperation}))) | ||||||
|             { |             { | ||||||
|  |                 # Source is default for this option | ||||||
|  |                 $oOption{$strOption}{source} = SOURCE_DEFAULT; | ||||||
|  |  | ||||||
|                 # Check for default in operation then option |                 # Check for default in operation then option | ||||||
|                 my $strDefault = optionDefault($strOption, $strOperation); |                 my $strDefault = optionDefault($strOption, $strOperation); | ||||||
|  |  | ||||||
| @@ -1430,7 +1453,7 @@ sub optionValid | |||||||
|                 if (defined($strDefault)) |                 if (defined($strDefault)) | ||||||
|                 { |                 { | ||||||
|                     # Only set default if dependency is resolved |                     # Only set default if dependency is resolved | ||||||
|                     $oOption{$strOption} = $strDefault if !$bNegate; |                     $oOption{$strOption}{value} = $strDefault if !$bNegate; | ||||||
|                 } |                 } | ||||||
|                 # Else check required |                 # Else check required | ||||||
|                 elsif (optionRequired($strOption, $strOperation)) |                 elsif (optionRequired($strOption, $strOperation)) | ||||||
| @@ -1551,16 +1574,50 @@ sub optionGet | |||||||
|     my $strOption = shift; |     my $strOption = shift; | ||||||
|     my $bRequired = shift; |     my $bRequired = shift; | ||||||
|  |  | ||||||
|     if (!defined($oOption{$strOption}) && (!defined($bRequired) || $bRequired)) |     if (!defined($oOption{$strOption}{value}) && (!defined($bRequired) || $bRequired)) | ||||||
|     { |     { | ||||||
|         confess &log(ASSERT, "option ${strOption} is required"); |         confess &log(ASSERT, "option ${strOption} is required"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return $oOption{$strOption}; |     return $oOption{$strOption}{value}; | ||||||
| } | } | ||||||
|  |  | ||||||
| #################################################################################################################################### | #################################################################################################################################### | ||||||
| # optionTest | # operationWrite | ||||||
|  | # | ||||||
|  | # Using the options that were passed to the current operations, write the command string for another operation.  For example, this | ||||||
|  | # can be used to write the archive-get command for recovery.conf during a restore. | ||||||
|  | #################################################################################################################################### | ||||||
|  | sub operationWrite | ||||||
|  | { | ||||||
|  |     my $strNewOperation = shift; | ||||||
|  |  | ||||||
|  |     my $strCommand = "$0"; | ||||||
|  |  | ||||||
|  |     foreach my $strOption (sort(keys(%oOption))) | ||||||
|  |     { | ||||||
|  |         if ((!defined($oOptionRule{$strOption}{&OPTION_RULE_OPERATION}) || | ||||||
|  |              defined($oOptionRule{$strOption}{&OPTION_RULE_OPERATION}{$strNewOperation})) && | ||||||
|  |             $oOption{$strOption}{source} eq SOURCE_PARAM) | ||||||
|  |         { | ||||||
|  |             my $strParam = "--${strOption}=$oOption{$strOption}{value}"; | ||||||
|  |  | ||||||
|  |             if (index($oOption{$strOption}{value}, " ") != -1) | ||||||
|  |             { | ||||||
|  |                 $strCommand .= " \"${strParam}\""; | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 $strCommand .= " ${strParam}"; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     $strCommand .= " ${strNewOperation}"; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #################################################################################################################################### | ||||||
|  | # commandWrite | ||||||
| # | # | ||||||
| # Test a option value. | # Test a option value. | ||||||
| #################################################################################################################################### | #################################################################################################################################### | ||||||
| @@ -1574,7 +1631,7 @@ sub optionTest | |||||||
|         return optionGet($strOption) eq $strValue; |         return optionGet($strOption) eq $strValue; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return defined($oOption{$strOption}); |     return defined($oOption{$strOption}{value}); | ||||||
| } | } | ||||||
|  |  | ||||||
| #################################################################################################################################### | #################################################################################################################################### | ||||||
|   | |||||||
| @@ -497,9 +497,7 @@ sub recovery | |||||||
|     # Write the restore command |     # Write the restore command | ||||||
|     if (!$bRestoreCommandOverride) |     if (!$bRestoreCommandOverride) | ||||||
|     { |     { | ||||||
|         $strRecovery .=  "restore_command = '$self->{strBackRestBin} --stanza=$self->{strStanza}" . |         $strRecovery .=  "restore_command = '" . operationWrite(OP_ARCHIVE_GET) . " %f \"%p\"'\n"; | ||||||
|                          (defined($self->{strConfigFile}) ? " --config=$self->{strConfigFile}" : '') . |  | ||||||
|                          " archive-get %f \"%p\"'\n"; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     # If RECOVERY_TYPE_DEFAULT do not write target options |     # If RECOVERY_TYPE_DEFAULT do not write target options | ||||||
|   | |||||||
| @@ -76,9 +76,10 @@ sub restoreFile | |||||||
|             else |             else | ||||||
|             { |             { | ||||||
|                 my ($strChecksum, $lSize) = $oFile->hash_size(PATH_DB_ABSOLUTE, $strDestinationFile); |                 my ($strChecksum, $lSize) = $oFile->hash_size(PATH_DB_ABSOLUTE, $strDestinationFile); | ||||||
|  |                 my $strManifestChecksum = $oManifest->get($strSection, $strFileName, MANIFEST_SUBKEY_CHECKSUM, false, 'INVALID'); | ||||||
|  |  | ||||||
|                 if (($lSize == $oManifest->get($strSection, $strFileName, MANIFEST_SUBKEY_SIZE) && $lSize == 0) || |                 if (($lSize == $oManifest->get($strSection, $strFileName, MANIFEST_SUBKEY_SIZE) && $lSize == 0) || | ||||||
|                     ($strChecksum eq $oManifest->get($strSection, $strFileName, MANIFEST_SUBKEY_CHECKSUM))) |                     ($strChecksum eq $strManifestChecksum)) | ||||||
|                 { |                 { | ||||||
|                     &log(DEBUG, "${strDestinationFile} exists and is zero size or matches backup checksum"); |                     &log(DEBUG, "${strDestinationFile} exists and is zero size or matches backup checksum"); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -507,6 +507,25 @@ sub BackRestTestConfig_Test | |||||||
|             optionTestExpect(OPTION_RESTORE_RECOVERY_SETTING, 'db.domain.net', 'primary-conn-info'); |             optionTestExpect(OPTION_RESTORE_RECOVERY_SETTING, 'db.domain.net', 'primary-conn-info'); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         if (BackRestTestCommon_Run(++$iRun, OP_RESTORE . ' values passed to ' . OP_ARCHIVE_GET)) | ||||||
|  |         { | ||||||
|  |             optionSetTest($oOption, OPTION_STANZA, $strStanza); | ||||||
|  |             optionSetTest($oOption, OPTION_DB_PATH, '/db path/main'); | ||||||
|  |             optionSetTest($oOption, OPTION_REPO_PATH, '/repo'); | ||||||
|  |             optionSetTest($oOption, OPTION_BACKUP_HOST, 'db.mydomain.com'); | ||||||
|  |  | ||||||
|  |             configLoadExpect($oOption, OP_RESTORE); | ||||||
|  |  | ||||||
|  |             my $strCommand = operationWrite(OP_ARCHIVE_GET); | ||||||
|  |             my $strExpectedCommand = "$0 --backup-host=db.mydomain.com \"--db-path=/db path/main\"" . | ||||||
|  |                                      " --repo-path=/repo --stanza=main " . OP_ARCHIVE_GET; | ||||||
|  |  | ||||||
|  |             if ($strCommand ne $strExpectedCommand) | ||||||
|  |             { | ||||||
|  |                 confess "expected command '${strExpectedCommand}' but got '${strCommand}'"; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         if (BackRestTestCommon_Run(++$iRun, OP_BACKUP . ' valid value ' . OPTION_COMMAND_PSQL)) |         if (BackRestTestCommon_Run(++$iRun, OP_BACKUP . ' valid value ' . OPTION_COMMAND_PSQL)) | ||||||
|         { |         { | ||||||
|             optionSetTest($oOption, OPTION_STANZA, $strStanza); |             optionSetTest($oOption, OPTION_STANZA, $strStanza); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user