Merge "Use local context to get messages and don't use implicit Message object to...
[lhc/web/wiklou.git] / maintenance / copyFileBackend.php
1 <?php
2 /**
3 * Copy all files in one container of one backend to another.
4 *
5 * This can also be used to re-shard the files for one backend using the
6 * config of second backend. The second backend should have the same config
7 * as the first, except for it having a different name and different sharding
8 * configuration. The backend should be made read-only while this runs.
9 * After this script finishes, the old files in the containers can be deleted.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @ingroup Maintenance
27 */
28
29 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
30
31 class CopyFileBackend extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Copy all the files in one backend to another.";
35 $this->addOption( 'src', 'Backend containing the source files', true, true );
36 $this->addOption( 'dst', 'Backend where files should be copied to', true, true );
37 $this->addOption( 'containers', 'Pipe separated list of containers', true, true );
38 $this->addOption( 'fast', 'Skip SHA-1 checks on pre-existing files' );
39 }
40
41 public function execute() {
42 $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) );
43 $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) );
44
45 $containers = explode( '|', $this->getOption( 'containers' ) );
46 foreach ( $containers as $container ) {
47 $this->output( "Doing container $container...\n" );
48
49 $srcPathsRel = $src->getFileList(
50 array( 'dir' => $src->getRootStoragePath() . "/$container" ) );
51 if ( $srcPathsRel === null ) {
52 $this->error( "Could not list files in $container.", 1 ); // die
53 }
54 foreach ( $srcPathsRel as $srcPathRel ) {
55 $srcPath = $src->getRootStoragePath() . "/$container/$srcPathRel";
56 $dstPath = $dst->getRootStoragePath() . "/$container/$srcPathRel";
57
58 if ( $dst->fileExists( array( 'src' => $dstPath, 'latest' => 1 ) ) ) {
59 if ( $this->hasOption( 'fast' ) ) {
60 $this->output( "Already have $dstPath.\n" );
61 continue; // assume already copied...
62 }
63 $srcSha1 = $src->getFileSha1Base36( array( 'src' => $srcPath ) );
64 $dstSha1 = $dst->getFileSha1Base36( array( 'src' => $dstPath ) );
65 if ( $srcSha1 && $srcSha1 === $dstSha1 ) {
66 $this->output( "Already have $dstPath.\n" );
67 continue; // already copied...
68 }
69 }
70
71 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
72 if ( !$fsFile ) {
73 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
74 }
75
76 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) );
77 $status->merge( $dst->store(
78 array( 'src' => $fsFile->getPath(), 'dst' => $dstPath ),
79 array( 'nonLocking' => 1, 'nonJournaled' => 1 )
80 ) );
81 if ( !$status->isOK() ) {
82 print_r( $status->getErrorsArray() );
83 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
84 }
85
86 $this->output( "Copied $srcPath to $dstPath.\n" );
87 }
88 }
89 }
90 }
91
92 $maintClass = 'CopyFileBackend';
93 require_once( RUN_MAINTENANCE_IF_MAIN );