Skip to content

Commit

Permalink
Create backup command
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Oct 17, 2024
1 parent 4e1a419 commit 03af9b4
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 58 deletions.
20 changes: 10 additions & 10 deletions .github/action_upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ if [[ $CHANNEL == "local" ]]; then
fi
fi

SKIP_BACKUP=$(docker exec -u www-data prestashop_autoupgrade ls admin-dev/autoupgrade/backup/ | wc -l)
if [[ "$SKIP_BACKUP" > 1 ]]; then
SKIP_BACKUP=1
else
SKIP_BACKUP=0
fi

docker exec -u www-data prestashop_autoupgrade mkdir admin-dev/autoupgrade/download
docker exec -u www-data prestashop_autoupgrade curl -L $ARCHIVE_URL -o admin-dev/autoupgrade/download/prestashop.zip
docker exec -u www-data prestashop_autoupgrade curl -L $XML_URL -o modules/autoupgrade/download/prestashop.xml
echo "{\"channel\":\"local\",\"archive_prestashop\":\"prestashop.zip\",\"archive_num\":\"${VERSION}\", \"archive_xml\":\"prestashop.xml\", \"PS_AUTOUP_CHANGE_DEFAULT_THEME\":${UPDATE_THEME}, \"skip_backup\": ${SKIP_BACKUP}}" > modules/autoupgrade/config.json
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="compareReleases" --config-file-path="modules/autoupgrade/config.json" admin-dev

FILE_COUNT=$(docker exec -u www-data prestashop_autoupgrade ls admin-dev/autoupgrade/backup/ | wc -l)
if [[ "$FILE_COUNT" == 0 ]]; then
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console backup:create admin-dev
fi

echo "{\"channel\":\"local\",\"archive_prestashop\":\"prestashop.zip\",\"archive_num\":\"${VERSION}\", \"archive_xml\":\"prestashop.xml\", \"PS_AUTOUP_CHANGE_DEFAULT_THEME\":${UPDATE_THEME}" > modules/autoupgrade/config.json
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="CompareReleases" --config-file-path="modules/autoupgrade/config.json" admin-dev
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --config-file-path="modules/autoupgrade/config.json" admin-dev
fi

docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="compareReleases" admin-dev
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console backup:create admin-dev
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="CompareReleases" admin-dev
docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start admin-dev
6 changes: 5 additions & 1 deletion .github/workflows/ui-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ jobs:
run: |
docker exec -t prestashop php -v bin/console prestashop:module install autoupgrade
- name: Upgrade
- name: Backup
run: |
docker exec -t prestashop php modules/autoupgrade/bin/console backup:create admin-dev
- name: Update
run: |
docker exec -t prestashop php modules/autoupgrade/bin/console update:start --channel=${{ matrix.UPGRADE_CHANNEL }} admin-dev
docker exec -t prestashop chmod 777 -R /var/www/html/var/cache
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ jobs:
matrix:
from: ['1.7.0.6', '1.7.6.9', '1.7.6.1', '1.7.7.0', '8.0.0', '8.1.0']
ps-versions:
- channel: minor
- channel: major
- channel: online
runs-on: ubuntu-latest
name: Upgrade
steps:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ The requirements can be reviewed to confirm the shop is safe to update:
$ php bin/console update:check <your-admin-dir>
```

A backup of the shop is created with:

```
$ php bin/console backup:create --config-file-path=[/path/to/config.json] <your-admin-dir>
```

The update process can be launched with:

```
Expand Down
2 changes: 2 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

use PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand;
use PrestaShop\Module\AutoUpgrade\Commands\CreateBackupCommand;
use PrestaShop\Module\AutoUpgrade\Commands\RestoreCommand;
use PrestaShop\Module\AutoUpgrade\Commands\UpdateCommand;
use Symfony\Component\Console\Application;
Expand All @@ -46,6 +47,7 @@ $application = new Application();
$application->add(new UpdateCommand());
$application->add(new RestoreCommand());
$application->add(new CheckRequirementsCommand());
$application->add(new CreateBackupCommand());

