Db.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Backup;
  7. use Magento\Framework\Archive;
  8. use Magento\Framework\Backup\Db\BackupFactory;
  9. use Magento\Framework\Backup\Filesystem\Iterator\File;
  10. /**
  11. * Class to work with database backups
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Db extends AbstractBackup
  18. {
  19. /**
  20. * @var BackupFactory
  21. */
  22. protected $_backupFactory;
  23. /**
  24. * @param BackupFactory $backupFactory
  25. */
  26. public function __construct(BackupFactory $backupFactory)
  27. {
  28. $this->_backupFactory = $backupFactory;
  29. }
  30. /**
  31. * Implements Rollback functionality for Db
  32. *
  33. * @return bool
  34. */
  35. public function rollback()
  36. {
  37. set_time_limit(0);
  38. ignore_user_abort(true);
  39. $this->_lastOperationSucceed = false;
  40. $archiveManager = new Archive();
  41. $source = $archiveManager->unpack($this->getBackupPath(), $this->getBackupsDir());
  42. $file = new File($source);
  43. foreach ($file as $statement) {
  44. $this->getResourceModel()->runCommand($statement);
  45. }
  46. if ($this->keepSourceFile() === false) {
  47. @unlink($source);
  48. }
  49. $this->_lastOperationSucceed = true;
  50. return true;
  51. }
  52. /**
  53. * Checks whether the line is last in sql command
  54. *
  55. * @param string $line
  56. * @return bool
  57. */
  58. protected function _isLineLastInCommand($line)
  59. {
  60. $cleanLine = trim($line);
  61. $lineLength = strlen($cleanLine);
  62. $returnResult = false;
  63. if ($lineLength > 0) {
  64. $lastSymbolIndex = $lineLength - 1;
  65. if ($cleanLine[$lastSymbolIndex] == ';') {
  66. $returnResult = true;
  67. }
  68. }
  69. return $returnResult;
  70. }
  71. /**
  72. * Implements Create Backup functionality for Db
  73. *
  74. * @return bool
  75. */
  76. public function create()
  77. {
  78. set_time_limit(0);
  79. ignore_user_abort(true);
  80. $this->_lastOperationSucceed = false;
  81. $backup = $this->_backupFactory->createBackupModel()->setTime(
  82. $this->getTime()
  83. )->setType(
  84. $this->getType()
  85. )->setPath(
  86. $this->getBackupsDir()
  87. )->setName(
  88. $this->getName()
  89. );
  90. $backupDb = $this->_backupFactory->createBackupDbModel();
  91. $backupDb->createBackup($backup);
  92. $this->_lastOperationSucceed = true;
  93. return true;
  94. }
  95. /**
  96. * Get database size
  97. *
  98. * @return int
  99. */
  100. public function getDBSize()
  101. {
  102. $backupDb = $this->_backupFactory->createBackupDbModel();
  103. return $backupDb->getDBBackupSize();
  104. }
  105. /**
  106. * Get Backup Type
  107. *
  108. * @return string
  109. */
  110. public function getType()
  111. {
  112. return 'db';
  113. }
  114. }