backupscript/src/CommandBackup.php

65 lines
2.3 KiB
PHP
Raw Normal View History

2023-05-26 11:47:40 +00:00
<?php
namespace App;
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\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
class CommandBackup extends Command
{
static $defaultName = "backup";
static $defaultDescription = "Start backup to assigned buckets";
protected function configure(): void
{
$this->addArgument('config', InputArgument::OPTIONAL, 'Configuration file', "config.yml");
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Start backup!');
$output->writeln('Opening: '.$input->getArgument('config'));
try {
$config = Yaml::parseFile($input->getArgument('config'));
} catch (ParseException $exception) {
$output->writeln('Unable to parse the YAML string: '. $exception->getMessage());
}
$rclone = new Rclone\Rclone();
$output->writeln("Rclone version: ". $rclone->getVersion());
$ntfy = new Ntfy\Ntfy($config['notification']['domain']);
$loader = new \Twig\Loader\ArrayLoader($config['templates']);
$twig = new \Twig\Environment($loader);
$twig->addExtension(new Twig\AppExtension());
foreach($config['backup'] as $conf)
{
try {
$template['config'] = $conf;
$template['start'] = new \DateTime();
$template['source_size'] = $rclone->getSize($conf['source']);
$template['destination_size_before'] = $rclone->getSize($conf['destination']);
$rclone->copy($conf['source'], $conf['destination'], "6M");
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
$template['end'] = new \DateTime();
$message = $twig->render('notify', $template);
echo $message;
} catch (\Throwable $e) {
$message = $e->getMessage();
}
$ntfy->send($config['notification']['topic'], $conf['title'], $message);
}
return Command::SUCCESS;
}
}