2026-06-04 11:20:35 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @package MokoJoomBackup
|
2026-06-06 14:52:27 -05:00
|
|
|
* @subpackage plg_console_mokojoombackup
|
2026-06-04 11:20:35 -05:00
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
|
2026-06-06 14:52:27 -05:00
|
|
|
namespace Joomla\Plugin\Console\MokoJoomBackup\Command;
|
2026-06-04 11:20:35 -05:00
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
|
|
use Joomla\CMS\Factory;
|
2026-06-06 14:52:27 -05:00
|
|
|
use Joomla\Component\MokoJoomBackup\Administrator\Engine\RestoreEngine;
|
2026-06-04 11:20:35 -05:00
|
|
|
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
|
|
|
|
|
{
|
2026-06-06 14:52:27 -05:00
|
|
|
protected static $defaultName = 'mokojoombackup:restore';
|
2026-06-04 11:20:35 -05:00
|
|
|
|
|
|
|
|
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('*')
|
2026-06-06 14:52:27 -05:00
|
|
|
->from($db->quoteName('#__mokojoombackup_records'))
|
2026-06-04 11:20:35 -05:00
|
|
|
->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;
|
2026-06-04 11:20:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($record->status !== 'complete') {
|
|
|
|
|
$io->error('Cannot restore — backup status is: ' . $record->status);
|
|
|
|
|
|
2026-06-04 20:15:52 -05:00
|
|
|
return 1;
|
2026-06-04 11:20:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2026-06-04 11:20:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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;
|
2026-06-04 11:20:35 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 14:52:27 -05:00
|
|
|
$engineFile = JPATH_ADMINISTRATOR . '/components/com_mokojoombackup/src/Engine/RestoreEngine.php';
|
2026-06-04 11:20:35 -05:00
|
|
|
|
|
|
|
|
if (!file_exists($engineFile)) {
|
|
|
|
|
$io->error('RestoreEngine not found. Is the component fully installed?');
|
|
|
|
|
|
2026-06-04 20:15:52 -05:00
|
|
|
return 1;
|
2026-06-04 11:20:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2026-06-04 11:20:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$io->error($result['message']);
|
|
|
|
|
|
2026-06-04 20:15:52 -05:00
|
|
|
return 1;
|
2026-06-04 11:20:35 -05:00
|
|
|
}
|
|
|
|
|
}
|