3 * Copy all files in some containers of one backend to another.
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.
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.
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.
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
26 * @ingroup Maintenance
29 require_once( dirname( __FILE__
) . '/Maintenance.php' );
31 class CopyFileBackend
extends Maintenance
{
32 public function __construct() {
33 parent
::__construct();
34 $this->mDescription
= "Copy 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( 'subdir', 'Only do items in this child directory', false, true );
39 $this->setBatchSize( 50 );
42 public function execute() {
43 $src = FileBackendGroup
::singleton()->get( $this->getOption( 'src' ) );
44 $dst = FileBackendGroup
::singleton()->get( $this->getOption( 'dst' ) );
45 $containers = explode( '|', $this->getOption( 'containers' ) );
46 $subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
49 foreach ( $containers as $container ) {
50 if ( $subDir != '' ) {
51 $backendRel = "$container/$subDir";
52 $this->output( "Doing container '$container', directory '$subDir'...\n" );
54 $backendRel = $container;
55 $this->output( "Doing container '$container'...\n" );
58 $dir = $src->getRootStoragePath() . "/$backendRel";
59 $srcPathsRel = $src->getFileList( array( 'dir' => $dir ) );
60 if ( $srcPathsRel === null ) {
61 $this->error( "Could not list files in $container.", 1 ); // die
64 $batchPaths = array();
65 foreach ( $srcPathsRel as $srcPathRel ) {
66 $batchPaths[$srcPathRel] = 1; // remove duplicates
67 if ( count( $batchPaths ) >= $this->mBatchSize
) {
68 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
69 $batchPaths = array(); // done
73 if ( count( $batchPaths ) ) { // left-overs
74 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
75 $batchPaths = array(); // done
78 if ( $subDir != '' ) {
79 $this->output( "Finished container '$container', directory '$subDir'.\n" );
81 $this->output( "Finished container '$container'.\n" );
85 $this->output( "Done [$count file(s)].\n" );
88 protected function copyFileBatch(
89 array $srcPathsRel, $backendRel, FileBackend
$src, FileBackend
$dst
93 foreach ( $srcPathsRel as $srcPathRel ) {
94 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
95 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
96 if ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
97 $this->output( "Already have $srcPathRel.\n" );
98 continue; // assume already copied...
100 // Note: getLocalReference() is fast for FS backends
101 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
103 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
105 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
106 // Note: prepare() is usually fast for key/value backends
107 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) );
108 if ( !$status->isOK() ) {
109 $this->error( print_r( $status->getErrorsArray(), true ) );
110 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
112 $ops[] = array( 'op' => 'store',
113 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
116 $status = $dst->doOperations( $ops, array( 'nonJournaled' => 1 ) );
117 if ( !$status->isOK() ) {
118 $this->error( print_r( $status->getErrorsArray(), true ) );
119 $this->error( "Could not copy file batch.", 1 ); // die
121 $this->output( "Copied these file(s):\n" . implode( "\n", $srcPathsRel ) . "\n\n" );
125 protected function filesAreSame( FileBackend
$src, FileBackend
$dst, $sPath, $dPath ) {
127 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
128 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
129 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
130 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
131 ) && ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
132 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
138 $maintClass = 'CopyFileBackend';
139 require_once( RUN_MAINTENANCE_IF_MAIN
);