1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Backup
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Database backup model
30: *
31: * @category Mage
32: * @package Mage_Backup
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Backup_Model_Db
36: {
37:
38: /**
39: * Buffer length for multi rows
40: * default 100 Kb
41: *
42: */
43: const BUFFER_LENGTH = 102400;
44:
45: /**
46: * List of tables which data should not be backed up
47: *
48: * @var array
49: */
50: protected $_ignoreDataTablesList = array(
51: 'importexport/importdata'
52: );
53:
54: /**
55: * Retrieve resource model
56: *
57: * @return Mage_Backup_Model_Mysql4_Db
58: */
59: public function getResource()
60: {
61: return Mage::getResourceSingleton('backup/db');
62: }
63:
64: public function getTables()
65: {
66: return $this->getResource()->getTables();
67: }
68:
69: public function getTableCreateScript($tableName, $addDropIfExists=false)
70: {
71: return $this->getResource()->getTableCreateScript($tableName, $addDropIfExists);
72: }
73:
74: public function getTableDataDump($tableName)
75: {
76: return $this->getResource()->getTableDataDump($tableName);
77: }
78:
79: public function getHeader()
80: {
81: return $this->getResource()->getHeader();
82: }
83:
84: public function getFooter()
85: {
86: return $this->getResource()->getFooter();
87: }
88:
89: public function renderSql()
90: {
91: ini_set('max_execution_time', 0);
92: $sql = $this->getHeader();
93:
94: $tables = $this->getTables();
95: foreach ($tables as $tableName) {
96: $sql.= $this->getTableCreateScript($tableName, true);
97: $sql.= $this->getTableDataDump($tableName);
98: }
99:
100: $sql.= $this->getFooter();
101: return $sql;
102: }
103:
104: /**
105: * Create backup and stream write to adapter
106: *
107: * @param Mage_Backup_Model_Backup $backup
108: * @return Mage_Backup_Model_Db
109: */
110: public function createBackup(Mage_Backup_Model_Backup $backup)
111: {
112: $backup->open(true);
113:
114: $this->getResource()->beginTransaction();
115:
116: $tables = $this->getResource()->getTables();
117:
118: $backup->write($this->getResource()->getHeader());
119:
120: $ignoreDataTablesList = $this->getIgnoreDataTablesList();
121:
122: foreach ($tables as $table) {
123: $backup->write($this->getResource()->getTableHeader($table)
124: . $this->getResource()->getTableDropSql($table) . "\n");
125: $backup->write($this->getResource()->getTableCreateSql($table, false) . "\n");
126:
127: $tableStatus = $this->getResource()->getTableStatus($table);
128:
129: if ($tableStatus->getRows() && !in_array($table, $ignoreDataTablesList)) {
130: $backup->write($this->getResource()->getTableDataBeforeSql($table));
131:
132: if ($tableStatus->getDataLength() > self::BUFFER_LENGTH) {
133: if ($tableStatus->getAvgRowLength() < self::BUFFER_LENGTH) {
134: $limit = floor(self::BUFFER_LENGTH / $tableStatus->getAvgRowLength());
135: $multiRowsLength = ceil($tableStatus->getRows() / $limit);
136: }
137: else {
138: $limit = 1;
139: $multiRowsLength = $tableStatus->getRows();
140: }
141: }
142: else {
143: $limit = $tableStatus->getRows();
144: $multiRowsLength = 1;
145: }
146:
147: for ($i = 0; $i < $multiRowsLength; $i ++) {
148: $backup->write($this->getResource()->getTableDataSql($table, $limit, $i*$limit));
149: }
150:
151: $backup->write($this->getResource()->getTableDataAfterSql($table));
152: }
153: }
154: $backup->write($this->getResource()->getTableForeignKeysSql());
155: $backup->write($this->getResource()->getFooter());
156:
157: $this->getResource()->commitTransaction();
158:
159: $backup->close();
160:
161: return $this;
162: }
163:
164: /**.
165: * Returns the list of tables which data should not be backed up
166: *
167: * @return array
168: */
169: public function getIgnoreDataTablesList()
170: {
171: $result = array();
172: $resource = Mage::getSingleton('core/resource');
173:
174: foreach ($this->_ignoreDataTablesList as $table) {
175: $result[] = $resource->getTableName($table);
176: }
177:
178: return $result;
179: }
180: }
181: