This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
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;
 | 
			
		||||
@@ -13,6 +14,15 @@ use Twig\Loader\ArrayLoader;
 | 
			
		||||
 | 
			
		||||
use DateTime;
 | 
			
		||||
 | 
			
		||||
use Ntfy\Server;
 | 
			
		||||
use Ntfy\Message;
 | 
			
		||||
use Ntfy\Client;
 | 
			
		||||
 | 
			
		||||
#[AsCommand(
 | 
			
		||||
    name: 'backup',
 | 
			
		||||
    description: 'Start backup to assigned buckets.',
 | 
			
		||||
    hidden: false,
 | 
			
		||||
)]
 | 
			
		||||
class CommandBackup extends Command
 | 
			
		||||
{
 | 
			
		||||
    static $defaultName = "backup";
 | 
			
		||||
@@ -40,14 +50,17 @@ class CommandBackup extends Command
 | 
			
		||||
        $rclone = new Rclone\Rclone();
 | 
			
		||||
        $rclone->setLogger($app->getLogger()->withName('rclone'));
 | 
			
		||||
 | 
			
		||||
        $ntfy = new Ntfy\Ntfy($app->getConfig()['notification']['domain']);
 | 
			
		||||
        $ntfy->setLogger($app->getLogger()->withName('notification'));
 | 
			
		||||
        $server = new Server($app->getConfig()['notification']['domain']);
 | 
			
		||||
        $client = new Client($server);
 | 
			
		||||
 | 
			
		||||
        $loader = new ArrayLoader($app->getConfig()['templates']);
 | 
			
		||||
        $twig = new Environment($loader);
 | 
			
		||||
        $twig->addExtension(new Twig\AppExtension());
 | 
			
		||||
 | 
			
		||||
        foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) {   
 | 
			
		||||
        foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) {
 | 
			
		||||
            $message = new Message();
 | 
			
		||||
            $message->topic($app->getConfig()['notification']['topic']);
 | 
			
		||||
            $message->title($conf['title']);
 | 
			
		||||
            try {
 | 
			
		||||
                $template = array();
 | 
			
		||||
                $template['config'] = $conf;
 | 
			
		||||
@@ -60,12 +73,13 @@ class CommandBackup extends Command
 | 
			
		||||
                $template['destination_size_after'] = $rclone->getSize($conf['destination']);
 | 
			
		||||
                $template['end'] = new DateTime();
 | 
			
		||||
 | 
			
		||||
                $message =  $twig->render('notify', $template);
 | 
			
		||||
            
 | 
			
		||||
                $message->body($twig->render('notify', $template));        
 | 
			
		||||
            } catch (\Throwable $e) {
 | 
			
		||||
                $message = $e->getMessage();
 | 
			
		||||
                $message->body($e->getMessage());
 | 
			
		||||
                $message->tags(["warning"]);
 | 
			
		||||
            }
 | 
			
		||||
            $ntfy->send($app->getConfig()['notification']['topic'], $conf['title'], $message);
 | 
			
		||||
 | 
			
		||||
            $client->send($message);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $io->success("Complete");
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +1,19 @@
 | 
			
		||||
<?php
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
use Symfony\Component\Yaml\Yaml;
 | 
			
		||||
use Symfony\Component\Yaml\Exception\ParseException;
 | 
			
		||||
 | 
			
		||||
#[AsCommand(
 | 
			
		||||
    name: 'show',
 | 
			
		||||
    description: 'Show all backup entries.',
 | 
			
		||||
    hidden: false,
 | 
			
		||||
)]
 | 
			
		||||
