24require_once __DIR__ .
'/Maintenance.php';
42 parent::__construct();
44 $this->
addOption(
'src',
'Backend containing the source files',
true,
true );
45 $this->
addOption(
'dst',
'Backend where files should be copied to',
true,
true );
46 $this->
addOption(
'containers',
'Pipe separated list of containers',
true,
true );
47 $this->
addOption(
'subdir',
'Only do items in this child directory',
false,
true );
48 $this->
addOption(
'ratefile',
'File to check periodically for batch size',
false,
true );
49 $this->
addOption(
'prestat',
'Stat the destination files first (try to use listings)' );
50 $this->
addOption(
'skiphash',
'Skip SHA-1 sync checks for files' );
51 $this->
addOption(
'missingonly',
'Only copy files missing from destination listing' );
52 $this->
addOption(
'syncviadelete',
'Delete destination files missing from source listing' );
53 $this->
addOption(
'utf8only',
'Skip source files that do not have valid UTF-8 names' );
60 $containers = explode(
'|', $this->
getOption(
'containers' ) );
61 $subDir = rtrim( $this->
getOption(
'subdir',
'' ),
'/' );
63 $rateFile = $this->
getOption(
'ratefile' );
65 foreach ( $containers as $container ) {
66 if ( $subDir !=
'' ) {
67 $backendRel =
"$container/$subDir";
68 $this->
output(
"Doing container '$container', directory '$subDir'...\n" );
70 $backendRel = $container;
71 $this->
output(
"Doing container '$container'...\n" );
74 if ( $this->
hasOption(
'missingonly' ) ) {
75 $this->
output(
"\tBuilding list of missing files..." );
77 $this->
output( count( $srcPathsRel ) .
" file(s) need to be copied.\n" );
79 $srcPathsRel = $src->getFileList( [
80 'dir' => $src->getRootStoragePath() .
"/$backendRel",
83 if ( $srcPathsRel ===
null ) {
84 $this->
fatalError(
"Could not list files in $container." );
90 $this->
output(
"\tBuilding destination stat cache..." );
91 $dstPathsRel = $dst->getFileList( [
92 'dir' => $dst->getRootStoragePath() .
"/$backendRel",
95 if ( $dstPathsRel ===
null ) {
96 $this->
fatalError(
"Could not list files in $container." );
98 $this->statCache = [];
99 foreach ( $dstPathsRel as $dstPathRel ) {
100 $path = $dst->getRootStoragePath() .
"/$backendRel/$dstPathRel";
101 $this->statCache[sha1(
$path )] = $dst->getFileStat( [
'src' =>
$path ] );
103 $this->
output(
"done [" . count( $this->statCache ) .
" file(s)]\n" );
106 $this->
output(
"\tCopying file(s)...\n" );
109 foreach ( $srcPathsRel as $srcPathRel ) {
111 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
112 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
113 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
115 $batchPaths[$srcPathRel] = 1;
117 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
122 if ( count( $batchPaths ) ) {
123 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
125 $this->
output(
"\tCopied $count file(s).\n" );
127 if ( $this->
hasOption(
'syncviadelete' ) ) {
128 $this->
output(
"\tBuilding list of excess destination files..." );
130 $this->
output( count( $delPathsRel ) .
" file(s) need to be deleted.\n" );
132 $this->
output(
"\tDeleting file(s)...\n" );
135 foreach ( $delPathsRel as $delPathRel ) {
137 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
138 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
139 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
141 $batchPaths[$delPathRel] = 1;
143 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
148 if ( count( $batchPaths ) ) {
149 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
152 $this->
output(
"\tDeleted $count file(s).\n" );
155 if ( $subDir !=
'' ) {
156 $this->
output(
"Finished container '$container', directory '$subDir'.\n" );
158 $this->
output(
"Finished container '$container'.\n" );
162 $this->
output(
"Done.\n" );
174 if ( $srcPathsRel ===
null ) {
175 $this->
fatalError(
"Could not list files in source container." );
179 if ( $dstPathsRel ===
null ) {
180 $this->
fatalError(
"Could not list files in destination container." );
183 $relFilesDstSha1 = [];
184 foreach ( $dstPathsRel as $dstPathRel ) {
185 $relFilesDstSha1[sha1( $dstPathRel )] = 1;
187 unset( $dstPathsRel );
189 $missingPathsRel = [];
190 foreach ( $srcPathsRel as $srcPathRel ) {
191 if ( !isset( $relFilesDstSha1[sha1( $srcPathRel )] ) ) {
192 $missingPathsRel[] = $srcPathRel;
195 unset( $srcPathsRel );
197 return $missingPathsRel;
216 if ( $this->
hasOption(
'missingonly' ) ) {
218 foreach ( $srcPathsRel as $srcPathRel ) {
221 $t_start = microtime(
true );
223 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
224 $this->
output(
"\n\tDownloaded these file(s) [{$elapsed_ms}ms]:\n\t" .
225 implode(
"\n\t", $srcPaths ) .
"\n\n" );
229 foreach ( $srcPathsRel as $srcPathRel ) {
232 if ( $this->
hasOption(
'utf8only' ) && !mb_check_encoding( $srcPath,
'UTF-8' ) ) {
233 $this->
error(
"$domainId: Detected illegal (non-UTF8) path for $srcPath." );
235 } elseif ( !$this->
hasOption(
'missingonly' )
236 && $this->
filesAreSame( $src, $dst, $srcPath, $dstPath )
238 $this->
output(
"\tAlready have $srcPathRel.\n" );
241 $fsFile = array_key_exists( $srcPath, $fsFiles )
246 if ( $src->
fileExists( [
'src' => $srcPath,
'latest' => 1 ] ) ===
false ) {
247 $this->
error(
"$domainId: File '$srcPath' was listed but does not exist." );
249 $this->
error(
"$domainId: Could not get local copy of $srcPath." );
252 } elseif ( !$fsFile->exists() ) {
256 $this->
error(
"$domainId: Detected possible illegal path for $srcPath." );
259 $fsFiles[] = $fsFile;
261 $status = $dst->
prepare( [
'dir' => dirname( $dstPath ),
'bypassReadOnly' => 1 ] );
262 if ( !$status->isOK() ) {
263 $this->
error( Status::wrap( $status )->getMessage(
false,
false,
'en' )->text() );
264 $this->
fatalError(
"$domainId: Could not copy $srcPath to $dstPath." );
266 $ops[] = [
'op' =>
'store',
267 'src' => $fsFile->getPath(),
'dst' => $dstPath,
'overwrite' => 1 ];
268 $copiedRel[] = $srcPathRel;
272 $t_start = microtime(
true );
274 if ( !$status->isOK() ) {
278 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
279 if ( !$status->isOK() ) {
280 $this->
error( Status::wrap( $status )->getMessage(
false,
false,
'en' )->text() );
281 $this->
fatalError(
"$domainId: Could not copy file batch." );
282 } elseif ( count( $copiedRel ) ) {
283 $this->
output(
"\n\tCopied these file(s) [{$elapsed_ms}ms]:\n\t" .
284 implode(
"\n\t", $copiedRel ) .
"\n\n" );
302 foreach ( $dstPathsRel as $dstPathRel ) {
304 $ops[] = [
'op' =>
'delete',
'src' => $dstPath ];
305 $deletedRel[] = $dstPathRel;
309 $t_start = microtime(
true );
311 if ( !$status->isOK() ) {
315 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
316 if ( !$status->isOK() ) {
317 $this->
error( Status::wrap( $status )->getMessage(
false,
false,
'en' )->text() );
318 $this->
fatalError(
"$domainId: Could not delete file batch." );
319 } elseif ( count( $deletedRel ) ) {
320 $this->
output(
"\n\tDeleted these file(s) [{$elapsed_ms}ms]:\n\t" .
321 implode(
"\n\t", $deletedRel ) .
"\n\n" );
333 $skipHash = $this->
hasOption(
'skiphash' );
334 $srcStat = $src->
getFileStat( [
'src' => $sPath ] );
335 $dPathSha1 = sha1( $dPath );
336 if ( $this->statCache !==
null ) {
338 $dstStat = $this->statCache[$dPathSha1] ??
false;
340 $dstStat = $dst->
getFileStat( [
'src' => $dPath ] );
345 && is_array( $dstStat )
346 && $srcStat[
'size'] === $dstStat[
'size']
351 } elseif ( isset( $srcStat[
'md5'] ) && isset( $dstStat[
'md5'] ) ) {
355 $same = ( $srcStat[
'md5'] === $dstStat[
'md5'] );
356 } elseif ( $skipHash ) {
361 $same = ( $srcStat[
'mtime'] <= $dstStat[
'mtime'] );
const 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 exists.
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.
setBatchSize( $s=0)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.