Files
backupscript/src/Rclone/Rclone.php

134 lines
3.3 KiB
PHP
Raw Normal View History

2023-05-26 11:47:40 +00:00
<?php
namespace App\Rclone;
2023-05-31 14:34:35 +00:00
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
2023-05-26 11:47:40 +00:00
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
2023-05-31 14:34:35 +00:00
2023-05-31 09:00:20 +00:00
use Exception;
2023-06-05 09:40:04 +00:00
/**
* Wrapper for the rclone command line utility
*
* Installation of rclone is required.
* Configuration of the mounts must be done before use.
* Tested using rclone v1.53.3-DEV
*/
2023-06-01 09:16:19 +00:00
class Rclone
2023-05-26 12:14:21 +00:00
{
2023-05-31 14:34:35 +00:00
use LoggerAwareTrait;
2023-05-31 09:00:20 +00:00
protected string $rclonePath;
2023-05-26 13:04:15 +00:00
/**
* Global options
*
* @var array<string>
*/
2023-05-31 09:00:20 +00:00
protected array $globalOptions = [];
protected string $version = "";
2023-05-26 11:47:40 +00:00
2023-06-05 09:40:04 +00:00
/**
* Create a new instance
*
* Default it looks for "rclone" on the path.
* But the path can be configured to be absolute.
*
* @param string $rclonePath Relative or absolute path
*/
2023-05-31 09:00:20 +00:00
function __construct(string $rclonePath = "rclone")
2023-05-26 11:47:40 +00:00
{
2023-05-31 09:00:20 +00:00
$this->rclonePath = $rclonePath;
2023-05-31 14:34:35 +00:00
$this->setLogger(new NullLogger);
2023-05-31 09:00:20 +00:00
try
{
$version = $this->exec('--version');
}
catch(ProcessFailedException $e)
{
throw new Exception("Check installation of rclone");
2023-05-31 10:12:52 +00:00
}
$this->version = explode("\n", $version)[0];
if (!\str_contains($this->version, 'rclone')) {
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
/**
* Get the rclone version
*
* @return string Version string
*/
2023-05-26 13:04:15 +00:00
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
/**
* Calculate the size of a mount/path
*
* @return int Size in bytes
*/
2023-05-26 13:04:15 +00:00
function getSize(string $path): int
2023-05-26 11:47:40 +00:00
{
$output = $this->exec('size', ['--json', $path]);
2023-05-26 13:04:15 +00:00
return (int)json_decode($output)->bytes;
2023-05-26 11:47:40 +00:00
}
2023-06-05 09:40:04 +00:00
/**
* Copy from src to dest
*
* @param $src Source mount and path
* @param $dest Destination mount and path
* @param $bandwidth Bandwidth limit provided as string
* @return string Stdout from command
*/
2023-05-26 13:04:15 +00:00
function copy(string $src, string $dest, string $bandwidth = null): string
2023-05-26 11:47:40 +00:00
{
$options = array();
$options[] = $src;
$options[] = $dest;
2023-05-26 12:14:21 +00:00
if ($bandwidth) {
2023-05-26 11:47:40 +00:00
$options[] = "--bwlimit";
$options[] = $bandwidth;
}
return $this->exec('copy', $options);
}
2023-05-26 13:04:15 +00:00
/**
2023-06-01 09:16:19 +00:00
* Execute a command on the rclone binary
*
* @param string $command Top level Rclone command
* @param array<String> $options Array of additional options
*
* @return string stdout data.
2023-05-26 13:04:15 +00:00
*/
protected function exec(string $command, array $options = array()) : string
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],
$this->globalOptions,
2023-05-26 12:14:21 +00:00
[$command],
$options
)
);
2023-05-31 14:34:35 +00:00
$this->logger->info("Execute command", [$process->getCommandLine()]);
2023-05-26 11:47:40 +00:00
$process->setTimeout(4*3600);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
2023-05-31 14:34:35 +00:00
$this->logger->error("Failed execution");
2023-05-26 11:47:40 +00:00
throw new ProcessFailedException($process);
}
2023-05-31 14:34:35 +00:00
$this->logger->info("Return code", [$process->getExitCode()]);
2023-05-26 11:47:40 +00:00
return $process->getOutput();
}
}