Files
backupscript/src/Rclone/Rclone.php

71 lines
1.6 KiB
PHP
Raw Normal View History

2023-05-26 11:47:40 +00:00
<?php
namespace App\Rclone;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
2023-05-26 12:14:21 +00:00
class Rclone
{
2023-05-26 13:04:15 +00:00
protected string $rclone_path;
/**
* Global options
*
* @var array<string>
*/
protected array $global_options = [];
2023-05-26 11:47:40 +00:00
2023-05-26 13:04:15 +00:00
function __construct(string $rclone_path = "rclone")
2023-05-26 11:47:40 +00:00
{
$this->rclone_path = $rclone_path;
}
2023-05-26 13:04:15 +00:00
function getVersion(): string
2023-05-26 11:47:40 +00:00
{
return $this->exec('--version');
}
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-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
/**
* @param $command Top level Rclone command
* @param array<String> $options
*/
protected function exec(string $command, array $options = array()) : string
2023-05-26 11:47:40 +00:00
{
2023-05-26 12:14:21 +00:00
$process = new Process(
array_merge(
[$this->rclone_path],
$this->global_options,
[$command],
$options
)
);
2023-05-26 11:47:40 +00:00
$process->setTimeout(4*3600);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
}