diff --git a/lib/internal/Magento/Framework/Backup/AbstractBackup.php b/lib/internal/Magento/Framework/Backup/AbstractBackup.php index 7433d261d7ed6..66bcdbf16a54b 100644 --- a/lib/internal/Magento/Framework/Backup/AbstractBackup.php +++ b/lib/internal/Magento/Framework/Backup/AbstractBackup.php @@ -5,12 +5,15 @@ */ namespace Magento\Framework\Backup; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Phrase; + /** * Class to work with archives * * @api */ -abstract class AbstractBackup implements BackupInterface +abstract class AbstractBackup implements BackupInterface, SourceFileInterface { /** * Backup name @@ -68,6 +71,13 @@ abstract class AbstractBackup implements BackupInterface */ protected $_lastErrorMessage; + /** + * Keep Source files in Backup + * + * @var boolean + */ + private $keepSourceFile; + /** * Set Backup Extension * @@ -138,14 +148,14 @@ public function getTime() * Set root directory of Magento installation * * @param string $rootDir - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException * @return $this */ public function setRootDir($rootDir) { if (!is_dir($rootDir)) { - throw new \Magento\Framework\Exception\LocalizedException( - new \Magento\Framework\Phrase('Bad root directory') + throw new LocalizedException( + new Phrase('Bad root directory') ); } @@ -296,4 +306,26 @@ protected function _filterName($name) return $name; } + + /** + * Check if keep files of backup + * + * @return bool + */ + public function keepSourceFile() + { + return $this->keepSourceFile; + } + + /** + * Set if keep files of backup + * + * @param bool $keepSourceFile + * @return $this + */ + public function setKeepSourceFile(bool $keepSourceFile) + { + $this->keepSourceFile = $keepSourceFile; + return $this; + } } diff --git a/lib/internal/Magento/Framework/Backup/Archive/Tar.php b/lib/internal/Magento/Framework/Backup/Archive/Tar.php index ae14ae2838e37..ca8e7caf9884d 100644 --- a/lib/internal/Magento/Framework/Backup/Archive/Tar.php +++ b/lib/internal/Magento/Framework/Backup/Archive/Tar.php @@ -11,6 +11,10 @@ */ namespace Magento\Framework\Backup\Archive; +use Magento\Framework\Backup\Filesystem\Iterator\Filter; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; + class Tar extends \Magento\Framework\Archive\Tar { /** @@ -35,12 +39,12 @@ protected function _createTar($skipRoot = false, $finalize = false) { $path = $this->_getCurrentFile(); - $filesystemIterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path), - \RecursiveIteratorIterator::SELF_FIRST + $filesystemIterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($path), + RecursiveIteratorIterator::SELF_FIRST ); - $iterator = new \Magento\Framework\Backup\Filesystem\Iterator\Filter( + $iterator = new Filter( $filesystemIterator, $this->_skipFiles ); diff --git a/lib/internal/Magento/Framework/Backup/Db.php b/lib/internal/Magento/Framework/Backup/Db.php index d9585f25365c8..d3b72f30d8cb3 100644 --- a/lib/internal/Magento/Framework/Backup/Db.php +++ b/lib/internal/Magento/Framework/Backup/Db.php @@ -5,6 +5,10 @@ */ namespace Magento\Framework\Backup; +use Magento\Framework\Archive; +use Magento\Framework\Backup\Db\BackupFactory; +use Magento\Framework\Backup\Filesystem\Iterator\File; + /** * Class to work with database backups * @@ -14,14 +18,14 @@ class Db extends AbstractBackup { /** - * @var \Magento\Framework\Backup\Db\BackupFactory + * @var BackupFactory */ protected $_backupFactory; /** - * @param \Magento\Framework\Backup\Db\BackupFactory $backupFactory + * @param BackupFactory $backupFactory */ - public function __construct(\Magento\Framework\Backup\Db\BackupFactory $backupFactory) + public function __construct(BackupFactory $backupFactory) { $this->_backupFactory = $backupFactory; } @@ -38,14 +42,16 @@ public function rollback() $this->_lastOperationSucceed = false; - $archiveManager = new \Magento\Framework\Archive(); + $archiveManager = new Archive(); $source = $archiveManager->unpack($this->getBackupPath(), $this->getBackupsDir()); - $file = new \Magento\Framework\Backup\Filesystem\Iterator\File($source); + $file = new File($source); foreach ($file as $statement) { $this->getResourceModel()->runCommand($statement); } - @unlink($source); + if ($this->keepSourceFile() === false) { + @unlink($source); + } $this->_lastOperationSucceed = true; diff --git a/lib/internal/Magento/Framework/Backup/Db/BackupFactory.php b/lib/internal/Magento/Framework/Backup/Db/BackupFactory.php index e8d5fa4bd0b5f..d1c9c3df1e9aa 100644 --- a/lib/internal/Magento/Framework/Backup/Db/BackupFactory.php +++ b/lib/internal/Magento/Framework/Backup/Db/BackupFactory.php @@ -6,6 +6,8 @@ namespace Magento\Framework\Backup\Db; +use Magento\Framework\ObjectManagerInterface; + /** * @api */ @@ -14,33 +16,33 @@ class BackupFactory /** * Object manager * - * @var \Magento\Framework\ObjectManagerInterface + * @var ObjectManagerInterface */ - private $_objectManager; + private $objectManager; /** * @var string */ - private $_backupInstanceName; + private $backupInstanceName; /** * @var string */ - private $_backupDbInstanceName; + private $backupDbInstanceName; /** - * @param \Magento\Framework\ObjectManagerInterface $objectManager + * @param ObjectManagerInterface $objectManager * @param string $backupInstanceName * @param string $backupDbInstanceName */ public function __construct( - \Magento\Framework\ObjectManagerInterface $objectManager, + ObjectManagerInterface $objectManager, $backupInstanceName, $backupDbInstanceName ) { - $this->_objectManager = $objectManager; - $this->_backupInstanceName = $backupInstanceName; - $this->_backupDbInstanceName = $backupDbInstanceName; + $this->objectManager = $objectManager; + $this->backupInstanceName = $backupInstanceName; + $this->backupDbInstanceName = $backupDbInstanceName; } /** @@ -51,7 +53,7 @@ public function __construct( */ public function createBackupModel(array $arguments = []) { - return $this->_objectManager->create($this->_backupInstanceName, $arguments); + return $this->objectManager->create($this->backupInstanceName, $arguments); } /** @@ -62,6 +64,6 @@ public function createBackupModel(array $arguments = []) */ public function createBackupDbModel(array $arguments = []) { - return $this->_objectManager->create($this->_backupDbInstanceName, $arguments); + return $this->objectManager->create($this->backupDbInstanceName, $arguments); } } diff --git a/lib/internal/Magento/Framework/Backup/Factory.php b/lib/internal/Magento/Framework/Backup/Factory.php index 4a87c33927a5b..effa796a2a074 100644 --- a/lib/internal/Magento/Framework/Backup/Factory.php +++ b/lib/internal/Magento/Framework/Backup/Factory.php @@ -9,6 +9,10 @@ */ namespace Magento\Framework\Backup; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\ObjectManagerInterface; +use Magento\Framework\Phrase; + /** * @api */ @@ -17,7 +21,7 @@ class Factory /** * Object manager * - * @var \Magento\Framework\ObjectManagerInterface + * @var ObjectManagerInterface */ private $_objectManager; @@ -54,9 +58,9 @@ class Factory protected $_allowedTypes; /** - * @param \Magento\Framework\ObjectManagerInterface $objectManager + * @param ObjectManagerInterface $objectManager */ - public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager) + public function __construct(ObjectManagerInterface $objectManager) { $this->_objectManager = $objectManager; $this->_allowedTypes = [ @@ -73,13 +77,13 @@ public function __construct(\Magento\Framework\ObjectManagerInterface $objectMan * * @param string $type * @return BackupInterface - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ public function create($type) { if (!in_array($type, $this->_allowedTypes)) { - throw new \Magento\Framework\Exception\LocalizedException( - new \Magento\Framework\Phrase( + throw new LocalizedException( + new Phrase( 'Current implementation not supported this type (%1) of backup.', [$type] ) diff --git a/lib/internal/Magento/Framework/Backup/Filesystem.php b/lib/internal/Magento/Framework/Backup/Filesystem.php index 606719b52c871..f3946444cec20 100644 --- a/lib/internal/Magento/Framework/Backup/Filesystem.php +++ b/lib/internal/Magento/Framework/Backup/Filesystem.php @@ -7,6 +7,15 @@ namespace Magento\Framework\Backup; use Magento\Framework\App\ObjectManager; +use Magento\Framework\Archive\Gz; +use Magento\Framework\Backup\Archive\Tar; +use Magento\Framework\Backup\Exception\NotEnoughFreeSpace; +use Magento\Framework\Backup\Exception\NotEnoughPermissions; +use Magento\Framework\Backup\Filesystem\Helper; +use Magento\Framework\Backup\Filesystem\Rollback\Fs; +use Magento\Framework\Backup\Filesystem\Rollback\Ftp; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Phrase; /** * Class to work with filesystem backups @@ -58,19 +67,19 @@ class Filesystem extends AbstractBackup protected $_ftpPath; /** - * @var \Magento\Framework\Backup\Filesystem\Rollback\Ftp + * @var Ftp */ protected $rollBackFtp; /** - * @var \Magento\Framework\Backup\Filesystem\Rollback\Fs + * @var Fs */ protected $rollBackFs; /** * Implementation Rollback functionality for Filesystem * - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException * @return bool */ public function rollback() @@ -90,7 +99,7 @@ public function rollback() /** * Implementation Create Backup functionality for Filesystem * - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException * @return boolean */ public function create() @@ -102,41 +111,41 @@ public function create() $this->_checkBackupsDir(); - $fsHelper = new \Magento\Framework\Backup\Filesystem\Helper(); + $fsHelper = new Helper(); $filesInfo = $fsHelper->getInfo( $this->getRootDir(), - \Magento\Framework\Backup\Filesystem\Helper::INFO_READABLE | - \Magento\Framework\Backup\Filesystem\Helper::INFO_SIZE, + Helper::INFO_READABLE | + Helper::INFO_SIZE, $this->getIgnorePaths() ); if (!$filesInfo['readable']) { - throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions( - new \Magento\Framework\Phrase('Not enough permissions to read files for backup') + throw new NotEnoughPermissions( + new Phrase('Not enough permissions to read files for backup') ); } $this->validateAvailableDiscSpace($this->getBackupsDir(), $filesInfo['size']); $tarTmpPath = $this->_getTarTmpPath(); - $tarPacker = new \Magento\Framework\Backup\Archive\Tar(); + $tarPacker = new Tar(); $tarPacker->setSkipFiles($this->getIgnorePaths())->pack($this->getRootDir(), $tarTmpPath, true); if (!is_file($tarTmpPath) || filesize($tarTmpPath) == 0) { - throw new \Magento\Framework\Exception\LocalizedException( - new \Magento\Framework\Phrase('Failed to create backup') + throw new LocalizedException( + new Phrase('Failed to create backup') ); } $backupPath = $this->getBackupPath(); - $gzPacker = new \Magento\Framework\Archive\Gz(); + $gzPacker = new Gz(); $gzPacker->pack($tarTmpPath, $backupPath); if (!is_file($backupPath) || filesize($backupPath) == 0) { - throw new \Magento\Framework\Exception\LocalizedException( - new \Magento\Framework\Phrase('Failed to create backup') + throw new LocalizedException( + new Phrase('Failed to create backup') ); } @@ -152,15 +161,15 @@ public function create() * @param string $backupDir * @param int $size * @return void - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ public function validateAvailableDiscSpace($backupDir, $size) { $freeSpace = disk_free_space($backupDir); $requiredSpace = 2 * $size; if ($requiredSpace > $freeSpace) { - throw new \Magento\Framework\Backup\Exception\NotEnoughFreeSpace( - new \Magento\Framework\Phrase( + throw new NotEnoughFreeSpace( + new Phrase( 'Warning: necessary space for backup is ' . (ceil($requiredSpace) / 1024) . 'MB, but your free disc space is ' . (ceil($freeSpace) / 1024) . 'MB.' ) @@ -269,7 +278,7 @@ public function getFtpConnectString() * Check backups directory existence and whether it's writeable * * @return void - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ protected function _checkBackupsDir() { @@ -279,8 +288,8 @@ protected function _checkBackupsDir() $backupsDirParentDirectory = basename($backupsDir); if (!is_writeable($backupsDirParentDirectory)) { - throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions( - new \Magento\Framework\Phrase('Cant create backups directory') + throw new NotEnoughPermissions( + new Phrase('Cant create backups directory') ); } @@ -289,8 +298,8 @@ protected function _checkBackupsDir() } if (!is_writable($backupsDir)) { - throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions( - new \Magento\Framework\Phrase('Backups directory is not writeable') + throw new NotEnoughPermissions( + new Phrase('Backups directory is not writeable') ); } } @@ -307,14 +316,14 @@ protected function _getTarTmpPath() } /** - * @return \Magento\Framework\Backup\Filesystem\Rollback\Ftp + * @return Ftp * @deprecated 100.2.0 */ protected function getRollBackFtp() { if (!$this->rollBackFtp) { $this->rollBackFtp = ObjectManager::getInstance()->create( - \Magento\Framework\Backup\Filesystem\Rollback\Ftp::class, + Ftp::class, ['snapshotObject' => $this] ); } @@ -323,14 +332,14 @@ protected function getRollBackFtp() } /** - * @return \Magento\Framework\Backup\Filesystem\Rollback\Fs + * @return Fs * @deprecated 100.2.0 */ protected function getRollBackFs() { if (!$this->rollBackFs) { $this->rollBackFs = ObjectManager::getInstance()->create( - \Magento\Framework\Backup\Filesystem\Rollback\Fs::class, + Fs::class, ['snapshotObject' => $this] ); } diff --git a/lib/internal/Magento/Framework/Backup/Filesystem/Helper.php b/lib/internal/Magento/Framework/Backup/Filesystem/Helper.php index 36f02191bbc6d..fb33b4639aad8 100644 --- a/lib/internal/Magento/Framework/Backup/Filesystem/Helper.php +++ b/lib/internal/Magento/Framework/Backup/Filesystem/Helper.php @@ -5,6 +5,10 @@ */ namespace Magento\Framework\Backup\Filesystem; +use Magento\Framework\Backup\Filesystem\Iterator\Filter; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; + /** * Filesystem helper * @@ -56,12 +60,12 @@ class Helper */ public function rm($path, $skipPaths = [], $removeRoot = false) { - $filesystemIterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path), - \RecursiveIteratorIterator::CHILD_FIRST + $filesystemIterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($path), + RecursiveIteratorIterator::CHILD_FIRST ); - $iterator = new \Magento\Framework\Backup\Filesystem\Iterator\Filter($filesystemIterator, $skipPaths); + $iterator = new Filter($filesystemIterator, $skipPaths); foreach ($iterator as $item) { $item->isDir() ? @rmdir($item->__toString()) : @unlink($item->__toString()); @@ -99,12 +103,12 @@ public function getInfo($path, $infoOptions = self::INFO_ALL, $skipFiles = []) $info['size'] = 0; } - $filesystemIterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path), - \RecursiveIteratorIterator::CHILD_FIRST + $filesystemIterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($path), + RecursiveIteratorIterator::CHILD_FIRST ); - $iterator = new \Magento\Framework\Backup\Filesystem\Iterator\Filter($filesystemIterator, $skipFiles); + $iterator = new Filter($filesystemIterator, $skipFiles); foreach ($iterator as $item) { if ($item->isLink()) { diff --git a/lib/internal/Magento/Framework/Backup/Filesystem/Iterator/Filter.php b/lib/internal/Magento/Framework/Backup/Filesystem/Iterator/Filter.php index 8f0c80287a353..ae7805e716a73 100644 --- a/lib/internal/Magento/Framework/Backup/Filesystem/Iterator/Filter.php +++ b/lib/internal/Magento/Framework/Backup/Filesystem/Iterator/Filter.php @@ -11,6 +11,8 @@ */ namespace Magento\Framework\Backup\Filesystem\Iterator; +use Iterator; + class Filter extends \FilterIterator { /** @@ -23,10 +25,10 @@ class Filter extends \FilterIterator /** * Constructor * - * @param \Iterator $iterator + * @param Iterator $iterator * @param array $filters list of files to skip */ - public function __construct(\Iterator $iterator, array $filters) + public function __construct(Iterator $iterator, array $filters) { parent::__construct($iterator); $this->_filters = $filters; diff --git a/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/AbstractRollback.php b/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/AbstractRollback.php index efd1b335d3252..8c8c21a8405b6 100644 --- a/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/AbstractRollback.php +++ b/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/AbstractRollback.php @@ -5,6 +5,8 @@ */ namespace Magento\Framework\Backup\Filesystem\Rollback; +use Magento\Framework\Backup\Filesystem; + /** * Filesystem rollback workers abstract class * @@ -15,16 +17,16 @@ abstract class AbstractRollback /** * Snapshot object * - * @var \Magento\Framework\Backup\Filesystem + * @var Filesystem */ protected $_snapshot; /** * Default worker constructor * - * @param \Magento\Framework\Backup\Filesystem $snapshotObject + * @param Filesystem $snapshotObject */ - public function __construct(\Magento\Framework\Backup\Filesystem $snapshotObject) + public function __construct(Filesystem $snapshotObject) { $this->_snapshot = $snapshotObject; } diff --git a/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Fs.php b/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Fs.php index 86150ab948c5b..19109001b02c1 100644 --- a/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Fs.php +++ b/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Fs.php @@ -6,6 +6,16 @@ namespace Magento\Framework\Backup\Filesystem\Rollback; use Magento\Framework\App\ObjectManager; +use Magento\Framework\Archive; +use Magento\Framework\Archive\Gz; +use Magento\Framework\Archive\Helper\File; +use Magento\Framework\Archive\Helper\File\Gz as HelperGz; +use Magento\Framework\Archive\Tar; +use Magento\Framework\Backup\Exception\CantLoadSnapshot; +use Magento\Framework\Backup\Exception\NotEnoughPermissions; +use Magento\Framework\Backup\Filesystem\Helper; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Phrase; /** * Rollback worker for rolling back via local filesystem @@ -15,7 +25,7 @@ class Fs extends AbstractRollback { /** - * @var \Magento\Framework\Backup\Filesystem\Helper + * @var Helper */ private $fsHelper; @@ -23,7 +33,7 @@ class Fs extends AbstractRollback * Files rollback implementation via local filesystem * * @return void - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException * * @see AbstractRollback::run() */ @@ -32,8 +42,8 @@ public function run() $snapshotPath = $this->_snapshot->getBackupPath(); if (!is_file($snapshotPath) || !is_readable($snapshotPath)) { - throw new \Magento\Framework\Backup\Exception\CantLoadSnapshot( - new \Magento\Framework\Phrase('Can\'t load snapshot archive') + throw new CantLoadSnapshot( + new Phrase('Can\'t load snapshot archive') ); } @@ -41,49 +51,55 @@ public function run() $filesInfo = $fsHelper->getInfo( $this->_snapshot->getRootDir(), - \Magento\Framework\Backup\Filesystem\Helper::INFO_WRITABLE, + Helper::INFO_WRITABLE, $this->_snapshot->getIgnorePaths() ); if (!$filesInfo['writable']) { if (!empty($filesInfo['writableMeta'])) { - throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions( - new \Magento\Framework\Phrase( + throw new NotEnoughPermissions( + new Phrase( 'You need write permissions for: %1', [implode(', ', $filesInfo['writableMeta'])] ) ); } - throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions( - new \Magento\Framework\Phrase('Unable to make rollback because not all files are writable') + throw new NotEnoughPermissions( + new Phrase('Unable to make rollback because not all files are writable') ); } - $archiver = new \Magento\Framework\Archive(); + $archiver = new Archive(); /** * we need these fake initializations because all magento's files in filesystem will be deleted and autoloader * wont be able to load classes that we need for unpacking */ - new \Magento\Framework\Archive\Tar(); - new \Magento\Framework\Archive\Gz(); - new \Magento\Framework\Archive\Helper\File(''); - new \Magento\Framework\Archive\Helper\File\Gz(''); - new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('dummy')); + new Tar(); + new Gz(); + new File(''); + new HelperGz(''); + new LocalizedException(new Phrase('dummy')); - $fsHelper->rm($this->_snapshot->getRootDir(), $this->_snapshot->getIgnorePaths()); + if (!$this->_snapshot->keepSourceFile()) { + $fsHelper->rm($this->_snapshot->getRootDir(), $this->_snapshot->getIgnorePaths()); + } $archiver->unpack($snapshotPath, $this->_snapshot->getRootDir()); + + if ($this->_snapshot->keepSourceFile() === false) { + @unlink($snapshotPath); + } } /** - * @return \Magento\Framework\Backup\Filesystem\Helper + * @return Helper * @deprecated 100.2.0 */ private function getFsHelper() { if (!$this->fsHelper) { - $this->fsHelper = ObjectManager::getInstance()->get(\Magento\Framework\Backup\Filesystem\Helper::class); + $this->fsHelper = ObjectManager::getInstance()->get(Helper::class); } return $this->fsHelper; diff --git a/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Ftp.php b/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Ftp.php index 10322bb5ab23c..a2b0c09eb9993 100644 --- a/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Ftp.php +++ b/lib/internal/Magento/Framework/Backup/Filesystem/Rollback/Ftp.php @@ -5,7 +5,12 @@ */ namespace Magento\Framework\Backup\Filesystem\Rollback; -use Magento\Framework\Filesystem\DriverInterface; +use Magento\Framework\Backup\Exception\CantLoadSnapshot; +use Magento\Framework\Backup\Exception\FtpConnectionFailed; +use Magento\Framework\Backup\Exception\FtpValidationFailed; +use Magento\Framework\Backup\Filesystem\Helper; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Phrase; /** * Rollback worker for rolling back via ftp @@ -25,7 +30,7 @@ class Ftp extends AbstractRollback * Files rollback implementation via ftp * * @return void - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException * * @see AbstractRollback::run() */ @@ -34,8 +39,8 @@ public function run() $snapshotPath = $this->_snapshot->getBackupPath(); if (!is_file($snapshotPath) || !is_readable($snapshotPath)) { - throw new \Magento\Framework\Backup\Exception\CantLoadSnapshot( - new \Magento\Framework\Phrase('Can\'t load snapshot archive') + throw new CantLoadSnapshot( + new Phrase('Can\'t load snapshot archive') ); } @@ -45,19 +50,22 @@ public function run() $tmpDir = $this->_createTmpDir(); $this->_unpackSnapshot($tmpDir); - $fsHelper = new \Magento\Framework\Backup\Filesystem\Helper(); + $fsHelper = new Helper(); $this->_cleanupFtp(); $this->_uploadBackupToFtp($tmpDir); - $fsHelper->rm($tmpDir, [], true); + if ($this->_snapshot->keepSourceFile() === false) { + $fsHelper->rm($tmpDir, [], true); + $this->_ftpClient->delete($snapshotPath); + } } /** * Initialize ftp client and connect to ftp * * @return void - * @throws \Magento\Framework\Backup\Exception\FtpConnectionFailed + * @throws FtpConnectionFailed */ protected function _initFtpClient() { @@ -65,8 +73,8 @@ protected function _initFtpClient() $this->_ftpClient = new \Magento\Framework\System\Ftp(); $this->_ftpClient->connect($this->_snapshot->getFtpConnectString()); } catch (\Exception $e) { - throw new \Magento\Framework\Backup\Exception\FtpConnectionFailed( - new \Magento\Framework\Phrase($e->getMessage()) + throw new FtpConnectionFailed( + new Phrase($e->getMessage()) ); } } @@ -75,7 +83,7 @@ protected function _initFtpClient() * Perform ftp validation. Check whether ftp account provided points to current magento installation * * @return void - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ protected function _validateFtp() { @@ -86,8 +94,8 @@ protected function _validateFtp() @fclose($fh); if (!is_file($validationFilePath)) { - throw new \Magento\Framework\Exception\LocalizedException( - new \Magento\Framework\Phrase('Unable to validate ftp account') + throw new LocalizedException( + new Phrase('Unable to validate ftp account') ); } @@ -98,8 +106,8 @@ protected function _validateFtp() @unlink($validationFilePath); if (!$fileExistsOnFtp) { - throw new \Magento\Framework\Backup\Exception\FtpValidationFailed( - new \Magento\Framework\Phrase('Failed to validate ftp account') + throw new FtpValidationFailed( + new Phrase('Failed to validate ftp account') ); } } @@ -120,7 +128,7 @@ protected function _unpackSnapshot($tmpDir) /** * @return string - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ protected function _createTmpDir() { @@ -130,7 +138,7 @@ protected function _createTmpDir() if (false === $result) { throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions( - new \Magento\Framework\Phrase('Failed to create directory %1', [$tmpDir]) + new Phrase('Failed to create directory %1', [$tmpDir]) ); } @@ -169,7 +177,7 @@ protected function _cleanupFtp() * * @param string $tmpDir * @return void - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function _uploadBackupToFtp($tmpDir) @@ -198,7 +206,7 @@ protected function _uploadBackupToFtp($tmpDir) $result = $this->_ftpClient->put($ftpPath, $item->__toString()); if (false === $result) { throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions( - new \Magento\Framework\Phrase('Failed to upload file %1 to ftp', [$item->__toString()]) + new Phrase('Failed to upload file %1 to ftp', [$item->__toString()]) ); } } diff --git a/lib/internal/Magento/Framework/Backup/Snapshot.php b/lib/internal/Magento/Framework/Backup/Snapshot.php index fb3d25a0ceb0d..1d2059eaacd3d 100644 --- a/lib/internal/Magento/Framework/Backup/Snapshot.php +++ b/lib/internal/Magento/Framework/Backup/Snapshot.php @@ -11,6 +11,7 @@ */ namespace Magento\Framework\Backup; +use Exception; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem as AppFilesystem; @@ -48,7 +49,7 @@ public function __construct(AppFilesystem $filesystem, Factory $backupFactory) /** * Implementation Rollback functionality for Snapshot * - * @throws \Exception + * @throws Exception * @return bool */ public function rollback() @@ -59,7 +60,7 @@ public function rollback() try { $this->_getDbBackupManager()->rollback(); - } catch (\Exception $e) { + } catch (Exception $e) { $this->_removeDbBackup(); throw $e; } @@ -73,7 +74,7 @@ public function rollback() /** * Implementation Create Backup functionality for Snapshot * - * @throws \Exception + * @throws Exception * @return bool */ public function create() @@ -82,7 +83,7 @@ public function create() try { $result = parent::create(); - } catch (\Exception $e) { + } catch (Exception $e) { $this->_removeDbBackup(); throw $e; } diff --git a/lib/internal/Magento/Framework/Backup/SourceFileInterface.php b/lib/internal/Magento/Framework/Backup/SourceFileInterface.php new file mode 100644 index 0000000000000..bcc1d18686741 --- /dev/null +++ b/lib/internal/Magento/Framework/Backup/SourceFileInterface.php @@ -0,0 +1,32 @@ + + */ + +namespace Magento\Framework\Backup; + +interface SourceFileInterface +{ + + /** + * Check if keep files of backup + * + * @return bool + */ + public function keepSourceFile(); + + /** + * Set if keep files of backup + * + * @param bool $keepSourceFile + * @return $this + */ + public function setKeepSourceFile(bool $keepSourceFile); +} diff --git a/lib/internal/Magento/Framework/Setup/BackupRollback.php b/lib/internal/Magento/Framework/Setup/BackupRollback.php index c19b78101db16..568f0dfcabdea 100644 --- a/lib/internal/Magento/Framework/Setup/BackupRollback.php +++ b/lib/internal/Magento/Framework/Setup/BackupRollback.php @@ -7,13 +7,12 @@ namespace Magento\Framework\Setup; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Backup\Factory; use Magento\Framework\Backup\Exception\NotEnoughPermissions; +use Magento\Framework\Backup\Factory; use Magento\Framework\Backup\Filesystem; use Magento\Framework\Backup\Filesystem\Helper; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem\Driver\File; -use Magento\Framework\Filesystem\DriverInterface; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Phrase; @@ -129,7 +128,7 @@ public function codeBackup($time, $type = Factory::TYPE_FILESYSTEM) $this->log->log($granularType . ' backup is starting...'); $fsBackup->create(); $this->log->log( - $granularType. ' backup filename: ' . $fsBackup->getBackupFilename() + $granularType . ' backup filename: ' . $fsBackup->getBackupFilename() . ' (The archive can be uncompressed with 7-Zip on Windows systems)' ); $this->log->log($granularType . ' backup path: ' . $fsBackup->getBackupPath()); @@ -142,10 +141,11 @@ public function codeBackup($time, $type = Factory::TYPE_FILESYSTEM) * * @param string $rollbackFile * @param string $type + * @param boolean $keepSourceFile * @return void * @throws LocalizedException */ - public function codeRollback($rollbackFile, $type = Factory::TYPE_FILESYSTEM) + public function codeRollback($rollbackFile, $type = Factory::TYPE_FILESYSTEM, $keepSourceFile = false) { if (preg_match('/[0-9]_(filesystem)_(code|media)\.(tgz)$/', $rollbackFile) !== 1) { throw new LocalizedException(new Phrase('Invalid rollback file.')); @@ -181,6 +181,7 @@ public function codeRollback($rollbackFile, $type = Factory::TYPE_FILESYSTEM) $fsRollback->setBackupsDir($this->backupsDir); $fsRollback->setBackupExtension('tgz'); $time = explode('_', $rollbackFile); + $fsRollback->setKeepSourceFile($keepSourceFile); $fsRollback->setTime($time[0]); $this->log->log($granularType . ' rollback is starting ...'); $fsRollback->rollback(); @@ -218,10 +219,11 @@ public function dbBackup($time) * Roll back database * * @param string $rollbackFile + * @param boolean $keepSourceFile * @return void * @throws LocalizedException */ - public function dbRollback($rollbackFile) + public function dbRollback($rollbackFile, $keepSourceFile = false) { if (preg_match('/[0-9]_(db)(.*?).(sql)$/', $rollbackFile) !== 1) { throw new LocalizedException(new Phrase('Invalid rollback file.')); @@ -241,6 +243,7 @@ public function dbRollback($rollbackFile) } $dbRollback->setTime($time[0]); $this->log->log('DB rollback is starting...'); + $dbRollback->setKeepSourceFile($keepSourceFile); $dbRollback->setResourceModel($this->objectManager->create(\Magento\Backup\Model\ResourceModel\Db::class)); $dbRollback->rollback(); $this->log->log('DB rollback filename: ' . $dbRollback->getBackupFilename()); diff --git a/setup/src/Magento/Setup/Console/Command/RollbackCommand.php b/setup/src/Magento/Setup/Console/Command/RollbackCommand.php index 2d728e22fa140..26f0890ad49de 100644 --- a/setup/src/Magento/Setup/Console/Command/RollbackCommand.php +++ b/setup/src/Magento/Setup/Console/Command/RollbackCommand.php @@ -127,7 +127,13 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!$helper->ask($input, $output, $question) && $input->isInteractive()) { return \Magento\Framework\Console\Cli::RETURN_FAILURE; } - $this->doRollback($input, $output); + $questionKeep = new ConfirmationQuestion( + 'Do you want to keep the backups?[y/N]', + false + ); + $keepSourceFile = $helper->ask($input, $output, $questionKeep); + + $this->doRollback($input, $output, $keepSourceFile); $output->writeln('Please set file permission of bin/magento to executable'); } catch (\Exception $e) { $output->writeln('' . $e->getMessage() . ''); @@ -145,24 +151,33 @@ protected function execute(InputInterface $input, OutputInterface $output) * * @param InputInterface $input * @param OutputInterface $output + * @param boolean $keepSourceFile * @return void * @throws \InvalidArgumentException */ - private function doRollback(InputInterface $input, OutputInterface $output) + private function doRollback(InputInterface $input, OutputInterface $output, $keepSourceFile) { $inputOptionProvided = false; $rollbackHandler = $this->backupRollbackFactory->create($output); if ($input->getOption(self::INPUT_KEY_CODE_BACKUP_FILE)) { - $rollbackHandler->codeRollback($input->getOption(self::INPUT_KEY_CODE_BACKUP_FILE)); + $rollbackHandler->codeRollback( + $input->getOption(self::INPUT_KEY_CODE_BACKUP_FILE), + Factory::TYPE_FILESYSTEM, + $keepSourceFile + ); $inputOptionProvided = true; } if ($input->getOption(self::INPUT_KEY_MEDIA_BACKUP_FILE)) { - $rollbackHandler->codeRollback($input->getOption(self::INPUT_KEY_MEDIA_BACKUP_FILE), Factory::TYPE_MEDIA); + $rollbackHandler->codeRollback( + $input->getOption(self::INPUT_KEY_MEDIA_BACKUP_FILE), + Factory::TYPE_MEDIA, + $keepSourceFile + ); $inputOptionProvided = true; } if ($input->getOption(self::INPUT_KEY_DB_BACKUP_FILE)) { $this->setAreaCode(); - $rollbackHandler->dbRollback($input->getOption(self::INPUT_KEY_DB_BACKUP_FILE)); + $rollbackHandler->dbRollback($input->getOption(self::INPUT_KEY_DB_BACKUP_FILE), $keepSourceFile); $inputOptionProvided = true; } if (!$inputOptionProvided) { diff --git a/setup/src/Magento/Setup/Test/Unit/Console/Command/RollbackCommandTest.php b/setup/src/Magento/Setup/Test/Unit/Console/Command/RollbackCommandTest.php index 6c2e22a9a202c..41595ee19f43d 100644 --- a/setup/src/Magento/Setup/Test/Unit/Console/Command/RollbackCommandTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Console/Command/RollbackCommandTest.php @@ -165,7 +165,7 @@ public function testInteraction() ->method('isAvailable') ->will($this->returnValue(true)); $this->question - ->expects($this->once()) + ->expects($this->atLeast(2)) ->method('ask') ->will($this->returnValue(false)); $this->helperSet