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( __DIR__
. '/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->addOption( 'missingonly', 'Only copy files missing from destination listing' );
48 $this->addOption( 'utf8only', 'Skip source files that do not have valid UTF-8 names' );
49 $this->setBatchSize( 50 );
52 public function execute() {
53 $src = FileBackendGroup
::singleton()->get( $this->getOption( 'src' ) );
54 $dst = FileBackendGroup
::singleton()->get( $this->getOption( 'dst' ) );
55 $containers = explode( '|', $this->getOption( 'containers' ) );
56 $subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
58 $rateFile = $this->getOption( 'ratefile' );
60 if ( $this->hasOption( 'utf8only' ) && !extension_loaded( 'mbstring' ) ) {
61 $this->error( "Cannot check for UTF-8, mbstring extension missing.", 1 ); // die
65 foreach ( $containers as $container ) {
66 if ( $subDir != '' ) {
67 $backendRel = "$container/$subDir";
68 $this->output( "Doing container '$container', directory '$subDir'...\n" );
70 $backendRel = $container;
71 $this->output( "Doing container '$container'...\n" );
74 $srcPathsRel = $src->getFileList( array(
75 'dir' => $src->getRootStoragePath() . "/$backendRel" ) );
76 if ( $srcPathsRel === null ) {
77 $this->error( "Could not list files in $container.", 1 ); // die
80 // Do a listing comparison if specified
81 if ( $this->hasOption( 'missingonly' ) ) {
82 $relFilesSrc = array();
83 $relFilesDst = array();
84 foreach ( $srcPathsRel as $srcPathRel ) {
85 $relFilesSrc[] = $srcPathRel;
87 $dstPathsRel = $dst->getFileList( array(
88 'dir' => $dst->getRootStoragePath() . "/$backendRel" ) );
89 if ( $dstPathsRel === null ) {
90 $this->error( "Could not list files in $container.", 1 ); // die
92 foreach ( $dstPathsRel as $dstPathRel ) {
93 $relFilesDst[] = $dstPathRel;
95 // Only copy the missing files over in the next loop
96 $srcPathsRel = array_diff( $relFilesSrc, $relFilesDst );
97 $this->output( count( $srcPathsRel ) . " file(s) need to be copied.\n" );
98 unset( $relFilesSrc );
99 unset( $relFilesDst );
102 $batchPaths = array();
103 foreach ( $srcPathsRel as $srcPathRel ) {
104 // Check up on the rate file periodically to adjust the concurrency
105 if ( $rateFile && ( !$count ||
( $count %
500 ) == 0 ) ) {
106 $this->mBatchSize
= max( 1, (int)file_get_contents( $rateFile ) );
107 $this->output( "Batch size is now {$this->mBatchSize}.\n" );
109 $batchPaths[$srcPathRel] = 1; // remove duplicates
110 if ( count( $batchPaths ) >= $this->mBatchSize
) {
111 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
112 $batchPaths = array(); // done
116 if ( count( $batchPaths ) ) { // left-overs
117 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
118 $batchPaths = array(); // done
121 if ( $subDir != '' ) {
122 $this->output( "Finished container '$container', directory '$subDir'.\n" );
124 $this->output( "Finished container '$container'.\n" );
128 $this->output( "Done [$count file(s)].\n" );
131 protected function copyFileBatch(
132 array $srcPathsRel, $backendRel, FileBackend
$src, FileBackend
$dst
136 $copiedRel = array(); // for output message
138 // Download the batch of source files into backend cache...
139 if ( $this->hasOption( 'missingonly' ) ) {
141 foreach ( $srcPathsRel as $srcPathRel ) {
142 $srcPaths[] = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
144 $t_start = microtime( true );
145 $fsFiles = $src->getLocalReferenceMulti( array( 'srcs' => $srcPaths, 'latest' => 1 ) );
146 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
147 $this->output( "\nDownloaded these file(s) [{$ellapsed_ms}ms]:\n" .
148 implode( "\n", $srcPaths ) . "\n\n" );
151 // Determine what files need to be copied over...
152 foreach ( $srcPathsRel as $srcPathRel ) {
153 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
154 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
155 if ( $this->hasOption( 'utf8only' ) && !mb_check_encoding( $srcPath, 'UTF-8' ) ) {
156 $this->error( "Detected illegal (non-UTF8) path for $srcPath." );
158 } elseif ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
159 $this->output( "Already have $srcPathRel.\n" );
160 continue; // assume already copied...
162 $fsFile = array_key_exists( $srcPath, $fsFiles )
164 : $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
166 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
167 } elseif ( !$fsFile->exists() ) {
168 // FSFileBackends just return the path for getLocalReference() and paths with
169 // illegal slashes may get normalized to a different path. This can cause the
170 // local reference to not exist...skip these broken files.
171 $this->error( "Detected possible illegal path for $srcPath." );
174 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
175 // Note: prepare() is usually fast for key/value backends
176 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ), 'bypassReadOnly' => 1 ) );
177 if ( !$status->isOK() ) {
178 $this->error( print_r( $status->getErrorsArray(), true ) );
179 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
181 $ops[] = array( 'op' => 'store',
182 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
183 $copiedRel[] = $srcPathRel;
186 // Copy in the batch of source files...
187 $t_start = microtime( true );
188 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
189 if ( !$status->isOK() ) {
190 sleep( 10 ); // wait and retry copy again
191 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
193 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
194 if ( !$status->isOK() ) {
195 $this->error( print_r( $status->getErrorsArray(), true ) );
196 $this->error( "Could not copy file batch.", 1 ); // die
197 } elseif ( count( $copiedRel ) ) {
198 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" .
199 implode( "\n", $copiedRel ) . "\n\n" );
203 protected function filesAreSame( FileBackend
$src, FileBackend
$dst, $sPath, $dPath ) {
204 $skipHash = $this->hasOption( 'skiphash' );
206 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
207 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
208 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
209 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
210 ) && ( $skipHash ||
( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
211 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
217 $maintClass = 'CopyFileBackend';
218 require_once( RUN_MAINTENANCE_IF_MAIN
);