You've already forked pgbackrest
							
							
				mirror of
				https://github.com/pgbackrest/pgbackrest.git
				synced 2025-10-30 23:37:45 +02:00 
			
		
		
		
	move() is working.
This commit is contained in:
		| @@ -487,47 +487,37 @@ sub move | |||||||
|     my $strDestinationFile = shift; |     my $strDestinationFile = shift; | ||||||
|     my $bDestinationPathCreate = shift; |     my $bDestinationPathCreate = shift; | ||||||
|  |  | ||||||
|     # Get the root path for the file list |     # Set defaults | ||||||
|     my $strErrorPrefix = "File->move"; |     $bDestinationPathCreate = defined($bDestinationPathCreate) ? $bDestinationPathCreate : false; | ||||||
|     my $bRemote = $self->is_remote($strSourcePathType); |  | ||||||
|     $bDestinationPathCreate = defined($bDestinationPathCreate) ? $bDestinationPathCreate : true; |  | ||||||
|  |  | ||||||
|     &log(TRACE, "${strErrorPrefix}: " . ($bRemote ? "remote" : "local") . |  | ||||||
|                 " ${strSourcePathType}" . (defined($strSourceFile) ? ":${strSourceFile}" : "") . |  | ||||||
|                 " to ${strDestinationPathType}" . (defined($strDestinationFile) ? ":${strDestinationFile}" : "") . |  | ||||||
|                 ", dest_path_create = " . ($bDestinationPathCreate ? "true" : "false")); |  | ||||||
|  |  | ||||||
|     # Get source and desination files |  | ||||||
|     if ($self->path_type_get($strSourcePathType) ne $self->path_type_get($strSourcePathType)) |  | ||||||
|     { |  | ||||||
|         confess &log(ASSERT, "source and destination path types must be equal"); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|  |     # Set operation variables | ||||||
|     my $strPathOpSource = $self->path_get($strSourcePathType, $strSourceFile); |     my $strPathOpSource = $self->path_get($strSourcePathType, $strSourceFile); | ||||||
|     my $strPathOpDestination = $self->path_get($strDestinationPathType, $strDestinationFile); |     my $strPathOpDestination = $self->path_get($strDestinationPathType, $strDestinationFile); | ||||||
|  |  | ||||||
|     # Run remotely |     # Set operation and debug strings | ||||||
|     if ($bRemote) |     my $strOperation = OP_FILE_MOVE; | ||||||
|     { |  | ||||||
|         my $strCommand = $self->{strCommand} . |  | ||||||
|                          ($bDestinationPathCreate ? " --dest-path-create" : "") . |  | ||||||
|                          " move ${strPathOpSource} ${strPathOpDestination}"; |  | ||||||
|  |  | ||||||
|         # Run via SSH |     my $strDebug = "${strSourcePathType}" . (defined($strSourceFile) ? ":${strSourceFile}" : "") . | ||||||
|         my $oSSH = $self->remote_get($strSourcePathType); |                    " to ${strDestinationPathType}" . (defined($strDestinationFile) ? ":${strDestinationFile}" : "") . | ||||||
|         my $strOutput = $oSSH->capture($strCommand); |                    ", destination_path_create = " . ($bDestinationPathCreate ? "true" : "false"); | ||||||
|  |     &log(DEBUG, "${strOperation}: ${strDebug}"); | ||||||
|  |  | ||||||
|         # Handle any errors |     # Source and destination path types must be the same | ||||||
|         if ($oSSH->error) |     if ($self->path_type_get($strSourcePathType) ne $self->path_type_get($strSourcePathType)) | ||||||
|     { |     { | ||||||
|             confess &log(ERROR, "${strErrorPrefix} remote (${strCommand}): " . (defined($strOutput) ? $strOutput : $oSSH->error)); |         confess &log(ASSERT, "${strDebug}: source and destination path types must be equal"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     # Run remotely | ||||||
|  |     if ($self->is_remote($strSourcePathType)) | ||||||
|  |     { | ||||||
|  |         confess "${strDebug}: remote operation not supported"; | ||||||
|     } |     } | ||||||
|     # Run locally |     # Run locally | ||||||
|     else |     else | ||||||
|     { |     { | ||||||
|         # If the destination path does not exist, create it |         # If the destination path does not exist, create it or error out | ||||||
|         unless (-e dirname($strPathOpDestination)) |         if (!$self->exists($strDestinationPathType, dirname($strDestinationFile))) | ||||||
|         { |         { | ||||||
|             if ($bDestinationPathCreate) |             if ($bDestinationPathCreate) | ||||||
|             { |             { | ||||||
| @@ -543,16 +533,16 @@ sub move | |||||||
|                     exit (COMMAND_ERR_PATH_MISSING); |                     exit (COMMAND_ERR_PATH_MISSING); | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 confess &log(ERROR, "${strErrorPrefix}: " . $strError); |                 confess &log(ERROR, "${strDebug}: " . $strError); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (!rename($strPathOpSource, $strPathOpDestination)) |         if (!rename($strPathOpSource, $strPathOpDestination)) | ||||||
|         { |         { | ||||||
|             my $strError = "${strPathOpSource} could not be moved:" . $!; |             my $strError = "${strPathOpSource} could not be moved: " . $!; | ||||||
|             my $iErrorCode = COMMAND_ERR_FILE_MOVE; |             my $iErrorCode = COMMAND_ERR_FILE_MOVE; | ||||||
|  |  | ||||||
|             unless (-e $strPathOpSource) |             if (!$self->exists($strSourcePathType, dirname($strSourceFile))) | ||||||
|             { |             { | ||||||
|                 $strError = "${strPathOpSource} does not exist"; |                 $strError = "${strPathOpSource} does not exist"; | ||||||
|                 $iErrorCode = COMMAND_ERR_FILE_MISSING; |                 $iErrorCode = COMMAND_ERR_FILE_MISSING; | ||||||
| @@ -564,7 +554,7 @@ sub move | |||||||
|                 exit ($iErrorCode); |                 exit ($iErrorCode); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             confess &log(ERROR, "${strErrorPrefix}: " . $strError); |             confess &log(ERROR, "${strDebug}: " . $strError); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -25,6 +25,36 @@ use BackRest::Remote; | |||||||
| use Exporter qw(import); | use Exporter qw(import); | ||||||
| our @EXPORT = qw(BackRestFileTest); | our @EXPORT = qw(BackRestFileTest); | ||||||
|  |  | ||||||
|  | my $strTestPath; | ||||||
|  | my $strHost; | ||||||
|  | my $strUserBackRest; | ||||||
|  |  | ||||||
|  | #################################################################################################################################### | ||||||
|  | # BackRestFileTestSetup | ||||||
|  | #################################################################################################################################### | ||||||
|  | sub BackRestFileTestSetup | ||||||
|  | { | ||||||
|  |     my $bDropOnly = shift; | ||||||
|  |      | ||||||
|  |     # Remove the backrest private directory | ||||||
|  |     system("ssh ${strUserBackRest}\@${strHost} 'rm -rf ${strTestPath}/private'") == 0 or die 'unable to remove test/private path'; | ||||||
|  |      | ||||||
|  |     # Remove the test directory | ||||||
|  |     system("rm -rf ${strTestPath}") == 0 or die 'unable to drop test path'; | ||||||
|  |      | ||||||
|  |     if (defined($bDropOnly) || !$bDropOnly) | ||||||
|  |     { | ||||||
|  |         # Create the test directory | ||||||
|  |         system("mkdir ${strTestPath}") == 0 or confess "Unable to create test directory"; | ||||||
|  |          | ||||||
|  |         # Create the backrest private directory | ||||||
|  |         system("ssh backrest\@${strHost} 'mkdir -m 700 ${strTestPath}/private'") == 0 or die 'unable to create test/private path'; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #################################################################################################################################### | ||||||
|  | # BackRestFileTest | ||||||
|  | #################################################################################################################################### | ||||||
| sub BackRestFileTest | sub BackRestFileTest | ||||||
| { | { | ||||||
|     my $strTest = shift; |     my $strTest = shift; | ||||||
| @@ -36,14 +66,15 @@ sub BackRestFileTest | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     # Setup test paths |     # Setup test paths | ||||||
|     my $strTestPath = dirname(abs_path($0)) . "/test"; |     $strTestPath = dirname(abs_path($0)) . "/test"; | ||||||
|     my $iRun; |     my $iRun; | ||||||
|  |  | ||||||
|     my $strStanza = "db"; |     my $strStanza = "db"; | ||||||
|     my $strCommand = "/Users/dsteele/pg_backrest/bin/pg_backrest_remote.pl"; |     my $strCommand = "/Users/dsteele/pg_backrest/bin/pg_backrest_remote.pl"; | ||||||
|     my $strHost = "127.0.0.1"; |     $strHost = "127.0.0.1"; | ||||||
|     my $strUser = getpwuid($<); |     my $strUser = getpwuid($<); | ||||||
|     my $strGroup = getgrgid($(); |     my $strGroup = getgrgid($(); | ||||||
|  |     $strUserBackRest = 'backrest'; | ||||||
|  |  | ||||||
|     # Print test parameters |     # Print test parameters | ||||||
|     &log(INFO, "Testing with test_path = ${strTestPath}, host = ${strHost}, user = ${strUser}, group = ${strGroup}"); |     &log(INFO, "Testing with test_path = ${strTestPath}, host = ${strHost}, user = ${strUser}, group = ${strGroup}"); | ||||||
| @@ -177,53 +208,62 @@ sub BackRestFileTest | |||||||
|         &log(INFO, "--------------------------------------------------------------------------------"); |         &log(INFO, "--------------------------------------------------------------------------------"); | ||||||
|         &log(INFO, "Test File->move()\n"); |         &log(INFO, "Test File->move()\n"); | ||||||
|  |  | ||||||
|         for (my $bRemote = 0; $bRemote <= 1; $bRemote++) |         for (my $bRemote = 0; $bRemote <= 0; $bRemote++) | ||||||
|         { |         { | ||||||
|  |             # Create the file object | ||||||
|             my $oFile = BackRest::File->new |             my $oFile = BackRest::File->new | ||||||
|             ( |             ( | ||||||
|                 strStanza => $strStanza, |                 strStanza => "db", | ||||||
|                 bNoCompression => true, |                 strBackupClusterPath => undef, | ||||||
|                 strCommand => $strCommand, |  | ||||||
|                 strBackupClusterPath => ${strTestPath}, |  | ||||||
|                 strBackupPath => ${strTestPath}, |                 strBackupPath => ${strTestPath}, | ||||||
|                 strBackupHost => $bRemote ? $strHost : undef, |                 strRemote => $bRemote ? 'backup' : undef, | ||||||
|                 strBackupUser => $bRemote ? $strUser : undef |                 oRemote => $bRemote ? $oRemote : undef | ||||||
|             ); |             ); | ||||||
|  |  | ||||||
|             # Loop through source exists |             # Loop through source exists | ||||||
|             for (my $bSourceExists = 0; $bSourceExists <= 1; $bSourceExists++) |             for (my $bSourceExists = 0; $bSourceExists <= 1; $bSourceExists++) | ||||||
|             { |             { | ||||||
|  |             # Loop through source errors | ||||||
|  |             for (my $bSourceError = 0; $bSourceError <= 1; $bSourceError++) | ||||||
|  |             { | ||||||
|             # Loop through destination exists |             # Loop through destination exists | ||||||
|             for (my $bDestinationExists = 0; $bDestinationExists <= 1; $bDestinationExists++) |             for (my $bDestinationExists = 0; $bDestinationExists <= 1; $bDestinationExists++) | ||||||
|             { |             { | ||||||
|  |             # Loop through source errors | ||||||
|  |             for (my $bDestinationError = 0; $bDestinationError <= 1; $bDestinationError++) | ||||||
|  |             { | ||||||
|             # Loop through create |             # Loop through create | ||||||
|             for (my $bCreate = 0; $bCreate <= $bDestinationExists; $bCreate++) |             for (my $bCreate = 0; $bCreate <= $bDestinationExists; $bCreate++) | ||||||
|             { |             { | ||||||
|                 $iRun++; |                 $iRun++; | ||||||
|  |  | ||||||
|                 &log(INFO, "run ${iRun} - " . |                 &log(INFO, "run ${iRun} - " . | ||||||
|                                    "remote $bRemote, src_exists $bSourceExists, dst_exists $bDestinationExists, create $bCreate"); |                            "remote $bRemote" . | ||||||
|  |                            ", src_exists $bSourceExists, src_error $bSourceError" . | ||||||
|  |                            ", dst_exists $bDestinationExists, dst_error $bDestinationError, dst_create $bCreate"); | ||||||
|  |  | ||||||
|                         # Drop the old test directory and create a new one |                 # Setup test directory | ||||||
|                         system("rm -rf test"); |                 BackRestFileTestSetup(); | ||||||
|                         system("mkdir test") == 0 or confess "Unable to create test directory"; |  | ||||||
|  |  | ||||||
|                 my $strSourceFile = "${strTestPath}/test.txt"; |                 my $strSourceFile = "${strTestPath}/test.txt"; | ||||||
|                 my $strDestinationFile = "${strTestPath}/test-dest.txt"; |                 my $strDestinationFile = "${strTestPath}/test-dest.txt"; | ||||||
|  |  | ||||||
|                         if ($bCreate) |                 if ($bSourceError) | ||||||
|                 { |                 { | ||||||
|                             $strDestinationFile = "${strTestPath}/sub/test-dest.txt" |                     $strSourceFile = "${strTestPath}/private/test.txt"; | ||||||
|                 } |                 } | ||||||
|  |                 elsif ($bSourceExists) | ||||||
|                         if ($bSourceExists) |  | ||||||
|                 { |                 { | ||||||
|                     system("echo 'TESTDATA' > ${strSourceFile}"); |                     system("echo 'TESTDATA' > ${strSourceFile}"); | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                         if (!$bDestinationExists) |                 if ($bDestinationError) | ||||||
|                 { |                 { | ||||||
|                             $strDestinationFile = "error" . $strDestinationFile; |                     $strSourceFile = "${strTestPath}/private/test.txt"; | ||||||
|  |                 } | ||||||
|  |                 elsif (!$bDestinationExists) | ||||||
|  |                 { | ||||||
|  |                     $strDestinationFile = "${strTestPath}/sub/test-dest.txt"; | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 # Execute in eval in case of error |                 # Execute in eval in case of error | ||||||
| @@ -234,7 +274,7 @@ sub BackRestFileTest | |||||||
|  |  | ||||||
|                 if ($@) |                 if ($@) | ||||||
|                 { |                 { | ||||||
|                             if (!$bSourceExists || !$bDestinationExists) |                     if (!$bSourceExists || (!$bDestinationExists && !$bCreate) || $bSourceError || $bDestinationError) | ||||||
|                     { |                     { | ||||||
|                         next; |                         next; | ||||||
|                     } |                     } | ||||||
| @@ -242,7 +282,7 @@ sub BackRestFileTest | |||||||
|                     confess "error raised: " . $@ . "\n"; |                     confess "error raised: " . $@ . "\n"; | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                         if (!$bSourceExists || !$bDestinationExists) |                 if (!$bSourceExists || (!$bDestinationExists && !$bCreate) || $bSourceError || $bDestinationError) | ||||||
|                 { |                 { | ||||||
|                     confess "error should have been raised"; |                     confess "error should have been raised"; | ||||||
|                 } |                 } | ||||||
| @@ -256,6 +296,8 @@ sub BackRestFileTest | |||||||
|             } |             } | ||||||
|             } |             } | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     #------------------------------------------------------------------------------------------------------------------------------- |     #------------------------------------------------------------------------------------------------------------------------------- | ||||||
|     # Test compress() |     # Test compress() | ||||||
| @@ -962,3 +1004,5 @@ sub BackRestFileTest | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | 1; | ||||||
		Reference in New Issue
	
	Block a user