70a923262e83f9804009257e070473453fd754ae
[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( dirname( __FILE__ ) . '/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->setBatchSize( 50 );
49 }
50
51 public function execute() {
52 $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) );
53 $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) );
54 $containers = explode( '|', $this->getOption( 'containers' ) );
55 $subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
56
57 $rateFile = $this->getOption( 'ratefile' );
58
59 $count = 0;
60 foreach ( $containers as $container ) {
61 if ( $subDir != '' ) {
62 $backendRel = "$container/$subDir";
63 $this->output( "Doing container '$container', directory '$subDir'...\n" );
64 } else {
65 $backendRel = $container;
66 $this->output( "Doing container '$container'...\n" );
67 }
68
69 $srcPathsRel = $src->getFileList( array(
70 'dir' => $src->getRootStoragePath() . "/$backendRel" ) );
71 if ( $srcPathsRel === null ) {
72 $this->error( "Could not list files in $container.", 1 ); // die
73 }
74
75 // Do a listing comparison if specified
76 if ( $this->hasOption( 'missingonly' ) ) {
77 $relFilesSrc = array();
78 $relFilesDst = array();
79 foreach ( $srcPathsRel as $srcPathRel ) {
80 $relFilesSrc[] = $srcPathRel;
81 }
82 $dstPathsRel = $dst->getFileList( array(
83 'dir' => $dst->getRootStoragePath() . "/$backendRel" ) );
84 if ( $dstPathsRel === null ) {
85 $this->error( "Could not list files in $container.", 1 ); // die
86 }
87 foreach ( $dstPathsRel as $dstPathRel ) {
88 $relFilesDst[] = $dstPathRel;
89 }
90 // Only copy the missing files over in the next loop
91 $srcPathsRel = array_diff( $relFilesSrc, $relFilesDst );
92 $this->output( count( $srcPathsRel ) . " file(s) need to be copied.\n" );
93 unset( $relFilesSrc );
94 unset( $relFilesDst );
95 }
96
97 $batchPaths = array();
98 foreach ( $srcPathsRel as $srcPathRel ) {
99 // Check up on the rate file periodically to adjust the concurrency
100 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
101 $this->mBatchSize = max( 1, (int)file_get_contents( $rateFile ) );
102 $this->output( "Batch size is now {$this->mBatchSize}.\n" );
103 }
104 $batchPaths[$srcPathRel] = 1; // remove duplicates
105 if ( count( $batchPaths ) >= $this->mBatchSize ) {
106 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
107 $batchPaths = array(); // done
108 }
109 ++$count;
110 }
111 if ( count( $batchPaths ) ) { // left-overs
112 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
113 $batchPaths = array(); // done
114 }
115
116 if ( $subDir != '' ) {
117 $this->output( "Finished container '$container', directory '$subDir'.\n" );
118 } else {
119 $this->output( "Finished container '$container'.\n" );
120 }
121 }
122
123 $this->output( "Done [$count file(s)].\n" );
124 }
125
126 protected function copyFileBatch(
127 array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst
128 ) {
129 $ops = array();
130 $fsFiles = array();
131 $copiedRel = array(); // for output message
132 foreach ( $srcPathsRel as $srcPathRel ) {
133 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
134 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
135 if ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
136 $this->output( "Already have $srcPathRel.\n" );
137 continue; // assume already copied...
138 }
139 // Note: getLocalReference() is fast for FS backends
140 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
141 if ( !$fsFile ) {
142 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
143 } elseif ( !$fsFile->exists() ) {
144 // FSFileBackends just return the path for getLocalReference() and paths with
145 // illegal slashes may get normalized to a different path. This can cause the
146 // local reference to not exist...skip these broken files.
147 $this->error( "Detected possible illegal path for $srcPath." );
148 continue;
149 }
150 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
151 // Note: prepare() is usually fast for key/value backends
152 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ), 'bypassReadOnly' => 1 ) );
153 if ( !$status->isOK() ) {
154 $this->error( print_r( $status->getErrorsArray(), true ) );
155 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
156 }
157 $ops[] = array( 'op' => 'store',
158 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
159 $copiedRel[] = $srcPathRel;
160 }
161
162 $t_start = microtime( true );
163 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
164 if ( !$status->isOK() ) {
165 sleep( 10 ); // wait and retry copy again
166 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
167 }
168 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
169 if ( !$status->isOK() ) {
170 $this->error( print_r( $status->getErrorsArray(), true ) );
171 $this->error( "Could not copy file batch.", 1 ); // die
172 } elseif ( count( $copiedRel ) ) {
173 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" .
174 implode( "\n", $copiedRel ) . "\n\n" );
175 }
176 }
177
178 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) {
179 $skipHash = $this->hasOption( 'skiphash' );
180 return (
181 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
182 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
183 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
184 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
185 ) && ( $skipHash || ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
186 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
187 ) )
188 );
189 }
190 }
191
192 $maintClass = 'CopyFileBackend';
193 require_once( RUN_MAINTENANCE_IF_MAIN );