* @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', array(), 'Modules.Autoupgrade.Admin')); $this->logger->error($this->translator->trans('[ERROR] backupFiles filename has not been set', array(), '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.', array(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', array($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', array(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', array(), '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.', array(), 'Modules.Autoupgrade.Admin')); $this->logger->info($this->translator->trans('All files saved. Now backing up database', array(), 'Modules.Autoupgrade.Admin')); } } }