Consolidate verifyDumpSucceeded and checksumCheck

This commit is contained in:
Greyscale 2022-09-01 15:25:32 +02:00
parent 24cc4e5ace
commit bf69b76c71
No known key found for this signature in database
GPG key ID: 74BAFF55434DA4B2
3 changed files with 42 additions and 48 deletions

View file

@ -37,6 +37,7 @@ abstract class AbstractSyncer
count($filesInS3),
$showLimit
));
/** @var FileAttributes $file */
foreach (array_slice($filesInS3, 0, $showLimit) as $file) {
$this->logger->debug(sprintf(
@ -143,4 +144,41 @@ abstract class AbstractSyncer
return $uncompressedFile;
}
protected function checksumCheck($dumpFile): void
{
// Checksum dump and don't upload if the checksum is the same as last time.
$hash = sha1_file("/dumps/{$dumpFile}");
if ($this->localFilesystem->has('previous_hash') && $hash == $this->localFilesystem->read('previous_hash')) {
$this->logger->debug(sprintf(
'%s Dump of %s matches previous dump (%s), not uploading the same file again.',
Emoji::abacus(),
$dumpFile,
substr($hash, 0, 7)
));
exit;
}
$this->localFilesystem->write('previous_hash', $hash);
}
protected function verifyDumpSucceeded($dumpFile): void
{
if (!$this->localFilesystem->fileExists($dumpFile)) {
$this->logger->critical('Database dump failed');
exit;
}
if (!$this->localFilesystem->fileSize($dumpFile) > 0) {
$this->logger->critical('Dump file was created, but was empty.');
exit;
}
$this->logger->debug(sprintf(
'Dump file was made, and is %s uncompressed',
ByteSize::formatMetric(
$this->localFilesystem->fileSize($dumpFile)
)
));
}
}

View file

@ -2,7 +2,6 @@
namespace S3DB\Sync;
use Rych\ByteSize\ByteSize;
use Spatie\Emoji\Emoji;
class MysqlSyncer extends AbstractSyncer
@ -15,31 +14,10 @@ class MysqlSyncer extends AbstractSyncer
passthru($command);
// Verify the dump worked
if (!$this->localFilesystem->fileExists($dumpFile)) {
$this->logger->critical('Database dump failed');
exit;
}
$this->logger->debug(sprintf(
'Dump file was made, and is %s uncompressed',
ByteSize::formatMetric(
$this->localFilesystem->fileSize($dumpFile)
)
));
$this->verifyDumpSucceeded($dumpFile);
// Checksum dump and don't upload if the checksum is the same as last time.
$hash = sha1_file("/dumps/{$dumpFile}");
if ($this->localFilesystem->has('previous_hash') && $hash == $this->localFilesystem->read('previous_hash')) {
$this->logger->debug(sprintf(
'%s Dump of %s matches previous dump (%s), not uploading the same file again.',
Emoji::abacus(),
$dumpFile,
substr($hash, 0, 7)
));
exit;
}
$this->localFilesystem->write('previous_hash', $hash);
$this->checksumCheck($dumpFile);
// XZ compress dump
$compressedDumpFile = $this->compress($dumpFile);

View file

@ -2,7 +2,6 @@
namespace S3DB\Sync;
use Rych\ByteSize\ByteSize;
use Spatie\Emoji\Emoji;
class PostgresSyncer extends AbstractSyncer
@ -15,31 +14,10 @@ class PostgresSyncer extends AbstractSyncer
passthru($command);
// Verify the dump worked
if (!$this->localFilesystem->fileExists($dumpFile)) {
$this->logger->critical('Database dump failed');
exit;
}
$this->logger->debug(sprintf(
'Dump file was made, and is %s uncompressed',
ByteSize::formatMetric(
$this->localFilesystem->fileSize($dumpFile)
)
));
$this->verifyDumpSucceeded($dumpFile);
// Checksum dump and don't upload if the checksum is the same as last time.
$hash = sha1_file("/dumps/{$dumpFile}");
if ($this->localFilesystem->has('previous_hash') && $hash == $this->localFilesystem->read('previous_hash')) {
$this->logger->debug(sprintf(
'%s Dump of %s matches previous dump (%s), not uploading the same file again.',
Emoji::abacus(),
$dumpFile,
substr($hash, 0, 7)
));
exit;
}
$this->localFilesystem->write('previous_hash', $hash);
$this->checksumCheck($dumpFile);
// XZ compress dump
$compressedDumpFile = $this->compress($dumpFile);