-
Notifications
You must be signed in to change notification settings - Fork 58
/
DumpDatabase.php
224 lines (191 loc) · 5.98 KB
/
DumpDatabase.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* This software is a derived product, based on:
*
* Simple Machines Forum (SMF)
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
* license: BSD, See included LICENSE.TXT for terms and conditions.
*
* @version 1.0 Alpha
*
* This file has a single job - database backup.
*
*/
if (!defined('ELK'))
die('No access...');
/**
* Dumps the database.
* It writes all of the database to standard output.
* It uses gzip compression if compress is set in the URL/post data.
* It may possibly time out, and mess up badly if you were relying on it. :P
* The data dumped depends on whether "struct" and "data" are passed.
* It is called from ManageMaintenance.php.
*/
function DumpDatabase2()
{
global $db_name, $scripturl, $modSettings;
global $db_prefix, $db_show_debug;
// We'll need a db to dump :P
$database = database();
// We don't need debug when dumping the database
$modSettings['disableQueryCheck'] = true;
$db_show_debug = false;
// You can't dump nothing!
if (!isset($_REQUEST['struct']) && !isset($_REQUEST['data']))
$_REQUEST['data'] = true;
// Attempt to stop from dying...
@set_time_limit(600);
$time_limit = ini_get('max_execution_time');
$start_time = time();
// @todo ... fail on not getting the requested memory?
setMemoryLimit('256M');
$memory_limit = memoryReturnBytes(ini_get('memory_limit')) / 4;
$current_used_memory = 0;
$db_backup = '';
$output_function = 'un_compressed';
@ob_end_clean();
// Start saving the output... (don't do it otherwise for memory reasons.)
if (isset($_REQUEST['compress']) && function_exists('gzencode'))
{
$output_function = 'gzencode';
// Send faked headers so it will just save the compressed output as a gzip.
header('Content-Type: application/x-gzip');
header('Accept-Ranges: bytes');
header('Content-Encoding: none');
// Gecko browsers... don't like this. (Mozilla, Firefox, etc.)
if (!isBrowser('gecko'))
header('Content-Transfer-Encoding: binary');
// The file extension will include .gz...
$extension = '.sql.gz';
}
else
{
// Get rid of the gzipping alreading being done.
if (!empty($modSettings['enableCompressedOutput']))
@ob_end_clean();
// If we can, clean anything already sent from the output buffer...
elseif (ob_get_length() != 0)
ob_clean();
// Tell the client to save this file, even though it's text.
header('Content-Type: ' . (isBrowser('ie') || isBrowser('opera') ? 'application/octetstream' : 'application/octet-stream'));
header('Content-Encoding: none');
// This time the extension should just be .sql.
$extension = '.sql';
}
// This should turn off the session URL parser.
$scripturl = '';
// If this database is flat file and has a handler function pass it to that.
if (method_exists($database, 'db_get_backup'))
{
$database->db_get_backup();
exit;
}
// Send the proper headers to let them download this file.
header('Content-Disposition: attachment; filename="' . $db_name . '-' . (empty($_REQUEST['struct']) ? 'data' : (empty($_REQUEST['data']) ? 'structure' : 'complete')) . '_' . strftime('%Y-%m-%d') . $extension . '"');
header('Cache-Control: private');
header('Connection: close');
// This makes things simpler when using it so very very often.
$crlf = "\r\n";
// SQL Dump Header.
$db_chunks =
'-- ==========================================================' . $crlf .
'--' . $crlf .
'-- Database dump of tables in `' . $db_name . '`' . $crlf .
'-- ' . standardTime(time(), false) . $crlf .
'--' . $crlf .
'-- ==========================================================' . $crlf .
$crlf;
// Get all tables in the database....
if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) != 0)
$dbp = str_replace('_', '\_', $match[2]);
else
$dbp = $db_prefix;
// Dump each table.
$tables = $database->db_list_tables(false, $db_prefix . '%');
foreach ($tables as $tableName)
{
// Are we dumping the structures?
if (isset($_REQUEST['struct']))
{
$db_chunks .=
$crlf .
'--' . $crlf .
'-- Table structure for table `' . $tableName . '`' . $crlf .
'--' . $crlf .
$crlf .
$database->db_table_sql($tableName) . ';' . $crlf;
}
else
// This is needed to speedup things later
$database->db_table_sql($tableName);
// How about the data?
if (!isset($_REQUEST['data']) || substr($tableName, -10) == 'log_errors')
continue;
$first_round = true;
$close_table = false;
// Are there any rows in this table?
while ($get_rows = $database->insert_sql($tableName, $first_round))
{
if (empty($get_rows))
break;
// Time is what we need here!
if (function_exists('apache_reset_timeout'))
@apache_reset_timeout();
elseif (!empty($time_limit) && ($start_time + $time_limit - 20 > time()))
{
$start_time = time();
@set_time_limit(150);
}
// for the first pass, start the output with a custom line...
if ($first_round)
{
$db_chunks .=
$crlf .
'--' . $crlf .
'-- Dumping data in `' . $tableName . '`' . $crlf .
'--' . $crlf .
$crlf;
$first_round = false;
}
$db_chunks .=
$get_rows;
$current_used_memory += Util::strlen($db_chunks);
$db_backup .= $db_chunks;
unset($db_chunks);
$db_chunks = '';
if ($current_used_memory > $memory_limit)
{
echo $output_function($db_backup);
$current_used_memory = 0;
// This is probably redundant
unset($db_backup);
$db_backup = '';
}
$close_table = true;
}
// No rows to get - skip it.
if ($close_table)
$db_backup .=
'-- --------------------------------------------------------' . $crlf;
}
// write the last line
$db_backup .=
$crlf .
'-- Done' . $crlf;
echo $output_function($db_backup);
exit;
}
/**
* Dummy/helper function, it simply returns the string passed as argument
*
* @param $string, string to uncompress
* @return the string passed
*/
function un_compressed($string = '')
{
return $string;
}