More codestandards.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-06-12 09:30:10 +00:00
parent d0270b00ca
commit 0f3bbf1f47
8 changed files with 124 additions and 42 deletions

View File

@ -2,6 +2,7 @@
namespace App;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
@ -10,15 +11,15 @@ use Symfony\Component\Console\Style\SymfonyStyle;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use DateTime;
use Ntfy\Server;
use Ntfy\Message;
use Ntfy\Client;
use App\Notification\Notification;
use App\Rclone\Rclone;
#[AsCommand(
name: 'backup',
description: 'Start backup to assigned buckets',
)]
class CommandBackup extends Command
{
public static $defaultName = "backup";
public static $defaultDescription = "Start backup to assigned buckets";
protected function configure(): void
{
$this->addArgument(
@ -41,39 +42,38 @@ class CommandBackup extends Command
return Command::FAILURE;
}
$rclone = new Rclone\Rclone();
$rclone = new Rclone();
$rclone->setLogger($app->getLogger()->withName('rclone'));
$server = new Server($app->getConfig()['notification']['domain']);
$client = new Client($server);
$notification = new Notification();
$notification->loadMany($app->getConfig()['notification']);
$loader = new ArrayLoader($app->getConfig()['templates']);
$twig = new Environment($loader);
$twig->addExtension(new Twig\AppExtension());
foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
$message = new Message();
$message->topic($app->getConfig()['notification']['topic']);
$message->title($conf['title']);
$title = $conf['title'];
$message = "";
try {
$template = array();
$template['config'] = $conf;
$template['start'] = new DateTime();
$template['source_size'] = $rclone->getSize($conf['source']);
$template['rclone_version'] = $rclone->getVersion();
$template['destination_size_before'] = $rclone->getSize($conf['destination']);
$rclone->copy($conf['source'], $conf['destination'], "6M");
$template['stdout'] = $rclone->copy($conf['source'], $conf['destination'], $app->getConfig()['rclone']['options']);
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
$template['end'] = new DateTime();
$message->body($twig->render('notify', $template));
$message = $twig->render('notify', $template);
} catch (\Throwable $e) {
$message->body($e->getMessage());
$message->tags(["warning"]);
$message = $e->getMessage();
}
$client->send($message);
$notification->send($title, $message);
}
$sio->success("Complete");

View File

@ -2,17 +2,19 @@
namespace App;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'show',
description: 'Show all backup entries.',
)]
class CommandShow extends Command
{
public static $defaultName = "show";
public static $defaultDescription = "Show all backup entries";
protected function configure(): void
{
$this->addArgument(

View File

@ -0,0 +1,31 @@
<?php
namespace App\Notification;
class Notification
{
/**
* @type NotificationInterface[] $notifiers
*/
public array $notifiers = array();
public function loadMany(array $config): void
{
foreach ($config as $key => $conf) {
$this->loadSingle($key, $conf);
}
}
public function loadSingle(string $key, array $config): void
{
$class = "\App\Notification\\" . $key;
$this->notifiers[$key] = new $class($config);
}
public function send(string $title, string $message): void
{
foreach ($this->notifiers as $notifier) {
$notifier->send($title, $message);
}
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Notification;
interface NotificationInterface
{
public function send(string $title, string $message): void;
}

32
src/Notification/Ntfy.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace App\Notification;
use Ntfy\Server;
use Ntfy\Message;
use Ntfy\Client;
class Ntfy implements NotificationInterface
{
private array $config;
private \Ntfy\Server $server;
private \Ntfy\Client $client;
public function __construct(array $config)
{
$this->config = $config;
$this->server = new Server($config['domain']);
$this->client = new Client($this->server);
}
public function send(string $title, string $message): void
{
$msg = new Message();
$msg->topic($this->config['topic']);
$msg->title($title);
$msg->body($message);
$this->client->send($msg);
}
}

View File

@ -81,21 +81,28 @@ class Rclone
/**
* Copy from source to destination.
*
* @param $src Source mount and path
* @param $dest Destination mount and path
* @param $bandwidth Bandwidth limit provided as string
* @param $src Source mount and path
* @param $dest Destination mount and path
* @param $additionalOptions strings[] Bandwidth limit provided as string
*
* @return string Stdout from command
*/
public function copy(string $src, string $dest, string $bandwidth = null): string
public function copy(string $src, string $dest, array $additionalOptions = array()): string
{
$options = array();
$options[] = $src;
$options[] = $dest;
if ($bandwidth) {
$options[] = "--bwlimit";
$options[] = $bandwidth;
foreach ($additionalOptions as $key => $value) {
if (strlen($key) == 1) {
$options[] = '-' . $key;
$options[] = $value;
} elseif (strlen($key) > 2) {
$options[] = '--' . $key;
$options[] = $value;
} else {
$options[] = $value;
}
}
return $this->exec('copy', $options);