3 * Copy all files in some containers of one backend to another.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once( dirname( __FILE__
) . '/Maintenance.php' );
27 * Copy all files in one container of one backend to another.
29 * This can also be used to re-shard the files for one backend using the
30 * config of second backend. The second backend should have the same config
31 * as the first, except for it having a different name and different sharding
32 * configuration. The backend should be made read-only while this runs.
33 * After this script finishes, the old files in the containers can be deleted.
35 * @ingroup Maintenance
37 class CopyFileBackend
extends Maintenance
{
38 public function __construct() {
39 parent
::__construct();
40 $this->mDescription
= "Copy files in one backend to another.";
41 $this->addOption( 'src', 'Backend containing the source files', true, true );
42 $this->addOption( 'dst', 'Backend where files should be copied to', true, true );
43 $this->addOption( 'containers', 'Pipe separated list of containers', true, true );
44 $this->addOption( 'subdir', 'Only do items in this child directory', false, true );
45 $this->addOption( 'ratefile', 'File to check periodically for batch size', false, true );
46 $this->addOption( 'skiphash', 'Skip SHA-1 sync checks for files' );
47 $this->setBatchSize( 50 );
50 public function execute() {
51 $src = FileBackendGroup
::singleton()->get( $this->getOption( 'src' ) );
52 $dst = FileBackendGroup
::singleton()->get( $this->getOption( 'dst' ) );
53 $containers = explode( '|', $this->getOption( 'containers' ) );
54 $subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
56 $rateFile = $this->getOption( 'ratefile' );
59 foreach ( $containers as $container ) {
60 if ( $subDir != '' ) {
61 $backendRel = "$container/$subDir";
62 $this->output( "Doing container '$container', directory '$subDir'...\n" );
64 $backendRel = $container;
65 $this->output( "Doing container '$container'...\n" );
68 $dir = $src->getRootStoragePath() . "/$backendRel";
69 $srcPathsRel = $src->getFileList( array( 'dir' => $dir ) );
70 if ( $srcPathsRel === null ) {
71 $this->error( "Could not list files in $container.", 1 ); // die
74 $batchPaths = array();
75 foreach ( $srcPathsRel as $srcPathRel ) {
76 // Check up on the rate file periodically to adjust the concurrency
77 if ( $rateFile && ( !$count ||
( $count %
500 ) == 0 ) ) {
78 $this->mBatchSize
= max( 1, (int)file_get_contents( $rateFile ) );
79 $this->output( "Batch size is now {$this->mBatchSize}.\n" );
81 $batchPaths[$srcPathRel] = 1; // remove duplicates
82 if ( count( $batchPaths ) >= $this->mBatchSize
) {
83 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
84 $batchPaths = array(); // done
88 if ( count( $batchPaths ) ) { // left-overs
89 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
90 $batchPaths = array(); // done
93 if ( $subDir != '' ) {
94 $this->output( "Finished container '$container', directory '$subDir'.\n" );
96 $this->output( "Finished container '$container'.\n" );
100 $this->output( "Done [$count file(s)].\n" );
103 protected function copyFileBatch(
104 array $srcPathsRel, $backendRel, FileBackend
$src, FileBackend
$dst
108 $copiedRel = array(); // for output message
109 foreach ( $srcPathsRel as $srcPathRel ) {
110 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
111 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
112 if ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
113 $this->output( "Already have $srcPathRel.\n" );
114 continue; // assume already copied...
116 // Note: getLocalReference() is fast for FS backends
117 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
119 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
121 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
122 // Note: prepare() is usually fast for key/value backends
123 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) );
124 if ( !$status->isOK() ) {
125 $this->error( print_r( $status->getErrorsArray(), true ) );
126 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
128 $ops[] = array( 'op' => 'store',
129 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
130 $copiedRel[] = $srcPathRel;
133 $t_start = microtime( true );
134 $status = $dst->doOperations( $ops, array( 'nonJournaled' => 1 ) );
135 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
136 if ( !$status->isOK() ) {
137 $this->error( print_r( $status->getErrorsArray(), true ) );
138 $this->error( "Could not copy file batch.", 1 ); // die
139 } elseif ( count( $copiedRel ) ) {
140 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" .
141 implode( "\n", $copiedRel ) . "\n\n" );
145 protected function filesAreSame( FileBackend
$src, FileBackend
$dst, $sPath, $dPath ) {
146 $skipHash = $this->hasOption( 'skiphash' );
148 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
149 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
150 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
151 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
152 ) && ( $skipHash ||
( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
153 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
159 $maintClass = 'CopyFileBackend';
160 require_once( RUN_MAINTENANCE_IF_MAIN
);