2012-04-25 22:52:27 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-06 18:49:59 +00:00
|
|
|
* Copy all files in some containers of one backend to another.
|
2012-04-25 22:52:27 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
2012-06-16 20:35:13 +00:00
|
|
|
* @file
|
2012-04-25 22:52:27 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2012-08-27 19:03:15 +00:00
|
|
|
require_once( __DIR__ . '/Maintenance.php' );
|
2012-04-25 22:52:27 +00:00
|
|
|
|
2012-06-16 20:35:13 +00:00
|
|
|
/**
|
|
|
|
|
* Copy all files in one container 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.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2012-04-25 22:52:27 +00:00
|
|
|
class CopyFileBackend extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2012-05-06 18:49:59 +00:00
|
|
|
$this->mDescription = "Copy files in one backend to another.";
|
2012-04-25 22:52:27 +00:00
|
|
|
$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 );
|
2012-05-06 18:49:59 +00:00
|
|
|
$this->addOption( 'subdir', 'Only do items in this child directory', false, true );
|
2012-07-11 23:34:43 +00:00
|
|
|
$this->addOption( 'ratefile', 'File to check periodically for batch size', false, true );
|
|
|
|
|
$this->addOption( 'skiphash', 'Skip SHA-1 sync checks for files' );
|
2012-07-12 17:14:08 +00:00
|
|
|
$this->addOption( 'missingonly', 'Only copy files missing from destination listing' );
|
2012-08-10 05:23:44 +00:00
|
|
|
$this->addOption( 'utf8only', 'Skip source files that do not have valid UTF-8 names' );
|
2012-05-06 18:49:59 +00:00
|
|
|
$this->setBatchSize( 50 );
|
2012-04-25 22:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) );
|
|
|
|
|
$dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) );
|
|
|
|
|
$containers = explode( '|', $this->getOption( 'containers' ) );
|
2012-05-06 18:49:59 +00:00
|
|
|
$subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
|
|
|
|
|
|
2012-07-09 23:30:44 +00:00
|
|
|
$rateFile = $this->getOption( 'ratefile' );
|
|
|
|
|
|
2012-08-10 05:23:44 +00:00
|
|
|
if ( $this->hasOption( 'utf8only' ) && !extension_loaded( 'mbstring' ) ) {
|
|
|
|
|
$this->error( "Cannot check for UTF-8, mbstring extension missing.", 1 ); // die
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 18:49:59 +00:00
|
|
|
$count = 0;
|
2012-04-25 22:52:27 +00:00
|
|
|
foreach ( $containers as $container ) {
|
2012-05-06 18:49:59 +00:00
|
|
|
if ( $subDir != '' ) {
|
|
|
|
|
$backendRel = "$container/$subDir";
|
|
|
|
|
$this->output( "Doing container '$container', directory '$subDir'...\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$backendRel = $container;
|
|
|
|
|
$this->output( "Doing container '$container'...\n" );
|
|
|
|
|
}
|
2012-04-25 22:52:27 +00:00
|
|
|
|
2012-07-12 17:14:08 +00:00
|
|
|
$srcPathsRel = $src->getFileList( array(
|
|
|
|
|
'dir' => $src->getRootStoragePath() . "/$backendRel" ) );
|
2012-04-25 22:52:27 +00:00
|
|
|
if ( $srcPathsRel === null ) {
|
|
|
|
|
$this->error( "Could not list files in $container.", 1 ); // die
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 17:14:08 +00:00
|
|
|
// Do a listing comparison if specified
|
|
|
|
|
if ( $this->hasOption( 'missingonly' ) ) {
|
|
|
|
|
$dstPathsRel = $dst->getFileList( array(
|
|
|
|
|
'dir' => $dst->getRootStoragePath() . "/$backendRel" ) );
|
|
|
|
|
if ( $dstPathsRel === null ) {
|
|
|
|
|
$this->error( "Could not list files in $container.", 1 ); // die
|
|
|
|
|
}
|
2013-05-01 01:42:03 +00:00
|
|
|
// Get the list of destination files
|
|
|
|
|
$relFilesDstSha1 = array();
|
2012-07-12 17:14:08 +00:00
|
|
|
foreach ( $dstPathsRel as $dstPathRel ) {
|
2013-05-01 01:42:03 +00:00
|
|
|
$relFilesDstSha1[sha1( $dstPathRel )] = 1;
|
|
|
|
|
}
|
|
|
|
|
unset( $dstPathsRel ); // free
|
|
|
|
|
// Get the list of missing files
|
|
|
|
|
$missingPathsRel = array();
|
|
|
|
|
foreach ( $srcPathsRel as $srcPathRel ) {
|
|
|
|
|
if ( !isset( $relFilesDstSha1[sha1( $srcPathRel )] ) ) {
|
|
|
|
|
$missingPathsRel[] = $srcPathRel;
|
|
|
|
|
}
|
2012-07-12 17:14:08 +00:00
|
|
|
}
|
2013-05-01 01:42:03 +00:00
|
|
|
unset( $srcPathsRel ); // free
|
2012-07-12 17:14:08 +00:00
|
|
|
// Only copy the missing files over in the next loop
|
2013-05-01 01:42:03 +00:00
|
|
|
$srcPathsRel = $missingPathsRel;
|
2012-07-12 17:14:08 +00:00
|
|
|
$this->output( count( $srcPathsRel ) . " file(s) need to be copied.\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 18:49:59 +00:00
|
|
|
$batchPaths = array();
|
|
|
|
|
foreach ( $srcPathsRel as $srcPathRel ) {
|
2012-07-09 23:30:44 +00:00
|
|
|
// Check up on the rate file periodically to adjust the concurrency
|
|
|
|
|
if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
|
|
|
|
|
$this->mBatchSize = max( 1, (int)file_get_contents( $rateFile ) );
|
|
|
|
|
$this->output( "Batch size is now {$this->mBatchSize}.\n" );
|
|
|
|
|
}
|
2012-05-06 18:49:59 +00:00
|
|
|
$batchPaths[$srcPathRel] = 1; // remove duplicates
|
|
|
|
|
if ( count( $batchPaths ) >= $this->mBatchSize ) {
|
|
|
|
|
$this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
|
|
|
|
|
$batchPaths = array(); // done
|
2012-04-25 22:52:27 +00:00
|
|
|
}
|
2012-05-06 18:49:59 +00:00
|
|
|
++$count;
|
|
|
|
|
}
|
|
|
|
|
if ( count( $batchPaths ) ) { // left-overs
|
|
|
|
|
$this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
|
|
|
|
|
$batchPaths = array(); // done
|
|
|
|
|
}
|
2012-04-25 22:52:27 +00:00
|
|
|
|
2012-05-06 18:49:59 +00:00
|
|
|
if ( $subDir != '' ) {
|
|
|
|
|
$this->output( "Finished container '$container', directory '$subDir'.\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "Finished container '$container'.\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-25 22:52:27 +00:00
|
|
|
|
2012-05-06 18:49:59 +00:00
|
|
|
$this->output( "Done [$count file(s)].\n" );
|
|
|
|
|
}
|
2012-04-25 22:52:27 +00:00
|
|
|
|
2012-05-06 18:49:59 +00:00
|
|
|
protected function copyFileBatch(
|
|
|
|
|
array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst
|
|
|
|
|
) {
|
|
|
|
|
$ops = array();
|
|
|
|
|
$fsFiles = array();
|
2012-07-10 19:20:55 +00:00
|
|
|
$copiedRel = array(); // for output message
|
2013-01-22 21:54:01 +00:00
|
|
|
|
|
|
|
|
// Download the batch of source files into backend cache...
|
|
|
|
|
if ( $this->hasOption( 'missingonly' ) ) {
|
|
|
|
|
$srcPaths = array();
|
|
|
|
|
foreach ( $srcPathsRel as $srcPathRel ) {
|
|
|
|
|
$srcPaths[] = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
|
|
|
|
|
}
|
2013-02-25 18:26:25 +00:00
|
|
|
$t_start = microtime( true );
|
2013-01-22 21:54:01 +00:00
|
|
|
$fsFiles = $src->getLocalReferenceMulti( array( 'srcs' => $srcPaths, 'latest' => 1 ) );
|
2013-02-25 18:26:25 +00:00
|
|
|
$ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
|
|
|
|
|
$this->output( "\nDownloaded these file(s) [{$ellapsed_ms}ms]:\n" .
|
|
|
|
|
implode( "\n", $srcPaths ) . "\n\n" );
|
2013-01-22 21:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine what files need to be copied over...
|
2012-05-06 18:49:59 +00:00
|
|
|
foreach ( $srcPathsRel as $srcPathRel ) {
|
|
|
|
|
$srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
|
|
|
|
|
$dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
|
2012-08-10 05:23:44 +00:00
|
|
|
if ( $this->hasOption( 'utf8only' ) && !mb_check_encoding( $srcPath, 'UTF-8' ) ) {
|
|
|
|
|
$this->error( "Detected illegal (non-UTF8) path for $srcPath." );
|
|
|
|
|
continue;
|
|
|
|
|
} elseif ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
|
2012-05-06 18:49:59 +00:00
|
|
|
$this->output( "Already have $srcPathRel.\n" );
|
|
|
|
|
continue; // assume already copied...
|
|
|
|
|
}
|
2013-01-22 21:54:01 +00:00
|
|
|
$fsFile = array_key_exists( $srcPath, $fsFiles )
|
|
|
|
|
? $fsFiles[$srcPath]
|
|
|
|
|
: $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
|
2012-05-06 18:49:59 +00:00
|
|
|
if ( !$fsFile ) {
|
|
|
|
|
$this->error( "Could not get local copy of $srcPath.", 1 ); // die
|
2012-07-28 17:38:25 +00:00
|
|
|
} elseif ( !$fsFile->exists() ) {
|
|
|
|
|
// FSFileBackends just return the path for getLocalReference() and paths with
|
|
|
|
|
// illegal slashes may get normalized to a different path. This can cause the
|
|
|
|
|
// local reference to not exist...skip these broken files.
|
|
|
|
|
$this->error( "Detected possible illegal path for $srcPath." );
|
|
|
|
|
continue;
|
2012-05-06 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
$fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
|
|
|
|
|
// Note: prepare() is usually fast for key/value backends
|
2012-07-12 19:48:16 +00:00
|
|
|
$status = $dst->prepare( array( 'dir' => dirname( $dstPath ), 'bypassReadOnly' => 1 ) );
|
2012-05-06 18:49:59 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->error( print_r( $status->getErrorsArray(), true ) );
|
|
|
|
|
$this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
|
2012-04-25 22:52:27 +00:00
|
|
|
}
|
2012-05-06 18:49:59 +00:00
|
|
|
$ops[] = array( 'op' => 'store',
|
|
|
|
|
'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
|
2012-07-10 19:20:55 +00:00
|
|
|
$copiedRel[] = $srcPathRel;
|
2012-04-25 22:52:27 +00:00
|
|
|
}
|
2012-05-06 18:49:59 +00:00
|
|
|
|
2013-01-22 21:54:01 +00:00
|
|
|
// Copy in the batch of source files...
|
2012-07-10 18:33:24 +00:00
|
|
|
$t_start = microtime( true );
|
2012-07-12 19:48:16 +00:00
|
|
|
$status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
|
2012-07-12 23:33:27 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
sleep( 10 ); // wait and retry copy again
|
2012-07-12 19:48:16 +00:00
|
|
|
$status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
|
2012-07-12 23:33:27 +00:00
|
|
|
}
|
2012-07-10 18:33:24 +00:00
|
|
|
$ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
|
2012-05-06 18:49:59 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->error( print_r( $status->getErrorsArray(), true ) );
|
|
|
|
|
$this->error( "Could not copy file batch.", 1 ); // die
|
2012-07-10 19:20:55 +00:00
|
|
|
} elseif ( count( $copiedRel ) ) {
|
2012-07-10 18:33:24 +00:00
|
|
|
$this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" .
|
2012-07-10 19:20:55 +00:00
|
|
|
implode( "\n", $copiedRel ) . "\n\n" );
|
2012-05-06 18:49:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) {
|
2012-07-11 23:34:43 +00:00
|
|
|
$skipHash = $this->hasOption( 'skiphash' );
|
2012-05-06 18:49:59 +00:00
|
|
|
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
|
2012-07-11 23:34:43 +00:00
|
|
|
) && ( $skipHash || ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
|
2012-05-06 18:49:59 +00:00
|
|
|
=== $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
|
2012-07-11 23:34:43 +00:00
|
|
|
) )
|
2012-05-06 18:49:59 +00:00
|
|
|
);
|
2012-04-25 22:52:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$maintClass = 'CopyFileBackend';
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|