Files
backupscript/src/CommandBackup.php
Jens True 3f33729b4b
Some checks failed
ci/woodpecker/push/docker Pipeline was successful
ci/woodpecker/push/test Pipeline failed
Push update.
2025-04-01 14:09:39 +00:00

118 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
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\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use App\Template\Twig;
use App\Notification\Notification;
use App\Rclone\Rclone;
use DateTime;
#[AsCommand(
name: 'backup',
description: 'Start backup to assigned buckets',
)]
final class CommandBackup extends Command
{
#[\Override]
protected function configure(): void
{
$this->addArgument(
'config',
InputArgument::REQUIRED,
'Configuration file'
);
}
/**
* Start the backup process.
*
* 1. Read the configuration file.
* 2. For each configured backup entry
* 1. Get the size of the source
* 2. Get the size of the destination
* 3. Perform the backup
* 4. Get the new size of the destination
* 5. Send push notifications.
* 3. Report final success
*/
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$sio = new SymfonyStyle($input, $output);
$sioProgressbar = &$sio;
if ($output instanceof ConsoleOutputInterface) {
$sio = new SymfonyStyle($input, $output->section());
$sioProgressbar = new SymfonyStyle($input, $output->section());
}
$sio->title('Start backup process');
try {
$app = new App((string)$input->getArgument('config'));
} catch (\Throwable $e) {
$sio->error('Configuration error:' . $e->getMessage());
return Command::FAILURE;
}
$rclone = new Rclone($app->getLogger()->withName('rclone'), (string)$app->getConfig('rclone.path'), (string)$app->getConfig('rclone.config'));
$notification = new Notification();
/**
* @var array<array-key,array<string,string>>
*/
$notificationConfig = $app->getConfig('notification');
$notification->loadMany($notificationConfig);
/**
* @var array<string,string>
*/
$templateConfig = $app->getConfig('templates');
$render = new Twig($templateConfig);
/**
* @var array{title: string, source: string, destination: string}[]
*/
$backupElements = $app->getConfig('backup');
foreach ($sioProgressbar->progressIterate($backupElements) as $conf) {
$title = $conf['title'];
$template = [];
$template['config'] = $conf;
try {
$template['start'] = new DateTime();
$template['source_size'] = $rclone->getSize($conf['source']);
$template['rclone_version'] = $rclone->getVersion();
$template['destination_size_before'] = $rclone->getSize($conf['destination']);
/**
* @var array<array-key, string>
*/
$rcloneOptions = $app->getConfig('rclone.options');
$rclone->copy($conf['source'], $conf['destination'], $rcloneOptions);
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
$template['end'] = new DateTime();
$message = $render->render('notify', $template);
} catch (\Exception $e) {
$template['exception'] = $e->getMessage();
$message = $render->render('error', $template);
$sio->error($message);
}
$notification->send($title, $message);
}
$sio->success("Complete");
return Command::SUCCESS;
}
}