Files
MokoSuiteBackup/source/packages/plg_console_mokojoombackup/src/Command/RestoreCommand.php
T

102 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/**
* @package MokoJoomBackup
* @subpackage plg_console_mokojoombackup
* @author Moko Consulting <hello@mokoconsulting.tech>
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE
*/
namespace Joomla\Plugin\Console\MokoJoomBackup\Command;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\Component\MokoJoomBackup\Administrator\Engine\RestoreEngine;
use Joomla\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class RestoreCommand extends AbstractCommand
{
protected static $defaultName = 'mokojoombackup:restore';
protected function configure(): void
{
$this->setDescription('Restore a backup by record ID');
$this->addArgument('id', InputArgument::REQUIRED, 'Backup record ID to restore');
}
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$recordId = (int) $input->getArgument('id');
$io->title('MokoJoomBackup — Restore Backup');
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__mokojoombackup_records'))
->where($db->quoteName('id') . ' = ' . $recordId);
$db->setQuery($query);
$record = $db->loadObject();
if (!$record) {
$io->error('Backup record not found: ' . $recordId);
2026-06-04 20:15:52 -05:00
return 1;
}
if ($record->status !== 'complete') {
$io->error('Cannot restore — backup status is: ' . $record->status);
2026-06-04 20:15:52 -05:00
return 1;
}
if (empty($record->absolute_path) || !is_file($record->absolute_path)) {
$io->error('Backup archive not found: ' . ($record->absolute_path ?: 'no path'));
2026-06-04 20:15:52 -05:00
return 1;
}
$io->warning('This will overwrite the current site files and/or database.');
$io->text('Archive: ' . $record->absolute_path);
$io->text('Type: ' . $record->backup_type);
if (!$io->confirm('Are you sure you want to continue?', false)) {
$io->info('Restore cancelled.');
2026-06-04 20:15:52 -05:00
return 0;
}
$engineFile = JPATH_ADMINISTRATOR . '/components/com_mokojoombackup/src/Engine/RestoreEngine.php';
if (!file_exists($engineFile)) {
$io->error('RestoreEngine not found. Is the component fully installed?');
2026-06-04 20:15:52 -05:00
return 1;
}
if (!class_exists(RestoreEngine::class)) {
require_once $engineFile;
}
$engine = new RestoreEngine();
$result = $engine->restore($record->absolute_path, $record->backup_type);
if ($result['success']) {
$io->success($result['message']);
2026-06-04 20:15:52 -05:00
return 0;
}
$io->error($result['message']);
2026-06-04 20:15:52 -05:00
return 1;
}
}