Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Read the [official documentation](https://manual.manticoresearch.com/dev/Securin
| bin/run | This script is used for run the backup script in development and use it for debug purpose |
| build | This directory is ignored by git but built binary goes there |
| src | All sources code goes here |
| src/lib | Library independed components |
| src/lib | Library independent components |
| src/lib/func.php | All helper functions that required to use the script are here |
| src/main.php | This is the main entrypoint for starting the logic |
| test | All tests are here |
Expand All @@ -38,7 +38,7 @@ The directory with name `backup-%date%` is created in the *--backup-dir* folder

## Building

To build the final executable you need to to run `bin/build`. The executable can be found then in the `./build` directory under `build/manticore-backup`.
To build the final executable you need to run `bin/build`. The executable can be found then in the `./build` directory under `build/manticore-backup`.

We recommend using [manticore-executor](https://github.com/manticoresoftware/executor). In this case, the script will use the custom-built PHP binary with all required extensions to run the tool. If you are adding a new functionality which requires a specific PHP module make sure you update [manticore-executor](https://github.com/manticoresoftware/executor) as well.

Expand Down
18 changes: 9 additions & 9 deletions src/Lib/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(?string $backupDir, protected bool $useCompression =
* @return string
* @throws \RuntimeException
*/
public function getBackupDir(): ?string {
public function getBackupDir(): string {
if (!isset($this->backupDir)) {
throw new \RuntimeException('Backup dir is not initialized.');
}
Expand Down Expand Up @@ -177,7 +177,7 @@ protected function copyDir(string $from, string $to): bool {
}

$rootDir = dirname($to);
if (!is_dir($rootDir) || !is_writeable($rootDir)) {
if (!is_dir($rootDir) || !is_writable($rootDir)) {
throw new InvalidPathException('Cannot write to backup directory - "' . $rootDir . '"');
}

Expand All @@ -191,7 +191,7 @@ protected function copyDir(string $from, string $to): bool {

// Create dir if it does not exist
if (!is_dir($destDir)) {
$this->createDir($destDir, $file->getPath(), true);
static::createDir($destDir, $file->getPath(), true);
}

// Skip directories
Expand Down Expand Up @@ -271,7 +271,7 @@ protected function copyFile(string $from, string $to): bool {
* Result of the operation
*/
public function copyPaths(array $paths, string $to, bool $preservePath = false): bool {
if (!is_dir($to) || !is_writeable($to)) {
if (!is_dir($to) || !is_writable($to)) {
throw new InvalidPathException('Cannot write to backup directory - "' . $to . '"');
}

Expand All @@ -285,7 +285,7 @@ public function copyPaths(array $paths, string $to, bool $preservePath = false):
if ($preservePath) {
$dir = is_file($path) ? dirname($dest) : $dest;
if (!is_dir($dir)) {
$this->createDir($dir, dirname($path), true);
static::createDir($dir, dirname($path), true);
}
}
if (is_file($path)) {
Expand Down Expand Up @@ -389,7 +389,7 @@ public static function deleteDir(string $dir, bool $removeSelf = true): void {
}

/**
* Get tmp directory for project related usage primarely in tests
* Get tmp directory for project related usage primarily in tests
* @return string
* The path to the temporary dir that contains only files created by us
*/
Expand All @@ -415,7 +415,7 @@ public function setBackupPathsUsingDir(string $dir): static {
$result = [];
$result['root'] = $destination;

// Now lets create additional directories
// Now let's create additional directories
foreach (['data', 'config', 'state'] as $dir) {
$path = $destination . DIRECTORY_SEPARATOR . $dir;
$result[$dir] = $path;
Expand Down Expand Up @@ -464,7 +464,7 @@ public function getBackupPaths(): array {
$result = [];
$result['root'] = $destination;

// Now lets create additional directories
// Now let's create additional directories
foreach (['data', 'config', 'state'] as $dir) {
$path = $destination . DIRECTORY_SEPARATOR . $dir;
$result[$dir] = $path;
Expand Down Expand Up @@ -516,7 +516,7 @@ public function cleanUp(): void {
return;
}

$this->deleteDir($this->backupPaths['root']);
static::deleteDir($this->backupPaths['root']);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Lib/ManticoreBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected static function store(ManticoreClient $client, FileStorage $storage, a
);

$backupPath = $destination['data'] . DIRECTORY_SEPARATOR . $index;
$storage->createDir(
FileStorage::createDir(
$backupPath,
$config->dataDir . DIRECTORY_SEPARATOR . $index
);
Expand Down Expand Up @@ -234,7 +234,7 @@ protected static function restore(FileStorage $storage): void {

static::validateRestore($storage, $backup['state']);

// Valdiate indexes
// Validate indexes
if (!is_dir($config->dataDir)) {
metric('restore_config_dir_missing', 1);
throw new \Exception('Failed to find data dir, make sure that it exists: ' . $config->dataDir);
Expand Down Expand Up @@ -329,7 +329,7 @@ protected static function restoreData(FileStorage $storage, ManticoreConfig $con
$from = $file->getRealPath();
$to = $config->dataDir . DIRECTORY_SEPARATOR . dirname($storage->getOriginRealPath($file->getRealPath()));
if (!is_dir($to)) {
$storage->createDir($to, $file->getPath(), true);
FileStorage::createDir($to, $file->getPath(), true);
}
println(LogLevel::Debug, ' ' . $from . ' -> ' . $to);

Expand All @@ -350,7 +350,7 @@ protected static function restoreData(FileStorage $storage, ManticoreConfig $con
*/
protected static function storeVersions(array $versions, string $backupDir): bool {
$filePath = $backupDir . DIRECTORY_SEPARATOR . 'versions.json';
return !!file_put_contents($filePath, json_encode($versions));
return (bool)file_put_contents($filePath, json_encode($versions));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/ManticoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function freeze(array|string $tables): array {
}

/**
* This method unfreezes the index we fronzen before
* This method unfreezes the index we frozen before
*
* @param array<string>|string $tables
* Name of index to unfreeze or list of tables
Expand Down
4 changes: 2 additions & 2 deletions src/Lib/ManticoreConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function parseHostPort(string $value): void {
return;
}
$listen = substr($value, 0, $httpPos);
if (false === strpos($listen, ':')) {
if (!str_contains($listen, ':')) {
$this->port = (int)$listen;
} else {
$this->host = strtok($listen, ':');
Expand Down Expand Up @@ -147,7 +147,7 @@ public function getStatePaths(): array {
*/
public static function isDataDirValid(string $dataDir): bool {
return OS::isWindows()
? !!preg_match('|^[a-z]\:\\\\|ius', $dataDir)
? (bool)preg_match('|^[a-z]\:\\\\|ius', $dataDir)
: $dataDir[0] === '/'
;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lib/OS.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public static function isLinux(): bool {
}

/**
* Little helper to find the real path to executoable depending on running os
* Little helper to find the real path to executable depending on running os
*
* @param string $program
* @return string
* The path to found executoable
* The path to found executable
* @throws \Exception
*/
public static function which(string $program): string {
Expand Down
6 changes: 3 additions & 3 deletions src/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function validate_args(array $args): array {
if (!isset($args['unlock'])) {
if (!isset($options['backup-dir'])
|| !is_dir($options['backup-dir'])
|| !is_writeable($options['backup-dir'])) {
|| !is_writable($options['backup-dir'])) {
throw new InvalidArgumentException(
'Failed to find backup dir to store backup: ' . ($options['backup-dir'] ?? 'none')
);
Expand All @@ -61,7 +61,7 @@ function validate_args(array $args): array {
}

/**
* Little helper to conver bytes to human readable size
* Little helper to convert bytes to human readable size
*
* @param int $bytes
* @param int $precision
Expand Down Expand Up @@ -107,7 +107,7 @@ function get_input_args(): array {

foreach ($argv as $arg) {
$arg = strtok($arg, '=');
if (false === strpos($supportedArgs, '!' . $arg . '!')) {
if (!str_contains($supportedArgs, '!' . $arg . '!')) {
throw new InvalidArgumentException('Unknown option: ' . $arg);
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/FileStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testDirCreated(): void {
$storage = new FileStorage($tmpDir, false);

$this->assertDirectoryDoesNotExist($dir);
$storage->createDir($dir);
FileStorage::createDir($dir);
$this->assertDirectoryExists($dir);
}

Expand All @@ -33,12 +33,12 @@ public function testCopyPathsWithoutOwnership(): void {
];
$target = $tmpDir . DIRECTORY_SEPARATOR . 'target-path-' . uniqid();
$storage = new FileStorage($tmpDir, false);
$storage->createDir($target);
FileStorage::createDir($target);

$this->expectException(InvalidPathException::class);
$storage->copyPaths($paths, $target);

$storage->createDir($paths[0]);
FileStorage::createDir($paths[0]);
$this->expectException(InvalidPathException::class);
$storage->copyPaths($paths, $target);

Expand Down
4 changes: 2 additions & 2 deletions test/ManticoreBackupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testStoreAllTables(): void {
public function testStoreAllTablesToSymlinkPath(): void {
[$config, $storage, $backupDir] = $this->initTestEnv();
$uniq = uniqid();
$tmpDir = $storage->getTmpDir();
$tmpDir = $storage::getTmpDir();
$baseDir = basename($backupDir);
$realPath = "$tmpDir-$uniq/first/second/$baseDir";
mkdir($realPath, 0755, true);
Expand Down Expand Up @@ -216,7 +216,7 @@ public function initTestEnv(): array {
* @param array<string,string> $tables
* @return void
*/
protected function assertBackupIsOK(ManticoreClient $client, string $backupDir, array $tables) {
protected function assertBackupIsOK(ManticoreClient $client, string $backupDir, array $tables): void {
$dirs = glob($backupDir . DIRECTORY_SEPARATOR . '*');
$this->assertIsArray($dirs);

Expand Down
2 changes: 1 addition & 1 deletion test/ScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testRestoreArg(): void {
}

/**
* Helper function to validate that shell comand executed and return output
* Helper function to validate that shell command executed and return output
*
* @param string $arg
* @return string
Expand Down