Files
backupscript/src/Rclone/Rclone.php

56 lines
1.3 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;
class Rclone {
private $rclone_path;
private $global_options = [];
function __construct($rclone_path = "rclone")
{
$this->rclone_path = $rclone_path;
}
function getVersion()
{
return $this->exec('--version');
}
function getSize($path)
{
$output = $this->exec('size', ['--json', $path]);
return (int)json_decode($output[0])->bytes;
}
function copy($src, $dest, $bandwidth = null)
{
$options = array();
$options[] = $src;
$options[] = $dest;
if($bandwidth)
{
$options[] = "--bwlimit";
$options[] = $bandwidth;
}
return $this->exec('copy', $options);
}
private function exec(string $command, array $options = array())
{
$process = new Process(array_merge([$this->rclone_path], $this->global_options, [$command], $options));
$process->setTimeout(4*3600);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
}