26require_once __DIR__ .
'/Maintenance.php';
44 parent::__construct();
46 $this->
addOption(
'src',
'Backend containing the source files',
true,
true );
47 $this->
addOption(
'dst',
'Backend where files should be copied to',
true,
true );
48 $this->
addOption(
'containers',
'Pipe separated list of containers',
true,
true );
49 $this->
addOption(
'subdir',
'Only do items in this child directory',
false,
true );
50 $this->
addOption(
'ratefile',
'File to check periodically for batch size',
false,
true );
51 $this->
addOption(
'prestat',
'Stat the destination files first (try to use listings)' );
52 $this->
addOption(
'skiphash',
'Skip SHA-1 sync checks for files' );
53 $this->
addOption(
'missingonly',
'Only copy files missing from destination listing' );
54 $this->
addOption(
'syncviadelete',
'Delete destination files missing from source listing' );
55 $this->
addOption(
'utf8only',
'Skip source files that do not have valid UTF-8 names' );
60 $backendGroup = MediaWikiServices::getInstance()->getFileBackendGroup();
61 $src = $backendGroup->get( $this->
getOption(
'src' ) );
62 $dst = $backendGroup->get( $this->
getOption(
'dst' ) );
63 $containers = explode(
'|', $this->
getOption(
'containers' ) );
64 $subDir = rtrim( $this->
getOption(
'subdir',
'' ),
'/' );
66 $rateFile = $this->
getOption(
'ratefile' );
68 foreach ( $containers as $container ) {
69 if ( $subDir !=
'' ) {
70 $backendRel =
"$container/$subDir";
71 $this->
output(
"Doing container '$container', directory '$subDir'...\n" );
73 $backendRel = $container;
74 $this->
output(
"Doing container '$container'...\n" );
77 if ( $this->
hasOption(
'missingonly' ) ) {
78 $this->
output(
"\tBuilding list of missing files..." );
80 $this->
output( count( $srcPathsRel ) .
" file(s) need to be copied.\n" );
82 $srcPathsRel = $src->getFileList( [
83 'dir' => $src->getRootStoragePath() .
"/$backendRel",
86 if ( $srcPathsRel ===
null ) {
87 $this->
fatalError(
"Could not list files in $container." );
93 $this->
output(
"\tBuilding destination stat cache..." );
94 $dstPathsRel = $dst->getFileList( [
95 'dir' => $dst->getRootStoragePath() .
"/$backendRel",
98 if ( $dstPathsRel ===
null ) {
99 $this->
fatalError(
"Could not list files in $container." );
101 $this->statCache = [];
102 foreach ( $dstPathsRel as $dstPathRel ) {
103 $path = $dst->getRootStoragePath() .
"/$backendRel/$dstPathRel";
104 $this->statCache[sha1(
$path )] = $dst->getFileStat( [
'src' =>
$path ] );
106 $this->
output(
"done [" . count( $this->statCache ) .
" file(s)]\n" );
109 $this->
output(
"\tCopying file(s)...\n" );
112 foreach ( $srcPathsRel as $srcPathRel ) {
114 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
115 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
116 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
118 $batchPaths[$srcPathRel] = 1;
120 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
125 if ( count( $batchPaths ) ) {
126 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
128 $this->
output(
"\tCopied $count file(s).\n" );
130 if ( $this->
hasOption(
'syncviadelete' ) ) {
131 $this->
output(
"\tBuilding list of excess destination files..." );
133 $this->
output( count( $delPathsRel ) .
" file(s) need to be deleted.\n" );
135 $this->
output(
"\tDeleting file(s)...\n" );
138 foreach ( $delPathsRel as $delPathRel ) {
140 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
141 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
142 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
144 $batchPaths[$delPathRel] = 1;
146 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
151 if ( count( $batchPaths ) ) {
152 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
155 $this->
output(
"\tDeleted $count file(s).\n" );
158 if ( $subDir !=
'' ) {
159 $this->
output(
"Finished container '$container', directory '$subDir'.\n" );
161 $this->
output(
"Finished container '$container'.\n" );
165 $this->
output(
"Done.\n" );
177 if ( $srcPathsRel ===
null ) {
178 $this->
fatalError(
"Could not list files in source container." );
182 if ( $dstPathsRel ===
null ) {
183 $this->
fatalError(
"Could not list files in destination container." );
186 $relFilesDstSha1 = [];
187 foreach ( $dstPathsRel as $dstPathRel ) {
188 $relFilesDstSha1[sha1( $dstPathRel )] = 1;
190 unset( $dstPathsRel );
192 $missingPathsRel = [];
193 foreach ( $srcPathsRel as $srcPathRel ) {
194 if ( !isset( $relFilesDstSha1[sha1( $srcPathRel )] ) ) {
195 $missingPathsRel[] = $srcPathRel;
198 unset( $srcPathsRel );
200 return $missingPathsRel;
219 if ( $this->
hasOption(
'missingonly' ) ) {
221 foreach ( $srcPathsRel as $srcPathRel ) {
224 $t_start = microtime(
true );
226 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
227 $this->
output(
"\n\tDownloaded these file(s) [{$elapsed_ms}ms]:\n\t" .
228 implode(
"\n\t", $srcPaths ) .
"\n\n" );
232 foreach ( $srcPathsRel as $srcPathRel ) {
235 if ( $this->
hasOption(
'utf8only' ) && !mb_check_encoding( $srcPath,
'UTF-8' ) ) {
236 $this->
error(
"$domainId: Detected illegal (non-UTF8) path for $srcPath." );
238 } elseif ( !$this->
hasOption(
'missingonly' )
239 && $this->
filesAreSame( $src, $dst, $srcPath, $dstPath )
241 $this->
output(
"\tAlready have $srcPathRel.\n" );
244 $fsFile = array_key_exists( $srcPath, $fsFiles )
249 if ( $src->
fileExists( [
'src' => $srcPath,
'latest' => 1 ] ) ===
false ) {
250 $this->
error(
"$domainId: File '$srcPath' was listed but does not exist." );
252 $this->
error(
"$domainId: Could not get local copy of $srcPath." );
255 } elseif ( !$fsFile->exists() ) {
259 $this->
error(
"$domainId: Detected possible illegal path for $srcPath." );
262 $fsFiles[] = $fsFile;
264 $status = $dst->
prepare( [
'dir' => dirname( $dstPath ),
'bypassReadOnly' =>
true ] );
265 if ( !$status->isOK() ) {
266 $this->
error( Status::wrap( $status )->getMessage(
false,
false,
'en' )->text() );
267 $this->
fatalError(
"$domainId: Could not copy $srcPath to $dstPath." );
269 $ops[] = [
'op' =>
'store',
270 'src' => $fsFile->getPath(),
'dst' => $dstPath,
'overwrite' =>
true ];
271 $copiedRel[] = $srcPathRel;
275 $t_start = microtime(
true );
277 if ( !$status->isOK() ) {
281 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
282 if ( !$status->isOK() ) {
283 $this->
error( Status::wrap( $status )->getMessage(
false,
false,
'en' )->text() );
284 $this->
fatalError(
"$domainId: Could not copy file batch." );
285 } elseif ( count( $copiedRel ) ) {
286 $this->
output(
"\n\tCopied these file(s) [{$elapsed_ms}ms]:\n\t" .
287 implode(
"\n\t", $copiedRel ) .
"\n\n" );
305 foreach ( $dstPathsRel as $dstPathRel ) {
307 $ops[] = [
'op' =>
'delete',
'src' => $dstPath ];
308 $deletedRel[] = $dstPathRel;
312 $t_start = microtime(
true );
314 if ( !$status->isOK() ) {
318 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
319 if ( !$status->isOK() ) {
320 $this->
error( Status::wrap( $status )->getMessage(
false,
false,
'en' )->text() );
321 $this->
fatalError(
"$domainId: Could not delete file batch." );
322 } elseif ( count( $deletedRel ) ) {
323 $this->
output(
"\n\tDeleted these file(s) [{$elapsed_ms}ms]:\n\t" .
324 implode(
"\n\t", $deletedRel ) .
"\n\n" );
336 $skipHash = $this->
hasOption(
'skiphash' );
337 $srcStat = $src->
getFileStat( [
'src' => $sPath ] );
338 $dPathSha1 = sha1( $dPath );
339 if ( $this->statCache !==
null ) {
341 $dstStat = $this->statCache[$dPathSha1] ??
false;
343 $dstStat = $dst->
getFileStat( [
'src' => $dPath ] );
348 && is_array( $dstStat )
349 && $srcStat[
'size'] === $dstStat[
'size']
354 } elseif ( isset( $srcStat[
'md5'] ) && isset( $dstStat[
'md5'] ) ) {
358 $same = ( $srcStat[
'md5'] === $dstStat[
'md5'] );
359 } elseif ( $skipHash ) {
365 $same = ( $srcStat[
'mtime'] <= $dstStat[
'mtime'] );
378require_once RUN_MAINTENANCE_IF_MAIN;
Copy all files in one container of one backend to another.
__construct()
Default constructor.
filesAreSame(FileBackend $src, FileBackend $dst, $sPath, $dPath)
array null $statCache
(path sha1 => stat) Pre-computed dst stat entries from listings
delFileBatch(array $dstPathsRel, $backendRel, FileBackend $dst)
execute()
Do the actual work.
getListingDiffRel(FileBackend $src, FileBackend $dst, $backendRel)
copyFileBatch(array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst)
Base class for all file backend classes (including multi-write backends).
getFileList(array $params)
Get an iterator to list all stored files under a storage directory.
getDomainId()
Get the domain identifier used for this backend (possibly empty).
getFileSha1Base36(array $params)
Get a SHA-1 hash of the content of the file at a storage path in the backend.
clearCache(array $paths=null)
Invalidate any in-process file stat and property cache.
getFileStat(array $params)
Get quick information about a file at a storage path in the backend.
fileExists(array $params)
Check if a file exists at a storage path in the backend.
getRootStoragePath()
Get the root storage path of this backend.
prepare(array $params)
Prepare a storage directory for usage.
doQuickOperations(array $ops, array $opts=[])
Perform a set of independent file operations on some files.
getLocalReferenceMulti(array $params)
Like getLocalReference() except it takes an array of storage paths and yields an order-preserved map ...
getLocalReference(array $params)
Returns a file system file, identical in content to the file at a storage path.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.