forked from PrestaShop/autoupgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackupFiles.php
98 lines (84 loc) · 4.74 KB
/
BackupFiles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?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\TaskRunner\Upgrade;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
class BackupFiles extends AbstractTask
{
public function run()
{
if (!$this->container->getUpgradeConfiguration()->get('PS_AUTOUP_BACKUP')) {
$this->stepDone = true;
$this->next = 'backupDb';
$this->logger->info('File backup skipped.');
return true;
}
$this->stepDone = false;
$backupFilesFilename = $this->container->getState()->getBackupFilesFilename();
if (empty($backupFilesFilename)) {
$this->next = 'error';
$this->error = true;
$this->logger->info($this->translator->trans('Error during backupFiles', [], 'Modules.Autoupgrade.Admin'));
$this->logger->error($this->translator->trans('[ERROR] backupFiles filename has not been set', [], 'Modules.Autoupgrade.Admin'));
return false;
}
if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::FILES_TO_BACKUP_LIST)) {
/** @todo : only add files and dir listed in "originalPrestashopVersion" list */
$filesToBackup = $this->container->getFilesystemAdapter()->listFilesInDir($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), 'backup', false);
$filesToBackup = array_reverse($filesToBackup);
$this->container->getFileConfigurationStorage()->save($filesToBackup, UpgradeFileNames::FILES_TO_BACKUP_LIST);
if (count($filesToBackup)) {
$this->logger->debug($this->translator->trans('%s Files to backup.', [count($filesToBackup)], 'Modules.Autoupgrade.Admin'));
}
// delete old backup, create new
if (!empty($backupFilesFilename) && file_exists($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename)) {
unlink($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename);
}
$this->logger->debug($this->translator->trans('Backup files initialized in %s', [$backupFilesFilename], 'Modules.Autoupgrade.Admin'));
}
$filesToBackup = $this->container->getFileConfigurationStorage()->load(UpgradeFileNames::FILES_TO_BACKUP_LIST);
$this->next = 'backupFiles';
if (is_array($filesToBackup) && count($filesToBackup)) {
$this->logger->info($this->translator->trans('Backup files in progress. %d files left', [count($filesToBackup)], 'Modules.Autoupgrade.Admin'));
$this->stepDone = false;
$res = $this->container->getZipAction()->compress($filesToBackup, $this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $backupFilesFilename);
if (!$res) {
$this->next = 'error';
$this->logger->info($this->translator->trans('Unable to open archive', [], 'Modules.Autoupgrade.Admin'));
return false;
}
$this->container->getFileConfigurationStorage()->save($filesToBackup, UpgradeFileNames::FILES_TO_BACKUP_LIST);
}
if (count($filesToBackup) <= 0) {
$this->stepDone = true;
$this->status = 'ok';
$this->next = 'backupDb';
$this->logger->debug($this->translator->trans('All files have been added to archive.', [], 'Modules.Autoupgrade.Admin'));
$this->logger->info($this->translator->trans('All files saved. Now backing up database', [], 'Modules.Autoupgrade.Admin'));
}
}
}