backupscript/src/CommandBackup.php
Jens True 6ac9fd5575
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Quality of life improvements all around.
2023-07-13 11:34:56 +00:00

102 lines
3.4 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\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',
)]
class CommandBackup extends Command
{
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
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$sio = new SymfonyStyle($input, $output);
$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((string)$app->getConfig('rclone.path'));
$rclone->setLogger($app->getLogger()->withName('rclone'));
$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');
/** @var array{title: string, source: string, destination: string} $conf */
foreach ($sio->progressIterate($backupElements) as $conf) {
$title = $conf['title'];
try {
$template = array();
$template['config'] = $conf;
$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) {
$message = $e->getMessage();
$sio->error($message);
}
$notification->send($title, $message);
}
$sio->success("Complete");
return Command::SUCCESS;
}
}