From 4845f5c01bade476a054bb717922e9af34e5a8e1 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 25 Apr 2012 15:52:27 -0700 Subject: [PATCH] [FileBackend] Added a script to copy files from one backend to another. Useful for re-sharding. Change-Id: Id942e83420fc5214ddcc8a5b483fd72166a76ff2 --- maintenance/copyFileBackend.php | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 maintenance/copyFileBackend.php diff --git a/maintenance/copyFileBackend.php b/maintenance/copyFileBackend.php new file mode 100644 index 0000000000..314318faa2 --- /dev/null +++ b/maintenance/copyFileBackend.php @@ -0,0 +1,93 @@ +mDescription = "Copy all the files in one backend to another."; + $this->addOption( 'src', 'Backend containing the source files', true, true ); + $this->addOption( 'dst', 'Backend where files should be copied to', true, true ); + $this->addOption( 'containers', 'Pipe separated list of containers', true, true ); + $this->addOption( 'fast', 'Skip SHA-1 checks on pre-existing files' ); + } + + public function execute() { + $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) ); + $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) ); + + $containers = explode( '|', $this->getOption( 'containers' ) ); + foreach ( $containers as $container ) { + $this->output( "Doing container $container...\n" ); + + $srcPathsRel = $src->getFileList( + array( 'dir' => $src->getRootStoragePath() . "/$container" ) ); + if ( $srcPathsRel === null ) { + $this->error( "Could not list files in $container.", 1 ); // die + } + foreach ( $srcPathsRel as $srcPathRel ) { + $srcPath = $src->getRootStoragePath() . "/$container/$srcPathRel"; + $dstPath = $dst->getRootStoragePath() . "/$container/$srcPathRel"; + + if ( $dst->fileExists( array( 'src' => $dstPath, 'latest' => 1 ) ) ) { + if ( $this->hasOption( 'fast' ) ) { + $this->output( "Already have $dstPath.\n" ); + continue; // assume already copied... + } + $srcSha1 = $src->getFileSha1Base36( array( 'src' => $srcPath ) ); + $dstSha1 = $dst->getFileSha1Base36( array( 'src' => $dstPath ) ); + if ( $srcSha1 && $srcSha1 === $dstSha1 ) { + $this->output( "Already have $dstPath.\n" ); + continue; // already copied... + } + } + + $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) ); + if ( !$fsFile ) { + $this->error( "Could not get local copy of $srcPath.", 1 ); // die + } + + $status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) ); + $status->merge( $dst->store( + array( 'src' => $fsFile->getPath(), 'dst' => $dstPath ), + array( 'nonLocking' => 1, 'nonJournaled' => 1 ) + ) ); + if ( !$status->isOK() ) { + print_r( $status->getErrorsArray() ); + $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die + } + + $this->output( "Copied $srcPath to $dstPath.\n" ); + } + } + } +} + +$maintClass = 'CopyFileBackend'; +require_once( RUN_MAINTENANCE_IF_MAIN ); -- 2.20.1