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