Skip to content

Commit 739a9b6

Browse files
author
Oleksii Korshenko
authored
MAGETWO-83409: Add option to keep Files in Rollback #11750
2 parents aa950c8 + 7a83b12 commit 739a9b6

File tree

16 files changed

+262
-122
lines changed

16 files changed

+262
-122
lines changed

lib/internal/Magento/Framework/Backup/AbstractBackup.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
*/
66
namespace Magento\Framework\Backup;
77

8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\Phrase;
10+
811
/**
912
* Class to work with archives
1013
*
1114
* @api
1215
*/
13-
abstract class AbstractBackup implements BackupInterface
16+
abstract class AbstractBackup implements BackupInterface, SourceFileInterface
1417
{
1518
/**
1619
* Backup name
@@ -68,6 +71,13 @@ abstract class AbstractBackup implements BackupInterface
6871
*/
6972
protected $_lastErrorMessage;
7073

74+
/**
75+
* Keep Source files in Backup
76+
*
77+
* @var boolean
78+
*/
79+
private $keepSourceFile;
80+
7181
/**
7282
* Set Backup Extension
7383
*
@@ -138,14 +148,14 @@ public function getTime()
138148
* Set root directory of Magento installation
139149
*
140150
* @param string $rootDir
141-
* @throws \Magento\Framework\Exception\LocalizedException
151+
* @throws LocalizedException
142152
* @return $this
143153
*/
144154
public function setRootDir($rootDir)
145155
{
146156
if (!is_dir($rootDir)) {
147-
throw new \Magento\Framework\Exception\LocalizedException(
148-
new \Magento\Framework\Phrase('Bad root directory')
157+
throw new LocalizedException(
158+
new Phrase('Bad root directory')
149159
);
150160
}
151161

@@ -296,4 +306,26 @@ protected function _filterName($name)
296306

297307
return $name;
298308
}
309+
310+
/**
311+
* Check if keep files of backup
312+
*
313+
* @return bool
314+
*/
315+
public function keepSourceFile()
316+
{
317+
return $this->keepSourceFile;
318+
}
319+
320+
/**
321+
* Set if keep files of backup
322+
*
323+
* @param bool $keepSourceFile
324+
* @return $this
325+
*/
326+
public function setKeepSourceFile(bool $keepSourceFile)
327+
{
328+
$this->keepSourceFile = $keepSourceFile;
329+
return $this;
330+
}
299331
}

lib/internal/Magento/Framework/Backup/Archive/Tar.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
*/
1212
namespace Magento\Framework\Backup\Archive;
1313

14+
use Magento\Framework\Backup\Filesystem\Iterator\Filter;
15+
use RecursiveDirectoryIterator;
16+
use RecursiveIteratorIterator;
17+
1418
class Tar extends \Magento\Framework\Archive\Tar
1519
{
1620
/**
@@ -35,12 +39,12 @@ protected function _createTar($skipRoot = false, $finalize = false)
3539
{
3640
$path = $this->_getCurrentFile();
3741

38-
$filesystemIterator = new \RecursiveIteratorIterator(
39-
new \RecursiveDirectoryIterator($path),
40-
\RecursiveIteratorIterator::SELF_FIRST
42+
$filesystemIterator = new RecursiveIteratorIterator(
43+
new RecursiveDirectoryIterator($path),
44+
RecursiveIteratorIterator::SELF_FIRST
4145
);
4246

43-
$iterator = new \Magento\Framework\Backup\Filesystem\Iterator\Filter(
47+
$iterator = new Filter(
4448
$filesystemIterator,
4549
$this->_skipFiles
4650
);

lib/internal/Magento/Framework/Backup/Db.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Framework\Backup;
77

8+
use Magento\Framework\Archive;
9+
use Magento\Framework\Backup\Db\BackupFactory;
10+
use Magento\Framework\Backup\Filesystem\Iterator\File;
11+
812
/**
913
* Class to work with database backups
1014
*
@@ -14,14 +18,14 @@
1418
class Db extends AbstractBackup
1519
{
1620
/**
17-
* @var \Magento\Framework\Backup\Db\BackupFactory
21+
* @var BackupFactory
1822
*/
1923
protected $_backupFactory;
2024

2125
/**
22-
* @param \Magento\Framework\Backup\Db\BackupFactory $backupFactory
26+
* @param BackupFactory $backupFactory
2327
*/
24-
public function __construct(\Magento\Framework\Backup\Db\BackupFactory $backupFactory)
28+
public function __construct(BackupFactory $backupFactory)
2529
{
2630
$this->_backupFactory = $backupFactory;
2731
}
@@ -38,14 +42,16 @@ public function rollback()
3842

3943
$this->_lastOperationSucceed = false;
4044

41-
$archiveManager = new \Magento\Framework\Archive();
45+
$archiveManager = new Archive();
4246
$source = $archiveManager->unpack($this->getBackupPath(), $this->getBackupsDir());
4347

44-
$file = new \Magento\Framework\Backup\Filesystem\Iterator\File($source);
48+
$file = new File($source);
4549
foreach ($file as $statement) {
4650
$this->getResourceModel()->runCommand($statement);
4751
}
48-
@unlink($source);
52+
if ($this->keepSourceFile() === false) {
53+
@unlink($source);
54+
}
4955

5056
$this->_lastOperationSucceed = true;
5157

lib/internal/Magento/Framework/Backup/Db/BackupFactory.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Framework\Backup\Db;
88

9+
use Magento\Framework\ObjectManagerInterface;
10+
911
/**
1012
* @api
1113
*/
@@ -14,33 +16,33 @@ class BackupFactory
1416
/**
1517
* Object manager
1618
*
17-
* @var \Magento\Framework\ObjectManagerInterface
19+
* @var ObjectManagerInterface
1820
*/
19-
private $_objectManager;
21+
private $objectManager;
2022

2123
/**
2224
* @var string
2325
*/
24-
private $_backupInstanceName;
26+
private $backupInstanceName;
2527

2628
/**
2729
* @var string
2830
*/
29-
private $_backupDbInstanceName;
31+
private $backupDbInstanceName;
3032

3133
/**
32-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
34+
* @param ObjectManagerInterface $objectManager
3335
* @param string $backupInstanceName
3436
* @param string $backupDbInstanceName
3537
*/
3638
public function __construct(
37-
\Magento\Framework\ObjectManagerInterface $objectManager,
39+
ObjectManagerInterface $objectManager,
3840
$backupInstanceName,
3941
$backupDbInstanceName
4042
) {
41-
$this->_objectManager = $objectManager;
42-
$this->_backupInstanceName = $backupInstanceName;
43-
$this->_backupDbInstanceName = $backupDbInstanceName;
43+
$this->objectManager = $objectManager;
44+
$this->backupInstanceName = $backupInstanceName;
45+
$this->backupDbInstanceName = $backupDbInstanceName;
4446
}
4547

4648
/**
@@ -51,7 +53,7 @@ public function __construct(
5153
*/
5254
public function createBackupModel(array $arguments = [])
5355
{
54-
return $this->_objectManager->create($this->_backupInstanceName, $arguments);
56+
return $this->objectManager->create($this->backupInstanceName, $arguments);
5557
}
5658

5759
/**
@@ -62,6 +64,6 @@ public function createBackupModel(array $arguments = [])
6264
*/
6365
public function createBackupDbModel(array $arguments = [])
6466
{
65-
return $this->_objectManager->create($this->_backupDbInstanceName, $arguments);
67+
return $this->objectManager->create($this->backupDbInstanceName, $arguments);
6668
}
6769
}

lib/internal/Magento/Framework/Backup/Factory.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*/
1010
namespace Magento\Framework\Backup;
1111

12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Framework\Phrase;
15+
1216
/**
1317
* @api
1418
*/
@@ -17,7 +21,7 @@ class Factory
1721
/**
1822
* Object manager
1923
*
20-
* @var \Magento\Framework\ObjectManagerInterface
24+
* @var ObjectManagerInterface
2125
*/
2226
private $_objectManager;
2327

@@ -54,9 +58,9 @@ class Factory
5458
protected $_allowedTypes;
5559

5660
/**
57-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
61+
* @param ObjectManagerInterface $objectManager
5862
*/
59-
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
63+
public function __construct(ObjectManagerInterface $objectManager)
6064
{
6165
$this->_objectManager = $objectManager;
6266
$this->_allowedTypes = [
@@ -73,13 +77,13 @@ public function __construct(\Magento\Framework\ObjectManagerInterface $objectMan
7377
*
7478
* @param string $type
7579
* @return BackupInterface
76-
* @throws \Magento\Framework\Exception\LocalizedException
80+
* @throws LocalizedException
7781
*/
7882
public function create($type)
7983
{
8084
if (!in_array($type, $this->_allowedTypes)) {
81-
throw new \Magento\Framework\Exception\LocalizedException(
82-
new \Magento\Framework\Phrase(
85+
throw new LocalizedException(
86+
new Phrase(
8387
'Current implementation not supported this type (%1) of backup.',
8488
[$type]
8589
)

0 commit comments

Comments
 (0)