Snapshot.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Class to work with full filesystem and database backups
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Backup;
  12. use Exception;
  13. use Magento\Framework\App\Filesystem\DirectoryList;
  14. use Magento\Framework\Filesystem as AppFilesystem;
  15. class Snapshot extends Filesystem
  16. {
  17. /**
  18. * Database backup manager
  19. *
  20. * @var Db
  21. */
  22. protected $_dbBackupManager;
  23. /**
  24. * Filesystem facade
  25. *
  26. * @var AppFilesystem
  27. */
  28. protected $_filesystem;
  29. /**
  30. * @var Factory
  31. */
  32. protected $_backupFactory;
  33. /**
  34. * @param AppFilesystem $filesystem
  35. * @param Factory $backupFactory
  36. */
  37. public function __construct(AppFilesystem $filesystem, Factory $backupFactory)
  38. {
  39. $this->_filesystem = $filesystem;
  40. $this->_backupFactory = $backupFactory;
  41. }
  42. /**
  43. * Implementation Rollback functionality for Snapshot
  44. *
  45. * @throws Exception
  46. * @return bool
  47. */
  48. public function rollback()
  49. {
  50. $result = parent::rollback();
  51. $this->_lastOperationSucceed = false;
  52. try {
  53. $this->_getDbBackupManager()->rollback();
  54. } catch (Exception $e) {
  55. $this->_removeDbBackup();
  56. throw $e;
  57. }
  58. $this->_removeDbBackup();
  59. $this->_lastOperationSucceed = true;
  60. return $result;
  61. }
  62. /**
  63. * Implementation Create Backup functionality for Snapshot
  64. *
  65. * @throws Exception
  66. * @return bool
  67. */
  68. public function create()
  69. {
  70. $this->_getDbBackupManager()->create();
  71. try {
  72. $result = parent::create();
  73. } catch (Exception $e) {
  74. $this->_removeDbBackup();
  75. throw $e;
  76. }
  77. $this->_lastOperationSucceed = false;
  78. $this->_removeDbBackup();
  79. $this->_lastOperationSucceed = true;
  80. return $result;
  81. }
  82. /**
  83. * Overlap getType
  84. *
  85. * @return string
  86. * @see BackupInterface::getType()
  87. */
  88. public function getType()
  89. {
  90. return 'snapshot';
  91. }
  92. /**
  93. * Create Db Instance
  94. *
  95. * @return BackupInterface
  96. */
  97. protected function _createDbBackupInstance()
  98. {
  99. return $this->_backupFactory->create(Factory::TYPE_DB)
  100. ->setBackupExtension('sql')
  101. ->setTime($this->getTime())
  102. ->setBackupsDir($this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->getAbsolutePath())
  103. ->setResourceModel($this->getResourceModel());
  104. }
  105. /**
  106. * Get database backup manager
  107. *
  108. * @return Db
  109. */
  110. protected function _getDbBackupManager()
  111. {
  112. if ($this->_dbBackupManager === null) {
  113. $this->_dbBackupManager = $this->_createDbBackupInstance();
  114. }
  115. return $this->_dbBackupManager;
  116. }
  117. /**
  118. * Set Db backup manager
  119. *
  120. * @param AbstractBackup $manager
  121. * @return $this
  122. */
  123. public function setDbBackupManager(AbstractBackup $manager)
  124. {
  125. $this->_dbBackupManager = $manager;
  126. return $this;
  127. }
  128. /**
  129. * Get Db Backup Filename
  130. *
  131. * @return string
  132. */
  133. public function getDbBackupFilename()
  134. {
  135. return $this->_getDbBackupManager()->getBackupFilename();
  136. }
  137. /**
  138. * Remove Db backup after added it to the snapshot
  139. *
  140. * @return $this
  141. */
  142. protected function _removeDbBackup()
  143. {
  144. @unlink($this->_getDbBackupManager()->getBackupPath());
  145. return $this;
  146. }
  147. }