Files
backupscript/src/CommandBackup.php

118 lines
3.8 KiB
PHP
Raw Normal View History

2023-05-26 11:47:40 +00:00
<?php
2023-06-08 12:44:59 +00:00
declare(strict_types=1);
2023-05-26 11:47:40 +00:00
namespace App;
2023-06-12 09:30:10 +00:00
use Symfony\Component\Console\Attribute\AsCommand;
2023-05-26 11:47:40 +00:00
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;
2023-05-31 09:00:20 +00:00
use Symfony\Component\Console\Style\SymfonyStyle;
2023-06-15 14:10:17 +00:00
use App\Template\Twig;
2023-06-12 09:30:10 +00:00
use App\Notification\Notification;
use App\Rclone\Rclone;
2023-06-15 14:10:17 +00:00
use DateTime;
2023-06-07 09:56:23 +00:00
2023-06-12 09:30:10 +00:00
#[AsCommand(
name: 'backup',
description: 'Start backup to assigned buckets',
)]
final class CommandBackup extends Command
2023-05-26 11:47:40 +00:00
{
#[\Override]
2023-05-26 11:47:40 +00:00
protected function configure(): void
{
2023-06-08 12:44:59 +00:00
$this->addArgument(
'config',
2023-06-15 10:56:05 +00:00
InputArgument::REQUIRED,
'Configuration file'
2023-06-08 12:44:59 +00:00
);
2023-05-26 11:47:40 +00:00
}
/**
* 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]
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);
$sioProgressbar = &$sio;
if ($output instanceof ConsoleOutputInterface) {
$sio = new SymfonyStyle($input, $output->section());
$sioProgressbar = new SymfonyStyle($input, $output->section());
}
2023-06-08 12:44:59 +00:00
$sio->title('Start backup process');
2023-05-26 11:47:40 +00:00
try {
2023-07-04 08:19:02 +00:00
$app = new App((string)$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
2025-04-01 14:09:39 +00:00
$rclone = new Rclone($app->getLogger()->withName('rclone'), (string)$app->getConfig('rclone.path'), (string)$app->getConfig('rclone.config'));
2023-05-31 14:34:35 +00:00
2023-06-12 09:30:10 +00:00
$notification = new Notification();
2024-02-07 11:00:08 +00:00
/**
* @var array<array-key,array<string,string>>
*/
2023-07-05 15:00:31 +00:00
$notificationConfig = $app->getConfig('notification');
$notification->loadMany($notificationConfig);
2023-05-26 11:47:40 +00:00
2024-02-07 11:00:08 +00:00
/**
* @var array<string,string>
*/
2023-07-05 15:00:31 +00:00
$templateConfig = $app->getConfig('templates');
$render = new Twig($templateConfig);
2023-05-26 11:47:40 +00:00
2024-02-07 11:00:08 +00:00
/**
* @var array{title: string, source: string, destination: string}[]
*/
2023-07-05 15:00:31 +00:00
$backupElements = $app->getConfig('backup');
foreach ($sioProgressbar->progressIterate($backupElements) as $conf) {
2023-06-12 09:30:10 +00:00
$title = $conf['title'];
2024-02-07 11:00:08 +00:00
$template = [];
2023-08-29 09:44:05 +00:00
$template['config'] = $conf;
2023-05-26 11:47:40 +00:00
try {
2023-05-31 09:00:20 +00:00
$template['start'] = new DateTime();
2023-07-05 14:36:56 +00:00
$template['source_size'] = $rclone->getSize($conf['source']);
2023-06-12 09:30:10 +00:00
$template['rclone_version'] = $rclone->getVersion();
2023-07-05 14:36:56 +00:00
$template['destination_size_before'] = $rclone->getSize($conf['destination']);
2023-05-26 11:47:40 +00:00
2024-02-07 11:00:08 +00:00
/**
* @var array<array-key, string>
*/
2023-07-05 14:36:56 +00:00
$rcloneOptions = $app->getConfig('rclone.options');
$rclone->copy($conf['source'], $conf['destination'], $rcloneOptions);
2023-05-26 11:47:40 +00:00
2023-07-05 14:36:56 +00:00
$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-15 14:10:17 +00:00
$message = $render->render('notify', $template);
2023-06-15 10:15:44 +00:00
} catch (\Exception $e) {
2023-08-29 09:44:05 +00:00
$template['exception'] = $e->getMessage();
$message = $render->render('error', $template);
2023-07-10 07:47:28 +00:00
$sio->error($message);
2023-05-26 11:47:40 +00:00
}
2023-07-05 14:36:56 +00:00
$notification->send($title, $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
}