try {
$application->run();
Expand Down
72 changes: 72 additions & 0 deletions classes/Commands/CreateBackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Commands;

use Exception;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllBackupTasks;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CreateBackupCommand extends AbstractCommand
{
/** @var string */
protected static $defaultName = 'backup:create';

protected function configure(): void
{
$this
->setDescription('Create backup.')
->setHelp('This command triggers the creation of the files and database backup.')
->addOption('config-file-path', null, InputOption::VALUE_REQUIRED, 'Configuration file location.')
->addArgument('admin-dir', InputArgument::REQUIRED, 'The admin directory name.');
}

/**
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
try {
$this->setupContainer($input, $output);

$controller = new AllBackupTasks($this->upgradeContainer);
$controller->init();
$exitCode = $controller->run();
$this->logger->debug('Process completed with exit code: ' . $exitCode);

return $exitCode;
} catch (Exception $e) {
$this->logger->error('An error occurred during the backup process: ' . $e->getMessage());

return ExitCode::FAIL;
}
}
}
12 changes: 4 additions & 8 deletions classes/Progress/CompletionCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupFiles;
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupInitialization;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\Restore;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreComplete;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Restore\Restore;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreComplete;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Update\CleanDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Update\Download;
use PrestaShop\Module\AutoUpgrade\Task\Update\Unzip;
Expand All @@ -47,10 +47,6 @@

class CompletionCalculator
{
public function __construct()
{
}

/**
* The key baseWithoutBackup exists while the backup and upgrade are on the same workflow
*
Expand Down
18 changes: 3 additions & 15 deletions classes/Task/Backup/BackupDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

use Exception;
use PDO;
use PrestaShop\Module\AutoUpgrade\Analytics;
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
Expand All @@ -51,15 +50,6 @@ class BackupDatabase extends AbstractTask
*/
public function run(): int
{
if (!$this->container->getUpgradeConfiguration()->shouldBackupFilesAndDatabase()) {
$this->stepDone = true;
$this->container->getState()->setDbStep(0);
$this->logger->info($this->translator->trans('Database backup skipped. Now upgrading files...'));
$this->next = 'upgradeFiles';

return ExitCode::SUCCESS;
}

$this->stepDone = false;
$this->next = TaskName::TASK_BACKUP_DATABASE;
$start_time = time();
Expand Down Expand Up @@ -248,7 +238,7 @@ public function run(): int
}

if ($tablesToBackup->getRemainingTotal()) {
$this->next = 'backupDb';
$this->next = TaskName::TASK_BACKUP_DATABASE;
$this->stepDone = false;
if ($numberOfSyncedTables) {
$this->logger->info($this->translator->trans('Database backup: %s table(s) left...', [$tablesToBackup->getRemainingTotal() + !empty($row)]));
Expand All @@ -265,10 +255,8 @@ public function run(): int
// reset dbStep at the end of this step
$this->container->getState()->setDbStep(0);

$this->logger->info($this->translator->trans('Database backup done in filename %s. Now upgrading files...', [$this->container->getState()->getBackupName()]));
$this->next = 'upgradeFiles';

$this->container->getAnalytics()->track('Backup Succeeded', Analytics::WITH_BACKUP_PROPERTIES);
$this->logger->info($this->translator->trans('Database backup done in filename %s.', [$this->container->getState()->getBackupName()]));
$this->next = TaskName::TASK_BACKUP_COMPLETE;

return ExitCode::SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions classes/Task/Restore/Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Task\Rollback;
namespace PrestaShop\Module\AutoUpgrade\Task\Restore;

use Exception;
use PrestaShop\Module\AutoUpgrade\Analytics;
Expand Down Expand Up @@ -59,7 +59,7 @@ public function run(): int
$restoreDbFilenames = $this->container->getState()->getRestoreDbFilenames();

if (empty($restoreName)) {
$this->next = TaskName::TASK_NO_RESTORE_FOUND;
$this->next = TaskName::TASK_RESTORE_EMPTY;

return ExitCode::SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Restore/RestoreComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Task\Rollback;
namespace PrestaShop\Module\AutoUpgrade\Task\Restore;

use PrestaShop\Module\AutoUpgrade\Analytics;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Restore/RestoreDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Task\Rollback;
namespace PrestaShop\Module\AutoUpgrade\Task\Restore;

use Exception;
use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Task\Rollback;
namespace PrestaShop\Module\AutoUpgrade\Task\Restore;

use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;

class NoRestoreFound extends AbstractTask
class RestoreEmpty extends AbstractTask
{
const TASK_TYPE = TaskType::TASK_TYPE_RESTORE;

Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Restore/RestoreFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\Task\Rollback;
namespace PrestaShop\Module\AutoUpgrade\Task\Restore;

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Runner/AllBackupTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
class AllBackupTasks extends ChainedTasks
{
const initialTask = TaskName::TASK_BACKUP_FILES;
const initialTask = TaskName::TASK_BACKUP_INITIALIZATION;

/**
* @var string
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Runner/AllUpdateTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function setOptions(array $options): void
if (!empty($options['channel'])) {
$config = [
'channel' => $options['channel'],
]);
];
$this->container->getUpgradeConfiguration()->validate($config);
$this->container->getUpgradeConfiguration()->merge($config);
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/TaskName.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TaskName

// RESTORE
const TASK_RESTORE = 'Restore';
const TASK_NO_RESTORE_FOUND = 'NoRestoreFound';
const TASK_RESTORE_EMPTY = 'RestoreEmpty';
const TASK_RESTORE_DATABASE = 'RestoreDatabase';
const TASK_RESTORE_FILES = 'RestoreFiles';
const TASK_RESTORE_COMPLETE = 'RestoreComplete';
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Update/Unzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function run(): int
}

$this->next = TaskName::TASK_UPDATE_FILES;
$this->logger->info($this->translator->trans('File extraction complete. Now upgrading files...'));
$this->logger->info($this->translator->trans('File extraction complete. Now updating files...'));

$this->container->getAnalytics()->track('Backup Launched', Analytics::WITH_BACKUP_PROPERTIES);

Expand Down
14 changes: 7 additions & 7 deletions classes/UpgradeTools/TaskRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
use PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\CompareReleases;
use PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\UpdateConfig;
use PrestaShop\Module\AutoUpgrade\Task\NullTask;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\NoRestoreFound;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\Restore;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreComplete;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Restore\Restore;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreComplete;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreEmpty;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\Task\Update\CleanDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Update\Download;
Expand Down Expand Up @@ -68,8 +68,8 @@ public static function get(string $step, UpgradeContainer $container): AbstractT
// RESTORE
case TaskName::TASK_RESTORE:
return new Restore($container);
case TaskName::TASK_NO_RESTORE_FOUND:
return new NoRestoreFound($container);
case TaskName::TASK_RESTORE_EMPTY:
return new RestoreEmpty($container);
case TaskName::TASK_RESTORE_DATABASE:
return new RestoreDatabase($container);
case TaskName::TASK_RESTORE_FILES:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Progress/CompletionCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
use PrestaShop\Module\AutoUpgrade\Progress\CompletionCalculator;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Runner\SingleTask;
use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateFiles;
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/Task/Upgrade/BackupDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
*/

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\BackupDb;
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupDatabase;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;

class BackupDbTest extends TestCase
class BackupDatabaseTest extends TestCase
{
/**
* @throws Exception
*/
public function testTablesAreIgnored()
{
define('_DB_PREFIX_', 'doge_');

$container = new UpgradeContainer(__DIR__, __DIR__ . '/..');
$task = new BackupDb($container);
$task = new BackupDatabase($container);

$tables = json_decode(file_get_contents(__DIR__ . '/../../../fixtures/listOfTablesInDb.json'), true);
$expectedTables = $this->getExpectedTablesToSync();
Expand Down

0 comments on commit 03af9b4

Please sign in to comment.