27require_once __DIR__ .
'/Maintenance.php';
46 parent::__construct();
48 $this->
addOption(
'src',
'Backend containing the source files',
true,
true );
49 $this->
addOption(
'dst',
'Backend where files should be copied to',
true,
true );
50 $this->
addOption(
'containers',
'Pipe separated list of containers',
true,
true );
51 $this->
addOption(
'subdir',
'Only do items in this child directory',
false,
true );
52 $this->
addOption(
'ratefile',
'File to check periodically for batch size',
false,
true );
53 $this->
addOption(
'prestat',
'Stat the destination files first (try to use listings)' );
54 $this->
addOption(
'skiphash',
'Skip SHA-1 sync checks for files' );
55 $this->
addOption(
'missingonly',
'Only copy files missing from destination listing' );
56 $this->
addOption(
'syncviadelete',
'Delete destination files missing from source listing' );
57 $this->
addOption(
'utf8only',
'Skip source files that do not have valid UTF-8 names' );
63 $src = $backendGroup->get( $this->
getOption(
'src' ) );
64 $dst = $backendGroup->get( $this->
getOption(
'dst' ) );
65 $containers = explode(
'|', $this->
getOption(
'containers' ) );
66 $subDir = rtrim( $this->
getOption(
'subdir',
'' ),
'/' );
68 $rateFile = $this->
getOption(
'ratefile' );
70 foreach ( $containers as $container ) {
71 if ( $subDir !=
'' ) {
72 $backendRel =
"$container/$subDir";
73 $this->
output(
"Doing container '$container', directory '$subDir'...\n" );
75 $backendRel = $container;
76 $this->
output(
"Doing container '$container'...\n" );
79 if ( $this->
hasOption(
'missingonly' ) ) {
80 $this->
output(
"\tBuilding list of missing files..." );
82 $this->
output( count( $srcPathsRel ) .
" file(s) need to be copied.\n" );
84 $srcPathsRel = $src->getFileList( [
85 'dir' => $src->getRootStoragePath() .
"/$backendRel",
88 if ( $srcPathsRel ===
null ) {
89 $this->
fatalError(
"Could not list files in $container." );
95 $this->
output(
"\tBuilding destination stat cache..." );
96 $dstPathsRel = $dst->getFileList( [
97 'dir' => $dst->getRootStoragePath() .
"/$backendRel",
100 if ( $dstPathsRel ===
null ) {
101 $this->
fatalError(
"Could not list files in $container." );
103 $this->statCache = [];
104 foreach ( $dstPathsRel as $dstPathRel ) {
105 $path = $dst->getRootStoragePath() .
"/$backendRel/$dstPathRel";
106 $this->statCache[sha1(
$path )] = $dst->getFileStat( [
'src' =>
$path ] );
108 $this->
output(
"done [" . count( $this->statCache ) .
" file(s)]\n" );
111 $this->
output(
"\tCopying file(s)...\n" );
114 foreach ( $srcPathsRel as $srcPathRel ) {
116 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
117 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
118 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
120 $batchPaths[$srcPathRel] = 1;
122 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
127 if ( count( $batchPaths ) ) {
128 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
130 $this->
output(
"\tCopied $count file(s).\n" );
132 if ( $this->
hasOption(
'syncviadelete' ) ) {
133 $this->
output(
"\tBuilding list of excess destination files..." );
135 $this->
output( count( $delPathsRel ) .
" file(s) need to be deleted.\n" );
137 $this->
output(
"\tDeleting file(s)...\n" );
140 foreach ( $delPathsRel as $delPathRel ) {
142 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
143 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
144 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
146 $batchPaths[$delPathRel] = 1;
148 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
153 if ( count( $batchPaths ) ) {
154 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
157 $this->
output(
"\tDeleted $count file(s).\n" );
160 if ( $subDir !=
'' ) {
161 $this->
output(
"Finished container '$container', directory '$subDir'.\n" );
163 $this->
output(
"Finished container '$container'.\n" );
167 $this->
output(
"Done.\n" );
179 if ( $srcPathsRel ===
null ) {
180 $this->
fatalError(
"Could not list files in source container." );
184 if ( $dstPathsRel ===
null ) {
185 $this->
fatalError(
"Could not list files in destination container." );
188 $relFilesDstSha1 = [];
189 foreach ( $dstPathsRel as $dstPathRel ) {
190 $relFilesDstSha1[sha1( $dstPathRel )] = 1;
192 unset( $dstPathsRel );
194 $missingPathsRel = [];
195 foreach ( $srcPathsRel as $srcPathRel ) {
196 if ( !isset( $relFilesDstSha1[sha1( $srcPathRel )] ) ) {
197 $missingPathsRel[] = $srcPathRel;
200 unset( $srcPathsRel );
202 return $missingPathsRel;
221 if ( $this->
hasOption(
'missingonly' ) ) {
223 foreach ( $srcPathsRel as $srcPathRel ) {
226 $t_start = microtime(
true );
228 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
229 $this->
output(
"\n\tDownloaded these file(s) [{$elapsed_ms}ms]:\n\t" .
230 implode(
"\n\t", $srcPaths ) .
"\n\n" );
234 foreach ( $srcPathsRel as $srcPathRel ) {
237 if ( $this->
hasOption(
'utf8only' ) && !mb_check_encoding( $srcPath,
'UTF-8' ) ) {
238 $this->
error(
"$domainId: Detected illegal (non-UTF8) path for $srcPath." );
240 } elseif ( !$this->
hasOption(
'missingonly' )
241 && $this->
filesAreSame( $src, $dst, $srcPath, $dstPath )
243 $this->
output(
"\tAlready have $srcPathRel.\n" );
246 $fsFile = array_key_exists( $srcPath, $fsFiles )
251 if ( $src->
fileExists( [
'src' => $srcPath,
'latest' => 1 ] ) ===
false ) {
252 $this->
error(
"$domainId: File '$srcPath' was listed but does not exist." );
254 $this->
error(
"$domainId: Could not get local copy of $srcPath." );
257 } elseif ( !$fsFile->exists() ) {
261 $this->
error(
"$domainId: Detected possible illegal path for $srcPath." );
264 $fsFiles[] = $fsFile;
266 $status = $dst->
prepare( [
'dir' => dirname( $dstPath ),
'bypassReadOnly' =>
true ] );
267 if ( !$status->isOK() ) {
268 $this->
error( $status );
269 $this->
fatalError(
"$domainId: Could not copy $srcPath to $dstPath." );
271 $ops[] = [
'op' =>
'store',
272 'src' => $fsFile->getPath(),
'dst' => $dstPath,
'overwrite' => true ];
273 $copiedRel[] = $srcPathRel;
277 $t_start = microtime(
true );
279 if ( !$status->isOK() ) {
283 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
284 if ( !$status->isOK() ) {
285 $this->
error( $status );
286 $this->
fatalError(
"$domainId: Could not copy file batch." );
287 } elseif ( count( $copiedRel ) ) {
288 $this->
output(
"\n\tCopied these file(s) [{$elapsed_ms}ms]:\n\t" .
289 implode(
"\n\t", $copiedRel ) .
"\n\n" );
307 foreach ( $dstPathsRel as $dstPathRel ) {
309 $ops[] = [
'op' =>
'delete',
'src' => $dstPath ];
310 $deletedRel[] = $dstPathRel;
314 $t_start = microtime(
true );
316 if ( !$status->isOK() ) {
320 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
321 if ( !$status->isOK() ) {
322 $this->
error( $status );
323 $this->
fatalError(
"$domainId: Could not delete file batch." );
324 } elseif ( count( $deletedRel ) ) {
325 $this->
output(
"\n\tDeleted these file(s) [{$elapsed_ms}ms]:\n\t" .
326 implode(
"\n\t", $deletedRel ) .
"\n\n" );
338 $skipHash = $this->
hasOption(
'skiphash' );
339 $srcStat = $src->
getFileStat( [
'src' => $sPath ] );
340 $dPathSha1 = sha1( $dPath );
341 if ( $this->statCache !==
null ) {
343 $dstStat = $this->statCache[$dPathSha1] ??
false;
345 $dstStat = $dst->
getFileStat( [
'src' => $dPath ] );
350 && is_array( $dstStat )
351 && $srcStat[
'size'] === $dstStat[
'size']
356 } elseif ( isset( $srcStat[
'md5'] ) && isset( $dstStat[
'md5'] ) ) {
360 $same = ( $srcStat[
'md5'] === $dstStat[
'md5'] );
361 } elseif ( $skipHash ) {
367 $same = ( $srcStat[
'mtime'] <= $dstStat[
'mtime'] );
381require_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)
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.
getServiceContainer()
Returns the main service container.
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.