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;
|
2023-05-31 09:00:20 +00:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2023-05-31 14:34:35 +00:00
|
|
|
|
2023-05-26 11:47:40 +00:00
|
|
|
|
2023-05-31 09:00:20 +00:00
|
|
|
use Twig\Environment;
|
|
|
|
use Twig\Loader\ArrayLoader;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
|
2023-05-26 11:47:40 +00:00
|
|
|
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
|
|
|
|
{
|
2023-05-31 09:00:20 +00:00
|
|
|
$io = new SymfonyStyle($input, $output);
|
|
|
|
$io->title('Start backup process');
|
2023-05-31 14:34:35 +00:00
|
|
|
|
2023-05-26 11:47:40 +00:00
|
|
|
try {
|
2023-05-31 14:34:35 +00:00
|
|
|
$app = new App($input->getArgument('config'));
|
|
|
|
}
|
|
|
|
catch (\Throwable $e) {
|
2023-05-31 09:00:20 +00:00
|
|
|
$io->error('Unable to parse the YAML string: '. $e->getMessage());
|
2023-05-26 13:04:15 +00:00
|
|
|
return Command::FAILURE;
|
2023-05-26 11:47:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:34:35 +00:00
|
|
|
|
2023-05-26 11:47:40 +00:00
|
|
|
$rclone = new Rclone\Rclone();
|
2023-05-31 14:34:35 +00:00
|
|
|
$rclone->setLogger($app->getLogger()->withName('rclone'));
|
|
|
|
|
|
|
|
$ntfy = new Ntfy\Ntfy($app->getConfig()['notification']['domain']);
|
2023-06-01 09:16:19 +00:00
|
|
|
$ntfy->setLogger($app->getLogger()->withName('notification'));
|
2023-05-26 11:47:40 +00:00
|
|
|
|
2023-05-31 14:34:35 +00:00
|
|
|
$loader = new ArrayLoader($app->getConfig()['templates']);
|
2023-05-31 09:00:20 +00:00
|
|
|
$twig = new Environment($loader);
|
2023-05-26 11:47:40 +00:00
|
|
|
$twig->addExtension(new Twig\AppExtension());
|
|
|
|
|
2023-05-31 14:34:35 +00:00
|
|
|
foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) {
|
2023-05-26 11:47:40 +00:00
|
|
|
try {
|
2023-05-31 09:00:20 +00:00
|
|
|
$template = array();
|
2023-05-26 11:47:40 +00:00
|
|
|
$template['config'] = $conf;
|
2023-05-31 09:00:20 +00:00
|
|
|
$template['start'] = new DateTime();
|
2023-05-26 11:47:40 +00:00
|
|
|
$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']);
|
2023-05-31 09:00:20 +00:00
|
|
|
$template['end'] = new DateTime();
|
2023-05-26 11:47:40 +00:00
|
|
|
|
|
|
|
$message = $twig->render('notify', $template);
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$message = $e->getMessage();
|
|
|
|
}
|
2023-05-31 14:34:35 +00:00
|
|
|
$ntfy->send($app->getConfig()['notification']['topic'], $conf['title'], $message);
|
2023-05-26 11:47:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 09:00:20 +00:00
|
|
|
$io->success("Complete");
|
2023-05-26 11:47:40 +00:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|