|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* class-xcloner-database.php |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2017 Ovidiu Liuta <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program; if not, write to the Free Software |
|
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
20
|
|
|
* MA 02110-1301, USA. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class Xcloner_Database extends wpdb { |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
public $debug = 0; |
|
28
|
|
|
public $recordsPerSession = 10000; |
|
29
|
|
|
public $dbCompatibility = ""; |
|
30
|
|
|
public $dbDropSyntax = 1; |
|
31
|
|
|
public $countRecords = 0; |
|
32
|
|
|
|
|
33
|
|
|
private $link; |
|
|
|
|
|
|
34
|
|
|
private $db_selected; |
|
|
|
|
|
|
35
|
|
|
private $logger; |
|
36
|
|
|
private $xcloner_settings; |
|
37
|
|
|
private $fs; |
|
38
|
|
|
|
|
39
|
|
|
private $TEMP_DBPROCESS_FILE = ".database"; |
|
40
|
|
|
private $TEMP_DUMP_FILE = "database-backup.sql"; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(Xcloner $xcloner_container, $wp_user = "", $wp_pass = "", $wp_db = "", $wp_host = "") |
|
43
|
|
|
{ |
|
44
|
|
|
$this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_database"); |
|
45
|
|
|
$this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
|
46
|
|
|
$this->fs = $xcloner_container->get_xcloner_filesystem(); |
|
47
|
|
|
|
|
48
|
|
|
if ($this->xcloner_settings->get_xcloner_option('xcloner_database_records_per_request')) |
|
|
|
|
|
|
49
|
|
|
$this->recordsPerSession = $this->xcloner_settings->get_xcloner_option('xcloner_database_records_per_request'); |
|
50
|
|
|
|
|
51
|
|
|
if (!$this->recordsPerSession) |
|
52
|
|
|
$this->recordsPerSession = 100; |
|
53
|
|
|
|
|
54
|
|
|
if (!$wp_user && !$wp_pass && !$wp_host && !$wp_db) |
|
55
|
|
|
{ |
|
56
|
|
|
$wp_host = $this->xcloner_settings->get_db_hostname(); |
|
57
|
|
|
$wp_user = $this->xcloner_settings->get_db_username(); |
|
58
|
|
|
$wp_pass = $this->xcloner_settings->get_db_password(); |
|
59
|
|
|
$wp_db = $this->xcloner_settings->get_db_database(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
parent::__construct($wp_user, $wp_pass, $wp_db, $wp_host); |
|
63
|
|
|
|
|
64
|
|
|
//$this->use_mysqli = true; |
|
65
|
|
|
} |
|
66
|
|
|
/* |
|
67
|
|
|
* Initialize the database connection |
|
68
|
|
|
* |
|
69
|
|
|
* name: init |
|
70
|
|
|
* @param array $data {'dbHostname', 'dbUsername', 'dbPassword', 'dbDatabase'} |
|
71
|
|
|
* @return |
|
72
|
|
|
*/ |
|
73
|
|
|
public function init($data, $start = 0) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($start and $this->fs->get_tmp_filesystem()->has($this->TEMP_DBPROCESS_FILE)) { |
|
|
|
|
|
|
76
|
|
|
$this->fs->get_tmp_filesystem()->delete($this->TEMP_DBPROCESS_FILE); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->headers(); |
|
80
|
|
|
|
|
81
|
|
|
$this->suppress_errors = true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function start_database_recursion($params, $extra_params, $init = 0) |
|
85
|
|
|
{ |
|
86
|
|
|
$tables = array(); |
|
|
|
|
|
|
87
|
|
|
$return['finished'] = 0; |
|
|
|
|
|
|
88
|
|
|
$return['stats'] = array( |
|
89
|
|
|
"total_records"=>0, |
|
90
|
|
|
"tables_count"=>0, |
|
91
|
|
|
"database_count"=>0, |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
if (!$this->xcloner_settings->get_enable_mysql_backup()) |
|
95
|
|
|
{ |
|
96
|
|
|
$return['finished'] = 1; |
|
97
|
|
|
return $return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->logger->debug(__("Starting database backup process")); |
|
101
|
|
|
|
|
102
|
|
|
$this->init($params, $init); |
|
103
|
|
|
|
|
104
|
|
|
if ($init) |
|
105
|
|
|
{ |
|
106
|
|
|
$db_count = 0; |
|
107
|
|
|
|
|
108
|
|
|
if (isset($params['#'])) |
|
109
|
|
|
{ |
|
110
|
|
|
foreach ($params['#'] as $database) |
|
111
|
|
|
{ |
|
112
|
|
|
if (!isset($params[$database]) or !is_array($params[$database])) |
|
113
|
|
|
$params[$database] = array(); |
|
114
|
|
|
} |
|
115
|
|
|
$db_count = -1; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (isset($params) and is_array($params)) |
|
119
|
|
|
foreach ($params as $database=>$tables) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($database != "#") |
|
122
|
|
|
{ |
|
123
|
|
|
$stats = $this->write_backup_process_list($database, $tables); |
|
124
|
|
|
$return['stats']['tables_count'] += $stats['tables_count']; |
|
125
|
|
|
$return['stats']['total_records'] += $stats['total_records']; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (sizeof($params)) |
|
130
|
|
|
$return['stats']['database_count'] = sizeof($params) + $db_count; |
|
131
|
|
|
else |
|
132
|
|
|
$return['stats']['database_count'] = 0; |
|
133
|
|
|
|
|
134
|
|
|
return $return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (!isset($extra_params['startAtLine'])) |
|
138
|
|
|
$extra_params['startAtLine'] = 0; |
|
139
|
|
|
if (!isset($extra_params['startAtRecord'])) |
|
140
|
|
|
$extra_params['startAtRecord'] = 0; |
|
141
|
|
|
if (!isset($extra_params['dumpfile'])) |
|
142
|
|
|
$extra_params['dumpfile'] = ""; |
|
143
|
|
|
|
|
144
|
|
|
$return = $this->process_incremental($extra_params['startAtLine'], $extra_params['startAtRecord'], $extra_params['dumpfile']); |
|
145
|
|
|
|
|
146
|
|
|
return $return; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function log($message = "") |
|
150
|
|
|
{ |
|
151
|
|
|
|
|
152
|
|
|
if ($message) { |
|
153
|
|
|
$this->logger->info($message, array("")); |
|
154
|
|
|
} else { |
|
155
|
|
|
if ($this->last_query) |
|
|
|
|
|
|
156
|
|
|
$this->logger->debug($this->last_query, array("")); |
|
157
|
|
|
if ($this->last_error) |
|
158
|
|
|
$this->logger->error($this->last_error, array("")); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/* |
|
165
|
|
|
*Return any error |
|
166
|
|
|
* |
|
167
|
|
|
* name: error |
|
168
|
|
|
* @param string $message |
|
169
|
|
|
* @return |
|
170
|
|
|
*/ |
|
171
|
|
|
public function error($message) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->logger->error($message, array("")); |
|
174
|
|
|
|
|
175
|
|
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/* |
|
179
|
|
|
* Send some special headers after the connection is initialized |
|
180
|
|
|
* |
|
181
|
|
|
* name: headers |
|
182
|
|
|
* @param |
|
183
|
|
|
*/ |
|
184
|
|
|
private function headers() |
|
185
|
|
|
{ |
|
186
|
|
|
$this->logger->debug(__("Setting mysql headers")); |
|
187
|
|
|
|
|
188
|
|
|
$this->query("SET SQL_QUOTE_SHOW_CREATE=1;"); |
|
189
|
|
|
//$this->log(); |
|
190
|
|
|
$this->query("SET sql_mode = 0;"); |
|
191
|
|
|
//$this->log(); |
|
192
|
|
|
|
|
193
|
|
|
$this->set_charset($this->dbh, 'utf8'); |
|
|
|
|
|
|
194
|
|
|
//$this->log(); |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function get_database_num_tables($database) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->logger->debug(sprintf(__("Getting number of tables in %s"), $database)); |
|
202
|
|
|
|
|
203
|
|
|
$query = "show tables in `".$database."`"; |
|
204
|
|
|
|
|
205
|
|
|
$res = $this->get_results($query); |
|
206
|
|
|
$this->log(); |
|
207
|
|
|
|
|
208
|
|
|
return count($res); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function get_all_databases() |
|
212
|
|
|
{ |
|
213
|
|
|
$this->logger->debug(("Getting all databases")); |
|
214
|
|
|
|
|
215
|
|
|
$query = "SHOW DATABASES;"; |
|
216
|
|
|
|
|
217
|
|
|
$databases = $this->get_results($query); |
|
218
|
|
|
$this->log(); |
|
219
|
|
|
|
|
220
|
|
|
$databases_list = array(); |
|
221
|
|
|
|
|
222
|
|
|
$i = 0; |
|
223
|
|
|
|
|
224
|
|
|
$databases_list[$i]['name'] = $this->dbname; |
|
225
|
|
|
$databases_list[$i]['num_tables'] = $this->get_database_num_tables($this->dbname); |
|
226
|
|
|
$i++; |
|
227
|
|
|
|
|
228
|
|
|
if (is_array($databases)) |
|
229
|
|
|
foreach ($databases as $db) { |
|
230
|
|
|
if ($db->Database != $this->dbname) |
|
231
|
|
|
{ |
|
232
|
|
|
$databases_list[$i]['name'] = $db->Database; |
|
233
|
|
|
$databases_list[$i]['num_tables'] = $this->get_database_num_tables($db->Database); |
|
234
|
|
|
$i++; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return $databases_list; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/* |
|
242
|
|
|
* Returns an array of tables from a database and mark $excluded ones |
|
243
|
|
|
* |
|
244
|
|
|
* name: list_tables |
|
245
|
|
|
* @param string $database |
|
246
|
|
|
* @param array $included |
|
247
|
|
|
* @param int $get_num_records |
|
248
|
|
|
* @return array $tablesList |
|
249
|
|
|
*/ |
|
250
|
|
|
public function list_tables($database = "", $included = array(), $get_num_records = 0) |
|
251
|
|
|
{ |
|
252
|
|
|
$tablesList[0] = array( ); |
|
|
|
|
|
|
253
|
|
|
$inc = 0; |
|
254
|
|
|
|
|
255
|
|
|
if (!$database) |
|
256
|
|
|
$database = $this->dbname; |
|
257
|
|
|
|
|
258
|
|
|
$this->logger->debug(sprintf(("Listing tables in %s database"), $database)); |
|
259
|
|
|
|
|
260
|
|
|
$tables = $this->get_results("SHOW TABLES in `".$database."`"); |
|
261
|
|
|
$this->log(); |
|
262
|
|
|
|
|
263
|
|
|
foreach ($tables as $table) { |
|
264
|
|
|
|
|
265
|
|
|
$table = array_values((array)$table)[0]; |
|
266
|
|
|
|
|
267
|
|
|
$tablesList[$inc]['name'] = $table; |
|
268
|
|
|
$tablesList[$inc]['database'] = $database; |
|
269
|
|
|
|
|
270
|
|
|
if ($get_num_records) |
|
271
|
|
|
{ |
|
272
|
|
|
$records_num_result = $this->get_var("SELECT count(*) FROM `".$database."`.`".$table."`"); |
|
273
|
|
|
$this->log(); |
|
274
|
|
|
|
|
275
|
|
|
$tablesList[$inc]['records'] = $records_num_result; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
$tablesList[$inc]['excluded'] = 0; |
|
279
|
|
|
|
|
280
|
|
|
if (sizeof($included) and is_array($included)) |
|
281
|
|
|
if (!in_array($table, $included)) |
|
282
|
|
|
{ |
|
283
|
|
|
$tablesList[$inc]['excluded'] = 1; |
|
284
|
|
|
$this->log(sprintf(__("Excluding table %s.%s from backup"), $table, $database)); |
|
285
|
|
|
} |
|
286
|
|
|
$inc++; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
return $tablesList; |
|
290
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
public function write_backup_process_list($dbname, $incl_tables) |
|
294
|
|
|
{ |
|
295
|
|
|
$return['total_records'] = 0; |
|
|
|
|
|
|
296
|
|
|
$return['tables_count'] = 0; |
|
297
|
|
|
|
|
298
|
|
|
$this->log(__("Preparing the database recursion file")); |
|
299
|
|
|
|
|
300
|
|
|
$tables = $this->list_tables($dbname, $incl_tables, 1); |
|
301
|
|
|
|
|
302
|
|
|
if ($this->dbname != $dbname) |
|
303
|
|
|
$dumpfile = $dbname."-backup.sql"; |
|
304
|
|
|
else |
|
305
|
|
|
$dumpfile = $this->TEMP_DUMP_FILE; |
|
306
|
|
|
|
|
307
|
|
|
$line = sprintf("###newdump###\t%s\t%s\n", $dbname, $dumpfile); |
|
308
|
|
|
$this->fs->get_tmp_filesystem_append()->write($this->TEMP_DBPROCESS_FILE, $line); |
|
309
|
|
|
|
|
310
|
|
|
// write this to the class and write to $TEMP_DBPROCESS_FILE file as database.table records |
|
311
|
|
|
foreach ($tables as $key=>$table) |
|
312
|
|
|
if ($table != "" and !$tables[$key]['excluded']) { |
|
313
|
|
|
|
|
314
|
|
|
$line = sprintf("`%s`.`%s`\t%s\t%s\n", $dbname, $tables[$key]['name'], $tables[$key]['records'], $tables[$key]['excluded']); |
|
315
|
|
|
$this->fs->get_tmp_filesystem_append()->write($this->TEMP_DBPROCESS_FILE, $line); |
|
316
|
|
|
$return['tables_count']++; |
|
317
|
|
|
$return['total_records'] += $tables[$key]['records']; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
$line = sprintf("###enddump###\t%s\t%s\n", $dbname, $dumpfile); |
|
321
|
|
|
$this->fs->get_tmp_filesystem_append()->write($this->TEMP_DBPROCESS_FILE, $line); |
|
322
|
|
|
|
|
323
|
|
|
return $return; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/* |
|
327
|
|
|
* Returns the number of records from a table |
|
328
|
|
|
* |
|
329
|
|
|
* name: countRecords |
|
330
|
|
|
* @param string $table - the source table |
|
331
|
|
|
* @return int $count |
|
332
|
|
|
*/ |
|
333
|
|
|
public function countRecords($table) |
|
334
|
|
|
{ |
|
335
|
|
|
|
|
336
|
|
|
$table = "`".$this->dbname."`.`$table`"; |
|
337
|
|
|
|
|
338
|
|
|
$result = $this->get_var("SELECT count(*) FROM $table;"); |
|
339
|
|
|
|
|
340
|
|
|
return intval($result) ;// not max limit on 32 bit systems 2147483647; on 64 bit 9223372036854775807 |
|
341
|
|
|
|
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/* |
|
345
|
|
|
* Processing the mysql backup incrementally |
|
346
|
|
|
* |
|
347
|
|
|
* name: processIncremental |
|
348
|
|
|
* @param |
|
349
|
|
|
* int $startAtLine - at which line from the perm.txt file to start reading |
|
350
|
|
|
* int startAtRecord - at which record to start from the table found at $startAtLine |
|
351
|
|
|
* string $dumpfie - where to save the data |
|
352
|
|
|
* string $dbCompatibility - MYSQL40, MYSQ32, none=default |
|
353
|
|
|
* int $dbDropSyntax - check if the DROP TABLE syntax should be added |
|
354
|
|
|
* @return array $return |
|
355
|
|
|
*/ |
|
356
|
|
|
public function process_incremental($startAtLine = 0, $startAtRecord = 0, $dumpfile = "", $dbCompatibility = "") { |
|
|
|
|
|
|
357
|
|
|
|
|
358
|
|
|
$count = 0; |
|
359
|
|
|
$return['finished'] = 0; |
|
|
|
|
|
|
360
|
|
|
$lines = array(); |
|
361
|
|
|
|
|
362
|
|
|
if ($this->fs->get_tmp_filesystem()->has($this->TEMP_DBPROCESS_FILE)) |
|
363
|
|
|
$lines = array_filter(explode("\n", $this->fs->get_tmp_filesystem()->read($this->TEMP_DBPROCESS_FILE))); |
|
364
|
|
|
|
|
365
|
|
|
foreach ($lines as $buffer) { |
|
366
|
|
|
|
|
367
|
|
|
if ($count == $startAtLine) |
|
368
|
|
|
{ |
|
369
|
|
|
|
|
370
|
|
|
$tableInfo = explode("\t", $buffer); |
|
371
|
|
|
|
|
372
|
|
|
if ($tableInfo[0] == "###newdump###") { |
|
373
|
|
|
// we create a new mysql dump file |
|
374
|
|
|
if ($dumpfile != "") { |
|
375
|
|
|
// we finished a previous one and write the footers |
|
376
|
|
|
$return['dumpsize'] = $this->data_footers($dumpfile); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
$dumpfile = $tableInfo[2]; |
|
380
|
|
|
|
|
381
|
|
|
$this->log(sprintf(__("Starting new backup dump to file %s"), $dumpfile)); |
|
382
|
|
|
|
|
383
|
|
|
$this->data_headers($dumpfile, $tableInfo[1]); |
|
384
|
|
|
$dumpfile = $tableInfo[2]; |
|
385
|
|
|
$startAtLine++; |
|
386
|
|
|
$return['new_dump'] = 1; |
|
387
|
|
|
//break; |
|
388
|
|
|
} else { |
|
389
|
|
|
//we export the table |
|
390
|
|
|
if ($tableInfo[0] == "###enddump###") |
|
391
|
|
|
$return['endDump'] = 1; |
|
392
|
|
|
|
|
393
|
|
|
//table is excluded |
|
394
|
|
|
if ($tableInfo[2]) |
|
395
|
|
|
continue; |
|
396
|
|
|
|
|
397
|
|
|
$next = $startAtRecord + $this->recordsPerSession; |
|
398
|
|
|
|
|
399
|
|
|
// $tableInfo[1] number of records in the table |
|
400
|
|
|
$table = explode("`.`", $tableInfo[0]); |
|
401
|
|
|
$tableName = str_replace("`", "", $table[1]); |
|
402
|
|
|
$databaseName = str_replace("`", "", $table[0]); |
|
403
|
|
|
|
|
404
|
|
|
//return something to the browser |
|
405
|
|
|
$return['databaseName'] = $databaseName; |
|
406
|
|
|
$return['tableName'] = $tableName; |
|
407
|
|
|
$return['totalRecords'] = $tableInfo[1]; |
|
408
|
|
|
|
|
409
|
|
|
$processed_records = 0; |
|
410
|
|
|
|
|
411
|
|
|
if (trim($tableName) != "" and !$tableInfo[2]) |
|
412
|
|
|
$processed_records = $this->export_table($databaseName, $tableName, $startAtRecord, $this->recordsPerSession, $dumpfile); |
|
413
|
|
|
|
|
414
|
|
|
$return['processedRecords'] = $startAtRecord + $processed_records; |
|
415
|
|
|
|
|
416
|
|
|
if ($next >= $tableInfo[1]) //we finished loading the records for next sessions, will go to the new record |
|
417
|
|
|
{ |
|
418
|
|
|
$startAtLine++; |
|
419
|
|
|
$startAtRecord = 0; |
|
420
|
|
|
} else { |
|
421
|
|
|
$startAtRecord = $startAtRecord + $this->recordsPerSession; |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
//$return['dbCompatibility'] = self::$dbCompatibility; |
|
425
|
|
|
|
|
426
|
|
|
$return['startAtLine'] = $startAtLine; |
|
427
|
|
|
$return['startAtRecord'] = $startAtRecord; |
|
428
|
|
|
$return['dumpfile'] = $dumpfile; |
|
429
|
|
|
$return['dumpsize'] = $this->fs->get_tmp_filesystem_append()->getSize($dumpfile); |
|
430
|
|
|
|
|
431
|
|
|
return $return; |
|
432
|
|
|
break; |
|
|
|
|
|
|
433
|
|
|
|
|
434
|
|
|
|
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
$count++; |
|
440
|
|
|
|
|
441
|
|
|
|
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
//while is finished, lets go home... |
|
445
|
|
|
if ($dumpfile != "") { |
|
446
|
|
|
// we finished a previous one and write the footers |
|
447
|
|
|
$return['dumpsize'] = $this->data_footers($dumpfile); |
|
448
|
|
|
$return['dumpfile'] = ($dumpfile); |
|
449
|
|
|
} |
|
450
|
|
|
$return['finished'] = 1; |
|
451
|
|
|
$return['startAtLine'] = $startAtLine; |
|
452
|
|
|
|
|
453
|
|
|
if ($this->fs->get_tmp_filesystem()->has($this->TEMP_DBPROCESS_FILE)) |
|
454
|
|
|
$this->fs->get_tmp_filesystem()->delete($this->TEMP_DBPROCESS_FILE); |
|
455
|
|
|
|
|
456
|
|
|
$this->logger->debug(sprintf(("Database backup finished!"))); |
|
457
|
|
|
|
|
458
|
|
|
return $return; |
|
459
|
|
|
|
|
460
|
|
|
|
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
|
|
464
|
|
|
/* |
|
465
|
|
|
* Exporting the table records |
|
466
|
|
|
* |
|
467
|
|
|
* name: exportTable |
|
468
|
|
|
* @param |
|
469
|
|
|
* string $databaseName - database name of the table |
|
470
|
|
|
* string tableName - table name |
|
471
|
|
|
* int $start - where to start from |
|
472
|
|
|
* int $limit - how many records |
|
473
|
|
|
* handler $fd - file handler where to write the records |
|
474
|
|
|
* @return |
|
475
|
|
|
*/ |
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
|
|
* @param integer $start |
|
479
|
|
|
* @param integer $limit |
|
480
|
|
|
*/ |
|
481
|
|
|
public function export_table($databaseName, $tableName, $start, $limit, $dumpfile) |
|
482
|
|
|
{ |
|
483
|
|
|
$this->logger->debug(sprintf(("Exporting table %s.%s data"), $databaseName, $tableName)); |
|
484
|
|
|
|
|
485
|
|
|
$records = 0; |
|
486
|
|
|
|
|
487
|
|
|
if ($start == 0) |
|
488
|
|
|
$this->dump_structure($databaseName, $tableName, $dumpfile); |
|
489
|
|
|
|
|
490
|
|
|
$start = intval($start); |
|
491
|
|
|
$limit = intval($limit); |
|
492
|
|
|
//exporting the table content now |
|
493
|
|
|
|
|
494
|
|
|
$query = "SELECT * from `$databaseName`.`$tableName` Limit $start, $limit ;"; |
|
495
|
|
|
if ($this->use_mysqli) |
|
|
|
|
|
|
496
|
|
|
{ |
|
497
|
|
|
$result = mysqli_query($this->dbh, $query); |
|
|
|
|
|
|
498
|
|
|
$mysql_fetch_function = "mysqli_fetch_array"; |
|
499
|
|
|
|
|
500
|
|
|
} else { |
|
501
|
|
|
$result = mysql_query($query, $this->dbh); |
|
|
|
|
|
|
502
|
|
|
$mysql_fetch_function = "mysqli_fetch_array"; |
|
503
|
|
|
} |
|
504
|
|
|
//$result = $this->get_results($query, ARRAY_N); |
|
505
|
|
|
//print_r($result); exit; |
|
506
|
|
|
|
|
507
|
|
|
if ($result) { |
|
508
|
|
|
while ($row = $mysql_fetch_function($result, MYSQLI_ASSOC)) { |
|
509
|
|
|
|
|
510
|
|
|
$this->fs->get_tmp_filesystem_append()->write($dumpfile, "INSERT INTO `$tableName` VALUES ("); |
|
511
|
|
|
$arr = $row; |
|
512
|
|
|
$buffer = ""; |
|
513
|
|
|
$this->countRecords++; |
|
514
|
|
|
|
|
515
|
|
|
foreach ($arr as $key => $value) { |
|
516
|
|
|
$value = $this->_real_escape($value); |
|
517
|
|
|
|
|
518
|
|
|
if (method_exists($this, 'remove_placeholder_escape')) { |
|
519
|
|
|
$value = $this->remove_placeholder_escape($value); |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
$buffer .= "'".$value."', "; |
|
523
|
|
|
} |
|
524
|
|
|
$buffer = rtrim($buffer, ', ').");\n"; |
|
525
|
|
|
$this->fs->get_tmp_filesystem_append()->write($dumpfile, $buffer); |
|
526
|
|
|
unset($buffer); |
|
527
|
|
|
|
|
528
|
|
|
$records++; |
|
529
|
|
|
|
|
530
|
|
|
} |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
$this->log(sprintf(__("Dumping %s records starting position %s from %s.%s table"), $records, $start, $databaseName, $tableName)); |
|
534
|
|
|
|
|
535
|
|
|
return $records; |
|
536
|
|
|
|
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
public function dump_structure($databaseName, $tableName, $dumpfile) |
|
540
|
|
|
{ |
|
541
|
|
|
$this->log(sprintf(__("Dumping the structure for %s.%s table"), $databaseName, $tableName)); |
|
542
|
|
|
|
|
543
|
|
|
$line = ("\n#\n# Table structure for table `$tableName`\n#\n\n"); |
|
544
|
|
|
$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line); |
|
545
|
|
|
|
|
546
|
|
|
if ($this->dbDropSyntax) |
|
547
|
|
|
{ |
|
548
|
|
|
$line = ("\nDROP table IF EXISTS `$tableName`;\n"); |
|
549
|
|
|
$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line); |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
//$result = mysqli_query($this->dbh,"SHOW CREATE table `$databaseName`.`$tableName`;"); |
|
553
|
|
|
$result = $this->get_row("SHOW CREATE table `$databaseName`.`$tableName`;", ARRAY_N); |
|
554
|
|
|
if ($result) { |
|
555
|
|
|
//$row = mysqli_fetch_row( $result); |
|
556
|
|
|
$line = ($result[1].";\n"); |
|
557
|
|
|
$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line); |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
$line = ("\n#\n# End Structure for table `$tableName`\n#\n\n"); |
|
561
|
|
|
$line .= ("#\n# Dumping data for table `$tableName`\n#\n\n"); |
|
562
|
|
|
$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line); |
|
563
|
|
|
|
|
564
|
|
|
return; |
|
565
|
|
|
|
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
public function data_footers($dumpfile) |
|
569
|
|
|
{ |
|
570
|
|
|
$this->logger->debug(sprintf(("Writing dump footers in file"), $dumpfile)); |
|
571
|
|
|
// we finished the dump file, not return the size of it |
|
572
|
|
|
$this->fs->get_tmp_filesystem_append()->write($dumpfile, "\n#\n# Finished at: ".date("M j, Y \a\\t H:i")."\n#"); |
|
573
|
|
|
$size = $this->fs->get_tmp_filesystem_append()->getSize($dumpfile); |
|
574
|
|
|
|
|
575
|
|
|
$metadata_dumpfile = $this->fs->get_tmp_filesystem()->getMetadata($dumpfile); |
|
576
|
|
|
|
|
577
|
|
|
//adding dump file to the included files list |
|
578
|
|
|
$this->fs->store_file($metadata_dumpfile, 'tmp_filesystem'); |
|
579
|
|
|
|
|
580
|
|
|
return $size; |
|
581
|
|
|
|
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
public function resetcountRecords() { |
|
585
|
|
|
|
|
586
|
|
|
$this->countRecords = 0; |
|
587
|
|
|
|
|
588
|
|
|
return $this->countRecords; |
|
589
|
|
|
|
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
public function getcountRecords() { |
|
593
|
|
|
|
|
594
|
|
|
return $this->countRecords; |
|
595
|
|
|
|
|
596
|
|
|
} |
|
597
|
|
|
|
|
598
|
|
|
|
|
599
|
|
|
public function data_headers($file, $database) |
|
600
|
|
|
{ |
|
601
|
|
|
$this->logger->debug(sprintf(("Writing dump header for %s database in file"), $database, $file)); |
|
602
|
|
|
|
|
603
|
|
|
$return = ""; |
|
604
|
|
|
|
|
605
|
|
|
$return .= "#\n"; |
|
606
|
|
|
$return .= "# Powered by XCloner Site Backup\n"; |
|
607
|
|
|
$return .= "# http://www.xcloner.com\n"; |
|
608
|
|
|
$return .= "#\n"; |
|
609
|
|
|
$return .= "# Host: ".get_site_url()."\n"; |
|
610
|
|
|
$return .= "# Generation Time: ".date("M j, Y \a\\t H:i")."\n"; |
|
611
|
|
|
$return .= "# PHP Version: ".phpversion()."\n"; |
|
612
|
|
|
$return .= "# Database Charset: ".$this->charset."\n"; |
|
613
|
|
|
|
|
614
|
|
|
$results = $this->get_results("SHOW VARIABLES LIKE \"%version%\";", ARRAY_N); |
|
615
|
|
|
if (isset($results)) { |
|
616
|
|
|
foreach ($results as $result) { |
|
617
|
|
|
|
|
618
|
|
|
$return .= "# MYSQL ".$result[0].": ".$result[1]."\n"; |
|
619
|
|
|
|
|
620
|
|
|
} |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
$results = $this->get_results("SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME |
|
624
|
|
|
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '".$database."';"); |
|
625
|
|
|
|
|
626
|
|
|
if (isset($results[0])) { |
|
627
|
|
|
|
|
628
|
|
|
$return .= "# MYSQL DEFAULT_CHARACTER_SET_NAME: ".$results[0]->DEFAULT_CHARACTER_SET_NAME."\n"; |
|
629
|
|
|
$return .= "# MYSQL SCHEMA_NAME: ".$results[0]->DEFAULT_COLLATION_NAME."\n"; |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
$return .= "#\n# Database : `".$database."`\n# --------------------------------------------------------\n\n"; |
|
633
|
|
|
|
|
634
|
|
|
$this->log(sprintf(__("Writing %s database dump headers"), $database)); |
|
635
|
|
|
|
|
636
|
|
|
$return = $this->fs->get_tmp_filesystem()->write($file, $return); |
|
637
|
|
|
return $return['size']; |
|
638
|
|
|
|
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
|
|
642
|
|
|
} |
|
643
|
|
|
|