class CommandShow extends Command
 | 
			
		||||
{
 | 
			
		||||
    static $defaultName = "show";
 | 
			
		||||
 
 | 
			
		||||
@@ -1,32 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace App\Ntfy;
 | 
			
		||||
 | 
			
		||||
use Psr\Log\LoggerAwareTrait;
 | 
			
		||||
 | 
			
		||||
class Ntfy
 | 
			
		||||
{
 | 
			
		||||
    use LoggerAwareTrait;
 | 
			
		||||
    protected string $domain;
 | 
			
		||||
    function __construct(string $domain)
 | 
			
		||||
    {
 | 
			
		||||
        $this->domain = $domain;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function send(string $topic, string $title, string $message): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->logger->debug("Sending ntfy notification", ["topic"=>$topic, "title"=>$title, "message"=>$message]);
 | 
			
		||||
        $result = file_get_contents(
 | 
			
		||||
            'https://'.$this->domain.'/'.$topic, false, stream_context_create(
 | 
			
		||||
                ['http' => [
 | 
			
		||||
                'method' => 'POST', 
 | 
			
		||||
                'header' =>
 | 
			
		||||
                    "Content-Type: text/plain\r\n" .
 | 
			
		||||
                    "Title: $title\r\n",
 | 
			
		||||
                'content' => $message]
 | 
			
		||||
                ]
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
        $this->logger->debug("Result of ntfy notification", [$result]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -3,6 +3,7 @@ namespace App\Rclone;
 | 
			
		||||
 | 
			
		||||
use Psr\Log\LoggerAwareTrait;
 | 
			
		||||
use Psr\Log\NullLogger;
 | 
			
		||||
use Psr\Log\LoggerInterface;
 | 
			
		||||
 | 
			
		||||
use Symfony\Component\Process\Process;
 | 
			
		||||
use Symfony\Component\Process\Exception\ProcessFailedException;
 | 
			
		||||
@@ -45,13 +46,14 @@ class Rclone
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            $version = $this->exec('--version');
 | 
			
		||||
            $this->version = explode("\n", $version)[0];
 | 
			
		||||
        }
 | 
			
		||||
        catch(ProcessFailedException $e)
 | 
			
		||||
        {
 | 
			
		||||
            throw new Exception("Check installation of rclone");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->version = explode("\n", $version)[0];
 | 
			
		||||
       
 | 
			
		||||
        
 | 
			
		||||
        if (!\str_contains($this->version, 'rclone')) {
 | 
			
		||||
            throw new Exception("Rclone not recognized");
 | 
			
		||||
@@ -122,16 +124,23 @@ class Rclone
 | 
			
		||||
                $options
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
        $this->logger->info("Execute command", [$process->getCommandLine()]);
 | 
			
		||||
        if ($this->logger instanceof LoggerInterface) {
 | 
			
		||||
            $this->logger->info("Execute command", [$process->getCommandLine()]);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $process->setTimeout(4*3600);
 | 
			
		||||
        $process->run();
 | 
			
		||||
 | 
			
		||||
        // executes after the command finishes
 | 
			
		||||
        if (!$process->isSuccessful()) {
 | 
			
		||||
            $this->logger->error("Failed execution");
 | 
			
		||||
            if ($this->logger instanceof LoggerInterface) {
 | 
			
		||||
                $this->logger->error("Failed execution");
 | 
			
		||||
            }
 | 
			
		||||
            throw new ProcessFailedException($process);
 | 
			
		||||
        }
 | 
			
		||||
        $this->logger->info("Return code", [$process->getExitCode()]);
 | 
			
		||||
        if ($this->logger instanceof LoggerInterface) {
 | 
			
		||||
            $this->logger->info("Return code", [$process->getExitCode()]);
 | 
			
		||||
        }
 | 
			
		||||
        return $process->getOutput();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -19,8 +19,10 @@ class AppExtension extends AbstractExtension
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Extend the filters
 | 
			
		||||
     * 
 | 
			
		||||
     * 
 | 
			
		||||
     */
 | 
			
		||||
    public function getFilters()
 | 
			
		||||
    public function getFilters() : array
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            new TwigFilter('formatBytes', array($this, 'formatBytes')),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user