예제 #1
0
 /**
  * Create backup action
  *
  * @return void|\Magento\Backend\App\Action
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function executeInternal()
 {
     if (!$this->getRequest()->isAjax()) {
         return $this->_redirect('*/*/index');
     }
     $response = new \Magento\Framework\DataObject();
     /**
      * @var \Magento\Backup\Helper\Data $helper
      */
     $helper = $this->_objectManager->get('Magento\\Backup\\Helper\\Data');
     try {
         $type = $this->getRequest()->getParam('type');
         if ($type == \Magento\Framework\Backup\Factory::TYPE_SYSTEM_SNAPSHOT && $this->getRequest()->getParam('exclude_media')) {
             $type = \Magento\Framework\Backup\Factory::TYPE_SNAPSHOT_WITHOUT_MEDIA;
         }
         $backupManager = $this->_backupFactory->create($type)->setBackupExtension($helper->getExtensionByType($type))->setTime(time())->setBackupsDir($helper->getBackupsDir());
         $backupManager->setName($this->getRequest()->getParam('backup_name'));
         $this->_coreRegistry->register('backup_manager', $backupManager);
         if ($this->getRequest()->getParam('maintenance_mode')) {
             if (!$this->maintenanceMode->set(true)) {
                 $response->setError(__('You need more permissions to activate maintenance mode right now.') . ' ' . __('To create the backup, please deselect ' . '"Put store into maintenance mode" or update your permissions.'));
                 $backupManager->setErrorMessage(__("Something went wrong while putting your store into maintenance mode."));
                 return $this->getResponse()->representJson($response->toJson());
             }
         }
         if ($type != \Magento\Framework\Backup\Factory::TYPE_DB) {
             /** @var Filesystem $filesystem */
             $filesystem = $this->_objectManager->get('Magento\\Framework\\Filesystem');
             $backupManager->setRootDir($filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath())->addIgnorePaths($helper->getBackupIgnorePaths());
         }
         $successMessage = $helper->getCreateSuccessMessageByType($type);
         $backupManager->create();
         $this->messageManager->addSuccess($successMessage);
         $response->setRedirectUrl($this->getUrl('*/*/index'));
     } catch (\Magento\Framework\Backup\Exception\NotEnoughFreeSpace $e) {
         $errorMessage = __('You need more free space to create a backup.');
     } catch (\Magento\Framework\Backup\Exception\NotEnoughPermissions $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->info($e->getMessage());
         $errorMessage = __('You need more permissions to create a backup.');
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->info($e->getMessage());
         $errorMessage = __('We can\'t create the backup right now.');
     }
     if (!empty($errorMessage)) {
         $response->setError($errorMessage);
         $backupManager->setErrorMessage($errorMessage);
     }
     if ($this->getRequest()->getParam('maintenance_mode')) {
         $this->maintenanceMode->set(false);
     }
     $this->getResponse()->representJson($response->toJson());
 }
예제 #2
0
 /**
  * Rollback Action
  *
  * @return void|\Magento\Backend\App\Action
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function executeInternal()
 {
     if (!$this->_objectManager->get('Magento\\Backup\\Helper\\Data')->isRollbackAllowed()) {
         $this->_forward('denied');
     }
     if (!$this->getRequest()->isAjax()) {
         return $this->_redirect('*/*/index');
     }
     $helper = $this->_objectManager->get('Magento\\Backup\\Helper\\Data');
     $response = new \Magento\Framework\DataObject();
     try {
         /* @var $backup \Magento\Backup\Model\Backup */
         $backup = $this->_backupModelFactory->create($this->getRequest()->getParam('time'), $this->getRequest()->getParam('type'));
         if (!$backup->getTime() || !$backup->exists()) {
             return $this->_redirect('backup/*');
         }
         if (!$backup->getTime()) {
             throw new \Magento\Framework\Backup\Exception\CantLoadSnapshot(__('Can\'t load snapshot archive'));
         }
         $type = $backup->getType();
         $backupManager = $this->_backupFactory->create($type)->setBackupExtension($helper->getExtensionByType($type))->setTime($backup->getTime())->setBackupsDir($helper->getBackupsDir())->setName($backup->getName(), false)->setResourceModel($this->_objectManager->create('Magento\\Backup\\Model\\ResourceModel\\Db'));
         $this->_coreRegistry->register('backup_manager', $backupManager);
         $passwordValid = $this->_objectManager->create('Magento\\Backup\\Model\\Backup')->validateUserPassword($this->getRequest()->getParam('password'));
         if (!$passwordValid) {
             $response->setError(__('Please correct the password.'));
             $backupManager->setErrorMessage(__('Please correct the password.'));
             return $this->getResponse()->representJson($response->toJson());
         }
         if ($this->getRequest()->getParam('maintenance_mode')) {
             if (!$this->maintenanceMode->set(true)) {
                 $response->setError(__('You need more permissions to activate maintenance mode right now.') . ' ' . __('To complete the rollback, please deselect ' . '"Put store into maintenance mode" or update your permissions.'));
                 $backupManager->setErrorMessage(__('Something went wrong while putting your store into maintenance mode.'));
                 return $this->getResponse()->representJson($response->toJson());
             }
         }
         if ($type != \Magento\Framework\Backup\Factory::TYPE_DB) {
             /** @var Filesystem $filesystem */
             $filesystem = $this->_objectManager->get('Magento\\Framework\\Filesystem');
             $backupManager->setRootDir($filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath())->addIgnorePaths($helper->getRollbackIgnorePaths());
             if ($this->getRequest()->getParam('use_ftp', false)) {
                 $backupManager->setUseFtp($this->getRequest()->getParam('ftp_host', ''), $this->getRequest()->getParam('ftp_user', ''), $this->getRequest()->getParam('ftp_pass', ''), $this->getRequest()->getParam('ftp_path', ''));
             }
         }
         $backupManager->rollback();
         $helper->invalidateCache();
         $adminSession = $this->_getSession();
         $adminSession->destroy();
         $response->setRedirectUrl($this->getUrl('*'));
     } catch (\Magento\Framework\Backup\Exception\CantLoadSnapshot $e) {
         $errorMsg = __('We can\'t find the backup file.');
     } catch (\Magento\Framework\Backup\Exception\FtpConnectionFailed $e) {
         $errorMsg = __('We can\'t connect to the FTP right now.');
     } catch (\Magento\Framework\Backup\Exception\FtpValidationFailed $e) {
         $errorMsg = __('Failed to validate FTP.');
     } catch (\Magento\Framework\Backup\Exception\NotEnoughPermissions $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->info($e->getMessage());
         $errorMsg = __('You need more permissions to perform a rollback.');
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->info($e->getMessage());
         $errorMsg = __('Failed to rollback.');
     }
     if (!empty($errorMsg)) {
         $response->setError($errorMsg);
         $backupManager->setErrorMessage($errorMsg);
     }
     if ($this->getRequest()->getParam('maintenance_mode')) {
         $this->maintenanceMode->set(false);
     }
     $this->getResponse()->representJson($response->toJson());
 }