wiki.techinc.nl/maintenance/copyFileBackend.php
Aaron Schulz ce0677a1a2 [FileBackend] Changed copy script to use batches for concurrency.
* Also added a 'subdir' option so multiple shards can be done at once.

Change-Id: Id8c3a89a4cb30978f66db3bf95ebfebc0a9c01b4
2012-05-06 18:07:29 -07:00

139 lines
5.5 KiB
PHP

<?php
/**
* Copy all files in some containers of one backend to another.
*
* This can also be used to re-shard the files for one backend using the
* config of second backend. The second backend should have the same config
* as the first, except for it having a different name and different sharding
* configuration. The backend should be made read-only while this runs.
* After this script finishes, the old files in the containers can be deleted.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @ingroup Maintenance
*/
require_once( dirname( __FILE__ ) . '/Maintenance.php' );
class CopyFileBackend extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = "Copy 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( 'subdir', 'Only do items in this child directory', false, true );
$this->setBatchSize( 50 );
}
public function execute() {
$src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) );
$dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) );
$containers = explode( '|', $this->getOption( 'containers' ) );
$subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
$count = 0;
foreach ( $containers as $container ) {
if ( $subDir != '' ) {
$backendRel = "$container/$subDir";
$this->output( "Doing container '$container', directory '$subDir'...\n" );
} else {
$backendRel = $container;
$this->output( "Doing container '$container'...\n" );
}
$dir = $src->getRootStoragePath() . "/$backendRel";
$srcPathsRel = $src->getFileList( array( 'dir' => $dir ) );
if ( $srcPathsRel === null ) {
$this->error( "Could not list files in $container.", 1 ); // die
}
$batchPaths = array();
foreach ( $srcPathsRel as $srcPathRel ) {
$batchPaths[$srcPathRel] = 1; // remove duplicates
if ( count( $batchPaths ) >= $this->mBatchSize ) {
$this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
$batchPaths = array(); // done
}
++$count;
}
if ( count( $batchPaths ) ) { // left-overs
$this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
$batchPaths = array(); // done
}
if ( $subDir != '' ) {
$this->output( "Finished container '$container', directory '$subDir'.\n" );
} else {
$this->output( "Finished container '$container'.\n" );
}
}
$this->output( "Done [$count file(s)].\n" );
}
protected function copyFileBatch(
array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst
) {
$ops = array();
$fsFiles = array();
foreach ( $srcPathsRel as $srcPathRel ) {
$srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
$dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
if ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
$this->output( "Already have $srcPathRel.\n" );
continue; // assume already copied...
}
// Note: getLocalReference() is fast for FS backends
$fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
if ( !$fsFile ) {
$this->error( "Could not get local copy of $srcPath.", 1 ); // die
}
$fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
// Note: prepare() is usually fast for key/value backends
$status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) );
if ( !$status->isOK() ) {
$this->error( print_r( $status->getErrorsArray(), true ) );
$this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
}
$ops[] = array( 'op' => 'store',
'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
}
$status = $dst->doOperations( $ops, array( 'nonJournaled' => 1 ) );
if ( !$status->isOK() ) {
$this->error( print_r( $status->getErrorsArray(), true ) );
$this->error( "Could not copy file batch.", 1 ); // die
} else {
$this->output( "Copied these file(s):\n" . implode( "\n", $srcPathsRel ) . "\n\n" );
}
}
protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) {
return (
( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
=== $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
=== $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
) && ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
=== $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
)
);
}
}
$maintClass = 'CopyFileBackend';
require_once( RUN_MAINTENANCE_IF_MAIN );