548a33b3585b1b3f7289e5d96fe61b30bb181475
[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 }
94
95 $batchPaths = array();
96 foreach ( $srcPathsRel as $srcPathRel ) {
97 // Check up on the rate file periodically to adjust the concurrency
98 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
99 $this->mBatchSize = max( 1, (int)file_get_contents( $rateFile ) );
100 $this->output( "Batch size is now {$this->mBatchSize}.\n" );
101 }
102 $batchPaths[$srcPathRel] = 1; // remove duplicates
103 if ( count( $batchPaths ) >= $this->mBatchSize ) {
104 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
105 $batchPaths = array(); // done
106 }
107 ++$count;
108 }
109 if ( count( $batchPaths ) ) { // left-overs
110 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
111 $batchPaths = array(); // done
112 }
113
114 if ( $subDir != '' ) {
115 $this->output( "Finished container '$container', directory '$subDir'.\n" );
116 } else {
117 $this->output( "Finished container '$container'.\n" );
118 }
119 }
120
121 $this->output( "Done [$count file(s)].\n" );
122 }
123
124 protected function copyFileBatch(
125 array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst
126 ) {
127 $ops = array();
128 $fsFiles = array();
129 $copiedRel = array(); // for output message
130 foreach ( $srcPathsRel as $srcPathRel ) {
131 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
132 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
133 if ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
134 $this->output( "Already have $srcPathRel.\n" );
135 continue; // assume already copied...
136 }
137 // Note: getLocalReference() is fast for FS backends
138 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
139 if ( !$fsFile ) {
140 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
141 }
142 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
143 // Note: prepare() is usually fast for key/value backends
144 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) );
145 if ( !$status->isOK() ) {
146 $this->error( print_r( $status->getErrorsArray(), true ) );
147 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
148 }
149 $ops[] = array( 'op' => 'store',
150 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
151 $copiedRel[] = $srcPathRel;
152 }
153
154 $t_start = microtime( true );
155 $status = $dst->doQuickOperations( $ops );
156 if ( !$status->isOK() ) {
157 sleep( 10 ); // wait and retry copy again
158 $status = $dst->doQuickOperations( $ops );
159 }
160 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
161 if ( !$status->isOK() ) {
162 $this->error( print_r( $status->getErrorsArray(), true ) );
163 $this->error( "Could not copy file batch.", 1 ); // die
164 } elseif ( count( $copiedRel ) ) {
165 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" .
166 implode( "\n", $copiedRel ) . "\n\n" );
167 }
168 }
169
170 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) {
171 $skipHash = $this->hasOption( 'skiphash' );
172 return (
173 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
174 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
175 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
176 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
177 ) && ( $skipHash || ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
178 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
179 ) )
180 );
181 }
182 }
183
184 $maintClass = 'CopyFileBackend';
185 require_once( RUN_MAINTENANCE_IF_MAIN );