Files
backupscript/src/Rclone/Rclone.php

133 lines
3.4 KiB
PHP
Raw Normal View History

2023-05-26 11:47:40 +00:00
<?php
2023-06-08 12:44:59 +00:00
declare(strict_types=1);
2023-05-26 11:47:40 +00:00
namespace App\Rclone;
2023-06-07 09:56:23 +00:00
use Psr\Log\LoggerInterface;
2023-05-26 11:47:40 +00:00
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
2023-05-31 09:00:20 +00:00
use Exception;
2023-06-08 12:44:59 +00:00
2023-06-05 09:40:04 +00:00
/**
* Wrapper for the rclone command line utility
2023-06-08 12:44:59 +00:00
*
2023-06-05 09:40:04 +00:00
* Installation of rclone is required.
* Configuration of the mounts must be done before use.
2023-07-28 12:12:47 +00:00
* Tested using rclone v1.62.2
2023-06-05 09:40:04 +00:00
*/
2023-06-01 09:16:19 +00:00
class Rclone
2023-05-26 12:14:21 +00:00
{
2023-05-31 09:00:20 +00:00
protected string $version = "";
2023-05-26 11:47:40 +00:00
2023-06-05 09:40:04 +00:00
/**
2023-06-08 12:44:59 +00:00
* Create a new instance.
*
2023-06-05 09:40:04 +00:00
* Default it looks for "rclone" on the path.
* But the path can be configured to be absolute.
2023-06-08 12:44:59 +00:00
*
2023-06-05 09:40:04 +00:00
* @param string $rclonePath Relative or absolute path
*/
2023-08-15 11:43:12 +00:00
public function __construct(protected LoggerInterface $logger, protected string $rclonePath = "rclone")
2023-05-26 11:47:40 +00:00
{
2023-05-31 09:00:20 +00:00
$this->rclonePath = $rclonePath;
2023-06-08 12:44:59 +00:00
2023-06-15 10:15:44 +00:00
$process = $this->exec('--version');
if (!$process->isSuccessful()) {
2023-05-31 09:00:20 +00:00
throw new Exception("Check installation of rclone");
2023-05-31 10:12:52 +00:00
}
2023-06-15 10:15:44 +00:00
$this->version = explode("\n", $process->getOutput())[0];
2023-05-31 10:12:52 +00:00
if (!\str_contains($this->version, 'rclone')) {
2023-07-25 06:35:51 +00:00
throw new Exception("rclone not recognized");
2023-05-31 09:00:20 +00:00
}
2023-05-26 11:47:40 +00:00
}
2023-06-05 09:40:04 +00:00
/**
2023-06-08 12:44:59 +00:00
* Get the rclone version.
*
2023-06-05 09:40:04 +00:00
* @return string Version string
*/
2023-06-08 12:44:59 +00:00
public function getVersion(): string
2023-05-26 11:47:40 +00:00
{
2023-05-31 09:00:20 +00:00
return $this->version;
2023-05-26 11:47:40 +00:00
}
2023-06-05 09:40:04 +00:00
/**
2023-06-08 12:44:59 +00:00
* Calculate the size of a mount/path.
*
2023-06-05 09:43:50 +00:00
* @param string $path mount/path.
2023-06-08 12:44:59 +00:00
*
2023-06-05 09:40:04 +00:00
* @return int Size in bytes
*/
2023-06-08 12:44:59 +00:00
public function getSize(string $path): int
2023-05-26 11:47:40 +00:00
{
2023-06-15 10:15:44 +00:00
$process = $this->exec('size', ['--json', $path]);
if (!$process->isSuccessful()) {
throw new Exception($process->getErrorOutput());
}
2023-07-05 15:00:31 +00:00
/** @var array{bytes: int} */
$output = json_decode($process->getOutput(), true);
return $output['bytes'];
2023-05-26 11:47:40 +00:00
}
2023-06-05 09:40:04 +00:00
/**
2023-06-08 12:44:59 +00:00
* Copy from source to destination.
*
2023-06-13 08:15:29 +00:00
* @param $src Source mount and path
* @param $dest Destination mount and path
* @param string[] $additionalOptions Additional options
2023-06-05 09:40:04 +00:00
*/
2023-06-15 10:15:44 +00:00
public function copy(string $src, string $dest, array $additionalOptions = array()): void
2023-06-08 12:44:59 +00:00
{
2023-05-26 11:47:40 +00:00
$options = array();
$options[] = $src;
$options[] = $dest;
2023-06-13 08:15:29 +00:00
2023-06-12 09:30:10 +00:00
foreach ($additionalOptions as $key => $value) {
2023-07-28 12:12:47 +00:00
$options[] = '--' . (string)$key;
2023-06-15 10:15:44 +00:00
$options[] = $value;
2023-05-26 11:47:40 +00:00
}
2023-06-15 10:15:44 +00:00
$process = $this->exec('copy', $options);
if (!$process->isSuccessful()) {
throw new Exception($process->getErrorOutput());
}
2023-05-26 11:47:40 +00:00
}
2023-05-26 13:04:15 +00:00
/**
2023-06-08 12:44:59 +00:00
* Execute a command on the rclone binary.
*
2023-06-01 09:16:19 +00:00
* @param string $command Top level Rclone command
* @param array<String> $options Array of additional options
2023-06-08 12:44:59 +00:00
*
2023-06-15 10:15:44 +00:00
* @return Process Instance.
2023-05-26 13:04:15 +00:00
*/
2023-06-15 10:15:44 +00:00
protected function exec(string $command, array $options = array()): Process
2023-05-31 14:34:35 +00:00
{
2023-05-26 12:14:21 +00:00
$process = new Process(
array_merge(
2023-05-31 09:00:20 +00:00
[$this->rclonePath],
2023-05-26 12:14:21 +00:00
[$command],
$options
)
);
2023-07-24 13:32:21 +00:00
$this->logger->info("Execute command", [$process->getCommandLine()]);
2023-06-08 12:44:59 +00:00
$process->setTimeout(4 * 3600);
2023-05-26 11:47:40 +00:00
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
2023-07-24 13:32:21 +00:00
$this->logger->error("Failed execution");
2023-06-07 09:56:23 +00:00
}
2023-07-24 13:32:21 +00:00
$this->logger->info("Return code", [$process->getExitCode()]);
2023-06-15 10:15:44 +00:00
return $process;
2023-05-26 11:47:40 +00:00
}
2023-06-08 12:44:59 +00:00
}