backupscript/src/CommandBackup.php

83 lines
2.6 KiB
PHP
Raw Normal View History

2023-05-26 11:47:40 +00:00
<?php
2023-06-08 12:44:59 +00:00
2023-05-26 11:47:40 +00:00
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;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use DateTime;
2023-06-07 09:56:23 +00:00
use Ntfy\Server;
use Ntfy\Message;
use Ntfy\Client;
2023-05-26 11:47:40 +00:00
class CommandBackup extends Command
{
2023-06-08 12:44:59 +00:00
public static $defaultName = "backup";
public static $defaultDescription = "Start backup to assigned buckets";
2023-05-26 11:47:40 +00:00
protected function configure(): void
{
2023-06-08 12:44:59 +00:00
$this->addArgument(
'config',
InputArgument::OPTIONAL,
'Configuration file',
"config.yml"
);
2023-05-26 11:47:40 +00:00
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
2023-06-08 12:44:59 +00:00
$sio = new SymfonyStyle($input, $output);
$sio->title('Start backup process');
2023-05-26 11:47:40 +00:00
try {
2023-05-31 14:34:35 +00:00
$app = new App($input->getArgument('config'));
2023-06-08 12:44:59 +00:00
} catch (\Throwable $e) {
$sio->error('Configuration error:' . $e->getMessage());
2023-05-26 13:04:15 +00:00
return Command::FAILURE;
2023-05-26 11:47:40 +00:00
}
2023-06-08 12:44:59 +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'));
2023-06-07 09:56:23 +00:00
$server = new Server($app->getConfig()['notification']['domain']);
$client = new Client($server);
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-06-08 12:44:59 +00:00
foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
2023-06-07 09:56:23 +00:00
$message = new Message();
$message->topic($app->getConfig()['notification']['topic']);
$message->title($conf['title']);
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
2023-06-08 12:44:59 +00:00
$message->body($twig->render('notify', $template));
2023-05-26 11:47:40 +00:00
} catch (\Throwable $e) {
2023-06-07 09:56:23 +00:00
$message->body($e->getMessage());
$message->tags(["warning"]);
2023-05-26 11:47:40 +00:00
}
2023-06-07 09:56:23 +00:00
$client->send($message);
2023-05-26 11:47:40 +00:00
}
2023-06-08 12:44:59 +00:00
$sio->success("Complete");
2023-05-26 11:47:40 +00:00
return Command::SUCCESS;
}
2023-06-08 12:44:59 +00:00
}