(bug 40829) Show cascading protection info on action=info
[lhc/web/wiklou.git] / maintenance / copyFileBackend.php
1 <?php
2 /**
3 * Copy all files in some containers of one backend to another.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once( __DIR__ . '/Maintenance.php' );
25
26 /**
27 * Copy all files in one container of one backend to another.
28 *
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.
34 *
35 * @ingroup Maintenance
36 */
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 );
50 }
51
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', '/' ), '' );
57
58 $rateFile = $this->getOption( 'ratefile' );
59
60 if ( $this->hasOption( 'utf8only' ) && !extension_loaded( 'mbstring' ) ) {
61 $this->error( "Cannot check for UTF-8, mbstring extension missing.", 1 ); // die
62 }
63
64 $count = 0;
65 foreach ( $containers as $container ) {
66 if ( $subDir != '' ) {
67 $backendRel = "$container/$subDir";
68 $this->output( "Doing container '$container', directory '$subDir'...\n" );
69 } else {
70 $backendRel = $container;
71 $this->output( "Doing container '$container'...\n" );
72 }
73
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
78 }
79
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;
86 }
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
91 }
92 foreach ( $dstPathsRel as $dstPathRel ) {
93 $relFilesDst[] = $dstPathRel;
94 }
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 );
100 }
101
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" );
108 }
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
113 }
114 ++$count;
115 }
116 if ( count( $batchPaths ) ) { // left-overs
117 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
118 $batchPaths = array(); // done
119 }
120
121 if ( $subDir != '' ) {
122 $this->output( "Finished container '$container', directory '$subDir'.\n" );
123 } else {
124 $this->output( "Finished container '$container'.\n" );
125 }
126 }
127
128 $this->output( "Done [$count file(s)].\n" );
129 }
130
131 protected function copyFileBatch(
132 array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst
133 ) {
134 $ops = array();
135 $fsFiles = array();
136 $copiedRel = array(); // for output message
137 foreach ( $srcPathsRel as $srcPathRel ) {
138 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
139 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
140 if ( $this->hasOption( 'utf8only' ) && !mb_check_encoding( $srcPath, 'UTF-8' ) ) {
141 $this->error( "Detected illegal (non-UTF8) path for $srcPath." );
142 continue;
143 } elseif ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
144 $this->output( "Already have $srcPathRel.\n" );
145 continue; // assume already copied...
146 }
147 // Note: getLocalReference() is fast for FS backends
148 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
149 if ( !$fsFile ) {
150 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
151 } elseif ( !$fsFile->exists() ) {
152 // FSFileBackends just return the path for getLocalReference() and paths with
153 // illegal slashes may get normalized to a different path. This can cause the
154 // local reference to not exist...skip these broken files.
155 $this->error( "Detected possible illegal path for $srcPath." );
156 continue;
157 }
158 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
159 // Note: prepare() is usually fast for key/value backends
160 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ), 'bypassReadOnly' => 1 ) );
161 if ( !$status->isOK() ) {
162 $this->error( print_r( $status->getErrorsArray(), true ) );
163 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
164 }
165 $ops[] = array( 'op' => 'store',
166 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
167 $copiedRel[] = $srcPathRel;
168 }
169
170 $t_start = microtime( true );
171 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
172 if ( !$status->isOK() ) {
173 sleep( 10 ); // wait and retry copy again
174 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
175 }
176 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
177 if ( !$status->isOK() ) {
178 $this->error( print_r( $status->getErrorsArray(), true ) );
179 $this->error( "Could not copy file batch.", 1 ); // die
180 } elseif ( count( $copiedRel ) ) {
181 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" .
182 implode( "\n", $copiedRel ) . "\n\n" );
183 }
184 }
185
186 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) {
187 $skipHash = $this->hasOption( 'skiphash' );
188 return (
189 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
190 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
191 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
192 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
193 ) && ( $skipHash || ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
194 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
195 ) )
196 );
197 }
198 }
199
200 $maintClass = 'CopyFileBackend';
201 require_once( RUN_MAINTENANCE_IF_MAIN );