-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass_backup_SQL.php
477 lines (448 loc) · 19.3 KB
/
class_backup_SQL.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
class SQL_Backup {
private $version = "1.0.7";
private $site = "https://github.com/Chak10/Backup-SQL-By-Chak10.git";
var $con;
var $compress;
var $table_name;
var $folder;
var $qlimit;
var $alltable_in_file;
var $err;
var $res;
var $del_csv;
var $enc_csv;
var $save;
var $sql_unique;
var $close;
var $json_pretty;
var $info_t;
var $info = array();
function __construct($con = null, $table_name = null, $folder = null, $query_limit = null, $compress = null, $ext = null, $alltable_in_file = null, $save = null, $sql_unique = null) {
$this->con = $con;
$this->table_name = $table_name;
$this->folder = $folder;
$this->qlimit = $query_limit;
$this->compress = $compress;
$this->ext = $ext;
$this->alltable_in_file = $alltable_in_file;
$this->save = $save;
$this->sql_unique = $sql_unique;
}
function con($HOST, $USER, $PASSWD, $NAME, $PORT = null, $SOCK = null) {
$con = new mysqli($HOST, $USER, $PASSWD, $NAME, $PORT != null ? $PORT : ini_get("mysqli.default_port"), $SOCK != null ? $SOCK : ini_get("mysqli.default_socket"));
$this->con = $con;
}
function execute() {
$this->checkcon($this->con);
$this->checkfolder($this->folder);
$this->checklimit($this->qlimit);
$this->checkcompress($this->compress);
$this->checkext($this->ext);
$this->checksep($this->alltable_in_file);
$this->checksave($this->save);
$this->checksql_unique($this->sql_unique);
$save = $this->save;
if ($this->res != null && $this->res == true) {
$con = $this->con;
$limit = $this->qlimit;
is_string($this->table_name) || is_array($this->table_name) ? $table_name = $this->table_name : $table_name = "*";
if ($table_name == "*") {
$tables = array();
$result = $con->query("SHOW TABLES");
while ($table_row = $result->fetch_row()) {
$tables[] = $table_row[0];
}
} else {
$tables = is_array($table_name) ? $table_name : explode(",", $table_name);
}
$this->del_csv != null ? $del_c = $this->del_csv : $del_c = ',';
$this->enc_csv != null ? $enc_c = $this->enc_csv : $enc_c = '';
foreach ($this->ext as $type_ext) {
$type_ext = trim($type_ext);
switch ($type_ext) {
case "sql":
$save == true ? $this->sql_exec($con, $tables, $limit) : $this->sql = $this->sql($con, $tables, $limit);
break;
case "csv":
$save == true ? $this->csv_exec($con, $tables, $del_c, $enc_c) : $this->csv = $this->csv($con, $tables, $del_c, $enc_c);
break;
case "json":
$save == true ? $this->json_exec($con, $tables) : $this->json = $this->json($con, $tables);
break;
}
}
}
}
function __destruct() {
$con = $this->con;
$this->checkcon($con);
$this->res != null && $this->res == true && $this->close === true ? $con->close() : '';
}
private function json_exec($con, $tables) {
if (is_array($tables) === false)
return false;
if ($this->compress == true) {
$result = $this->json($con, $tables);
$res_f = $this->joiner($result);
foreach ($result as $tab => $res) {
$this->compress($res, $tab, 'json', $this->alltable_in_file == false ? false : true, $res_f[1]);
}
} else {
$result = $this->json($con, $tables);
foreach ($result as $tab => $res) {
$this->nocompress($res, $tab, 'json');
}
}
}
private function csv_exec($con, $tables, $del_c, $enc_c) {
if (is_array($tables) === false)
return false;
if ($this->compress == true) {
$result = $this->csv($con, $tables, $del_c, $enc_c);
$res_f = $this->joiner($result);
foreach ($result as $tab => $res) {
$this->compress($res, $tab, 'csv', $this->alltable_in_file == false ? false : true, $res_f[1]);
}
} else {
$result = $this->csv($con, $tables, $del_c, $enc_c);
foreach ($result as $tab => $res) {
$this->nocompress($res, $tab, 'csv');
}
}
}
private function sql_exec($con, $tables, $limit) {
if (is_array($tables) === false)
return false;
if ($this->compress == true) {
if ($this->sql_unique == true) {
$result = $this->sql($con, $tables, $limit);
$res = $this->joiner($result);
$this->compress($res[0], $res[1], 'sql', $this->alltable_in_file == false ? false : true, $res[1]);
} else {
$result = $this->sql($con, $tables, $limit);
$res_f = $this->joiner($result);
foreach ($result as $tab => $res) {
$this->compress($res, $tab, 'sql', $this->alltable_in_file == false ? false : true, $res_f[1]);
}
}
} else {
if ($this->sql_unique == true) {
$result = $this->sql($con, $tables, $limit);
$res = $this->joiner($result);
$this->nocompress($res[0], $res[1], 'sql');
} else {
$result = $this->sql($con, $tables, $limit);
$res_f = $this->joiner($result);
foreach ($result as $tab => $res) {
$this->nocompress($res, $tab, 'sql', $this->alltable_in_file == true ? true : false, $res_f[1]);
}
}
}
}
private function joiner($result) {
is_array($result) === false ? $this->res = false && $this->err = -8 : '';
if (!$this->res)
return false;
$str = '';
$tabnm = array();
foreach ($result as $tab => $res) {
$str .= $res;
!in_array($tab, $tabnm) ? $tabnm[] = $tab : '';
}
$tb = implode("-", $tabnm);
return array(
$str,
$tb
);
}
private function json($con, $tables) {
if (is_array($tables) === false)
return false;
foreach ($tables as $k => $table) {
$result = $con->query("SELECT * FROM `" . $table . "`");
$result == false ? $this->res = false && $this->err = -7 && $this->info["MySQL_Errno"] = $con->errno && $this->info["MySQL_Error"] = $con->error : '';
if (!$this->res)
return false;
$num_fields = $result->field_count;
$num_rows = $result->num_rows;
if ($result) {
$info = $result->fetch_all(MYSQLI_ASSOC);
$forjson[$table] = json_encode($info, $this->json_pretty === true ? JSON_PRETTY_PRINT : 0);
}
$this->info_t === true ? $this->info[$table] = array(
"R" => $num_rows,
"C" => $num_fields
) : '';
}
return $forjson;
}
private function csv($con, $tables, $del = ',', $enc = '') {
if (is_array($tables) === false)
return false;
$return = array();
foreach ($tables as $table) {
if ($result = $con->query("SELECT * FROM `" . $table . "`")) {
$x = 0;
$fields = array();
while ($field_info = $result->fetch_field()) {
$fields[$x] = $field_info->name;
++$x;
}
$num_fields = count($fields);
$x = '';
foreach ($fields as $k => $field) {
if ($k != count($fields) - 1) {
$x .= $enc . $field . $enc . $del;
} else {
$x .= $enc . $field . $enc . PHP_EOL;
}
}
$num_rows = 0;
while ($row = $result->fetch_row()) {
foreach ($row as $k => $f) {
if ($k != count($row) - 1) {
$x .= $enc . $f . $enc . $del;
} else {
$x .= $enc . $f . $enc . PHP_EOL;
}
}
++$num_rows;
}
$this->info_t === true ? $this->info[$table] = array(
"R" => $num_rows,
"C" => $num_fields
) : '';
$return[$table] = $x;
} else {
$this->res = false;
$this->err = -6;
$this->info["MySQL_Errno"] = $con->errno;
$this->info["MySQL_Error"] = $con->error;
return false;
}
}
return $return;
}
private function sql($con, $tables, $limit) {
if (is_array($tables) === false)
return false;
$foreturn = array();
foreach ($tables as $table) {
$info = $con->query("SHOW TABLE STATUS WHERE NAME LIKE '$table'");
$info == false ? $this->res = false && $this->err = -5 && $this->info["MySQL_Errno"] = $con->errno && $this->info["MySQL_Error"] = $con->error : '';
if (!$this->res)
return false;
$nl = PHP_EOL;
$info = $info->fetch_assoc();
$charset = $con->get_charset();
$return = "-- Backup SQL By Chak10" . $nl . "-- Version: " . ($this->version) . $nl . "-- Github: " . ($this->site) . $nl . "--" . $nl . "--" . $nl;
$return .= "-- Server Version: " . ($con->server_info) . $nl . "-- PHP Version: " . (PHP_VERSION) . $nl . "-- Host Info: " . ($con->host_info) . $nl;
$return .= "-- Date: " . (date('Y-m-d H:i:s')) . $nl . $nl . $nl;
$return .= "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";" . $nl . "SET time_zone = \"+00:00\";" . $nl . $nl . $nl . $nl;
$return .= "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;" . $nl . "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;" . $nl . "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;" . $nl . "/*!40101 SET NAMES utf8 */;" . $nl . $nl . $nl;
$return .= "--" . $nl . "-- Charset General: " . ($charset->charset) . $nl . "-- Charset Table: " . ($info['Collation']) . $nl . "--" . $nl . $nl;
$return .= "-- ------------------------------------------" . $nl . $nl . "--" . $nl;
$return .= "-- Table Name: `" . ($table) . "`" . $nl;
$res = $con->query("SHOW CREATE TABLE `" . $table . "`");
$table_init = $res->fetch_row();
$result = $con->query("SELECT * FROM `" . $table . "`");
$num_fields = $result->field_count;
$num_rows = $result->num_rows;
$fields = '';
while ($field_info = $result->fetch_field()) {
$fields .= "`" . $field_info->name . "`,";
$db = $field_info->db;
}
$fields = substr($fields, 0, -1);
$return .= "-- Database: " . ($db) . $nl . "--" . $nl;
$this->info_t === true ? $this->info[$table] = array(
"R" => $num_rows,
"C" => $num_fields
) : '';
$return .= "-- Columns: $num_fields" . $nl;
$return .= "-- Rows: $num_rows" . $nl . "--" . $nl . $nl;
$return .= "DROP TABLE IF EXISTS " . $table . ";" . $nl;
$return .= $table_init[1] . ";" . $nl . $nl . $nl;
for ($i = 0, $s = 0; $i < $num_fields; ++$i) {
while ($row = $result->fetch_row()) {
if ($s == 0) {
$return .= "INSERT INTO `" . $table . "` ( $fields ) VALUES " . $nl . '(';
} elseif (is_int($s / $limit) === true) {
$return .= ';' . $nl . "INSERT INTO `" . $table . "` ( $fields ) VALUES " . $nl . '(';
} else {
$return .= ',' . $nl . "(";
}
for ($j = 0; $j < $num_fields; $j++) {
$row[$j] = str_replace("\n", "\\n", addslashes($row[$j]));
if (isset($row[$j])) {
$return .= "'" . $row[$j] . "'";
} else {
$return .= "''";
}
if ($j < ($num_fields - 1))
$return .= ',';
}
$return .= ")";
++$s;
}
}
$return .= ';' . $nl . $nl . $nl;
$return .= "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;" . $nl;
$return .= "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;" . $nl;
$return .= "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;" . $nl;
$foreturn[$table] = $return;
}
return $foreturn;
}
private function compress($str, $table, $extens, $recursive = false, $tab_name = null) {
if (!$this->res)
return false;
!is_dir($this->folder . "/" . $extens) ? $res = mkdir($this->folder . "/" . $extens, 0764, true) : '';
!is_writable($this->folder . "/" . $extens) ? $res = chmod($this->folder . "/" . $extens, 0764) : '';
$zip = new ZipArchive;
if ($recursive) {
if ($tab_name == null) {
$this->res = false;
$this->err = -11;
return false;
}
$name_zip = $this->folder . "/" . $extens . '/' . $tab_name . '_' . hash('crc32b', $tab_name) . '.' . $extens . '.zip';
$res = $zip->open($name_zip, ZIPARCHIVE::CREATE);
if (!file_exists($name_zip)) {
$comment = "Created with Backup SQL By Chak10" . PHP_EOL;
$comment .= "Version: " . ($this->version) . PHP_EOL;
$comment .= "Github: " . ($this->site) . PHP_EOL;
} else {
$comment = $zip->getArchiveComment() . PHP_EOL;
}
} else {
$res = $zip->open($this->folder . "/" . $extens . '/' . $table . '_' . time() . '-' . hash('crc32b', microtime(true)) . '.' . $extens . '.zip', ZipArchive::CREATE);
$comment = "Created with Backup SQL By Chak10" . PHP_EOL;
$comment .= "Version: " . ($this->version) . PHP_EOL;
$comment .= "Github: " . ($this->site) . PHP_EOL;
}
if ($res === TRUE) {
$zip->addFromString($table . '-' . date('Y-m-d_H:i:s') . '-' . hash('crc32b', microtime(true)) . '.' . $extens, $str);
$comment .= "Table: " . $table . " - CRC32b: " . hash('crc32b', $str) . ' - Date: ' . date('Y-m-d H:i:s');
$zip->setArchiveComment($comment);
$zip->close();
$this->res = true;
} else {
$this->res = false;
}
}
private function nocompress($res, $table, $extens, $new_dir = false, $name_new = null) {
if (!$this->res)
return false;
!is_dir($this->folder . "/" . $extens) ? $res = mkdir($this->folder . "/" . $extens, 0764, true) : '';
!is_writable($this->folder . "/" . $extens) ? $res = chmod($this->folder . "/" . $extens, 0764) : '';
if ($new_dir === true) {
if ($name_new == null) {
$this->res = false;
$this->err = -9;
return false;
}
!is_dir($this->folder . "/" . $extens . '/' . $name_new) ? $res = mkdir($this->folder . "/" . $extens . '/' . $name_new, 0764, true) : '';
$name = $this->folder . "/" . $extens . '/' . $name_new . '/' . $table . '_' . time() . '-' . md5($table . microtime(true)) . '.' . $extens;
$fpc = @fopen($name, 'w');
$fpc === false ? $this->res = false : $this->res = true;
if (!$this->res)
return false;
fwrite($fpc, $res) === false ? $this->res = false : $this->res = true;
fclose($fpc);
} else {
$name = $this->folder . "/" . $extens . '/' . $table . '_' . time() . '-' . hash('crc32b', microtime(true)) . '.' . $extens;
$fpc = @fopen($name, 'w');
$fpc === false ? $this->res = false && $this->err = -10 : $this->res = true;
if (!$this->res)
return false;
fwrite($fpc, $res) === false ? $this->res = false : $this->res = true;
fclose($fpc);
}
}
private function checksql_unique($s) {
if (!$this->res)
return false;
!is_bool($s) && $s == null ? $this->sql_unique = false : '';
$this->res = true;
}
private function checksave($s) {
if (!$this->res)
return false;
!is_bool($s) && $s == null ? $this->save = true : '';
$this->res = true;
}
private function checksep($s) {
if (!$this->res)
return false;
!is_bool($s) && $s == null ? $this->alltable_in_file = false : '';
$this->res = true;
}
private function checkext($ext) {
if (!$this->res)
return false;
if (is_string($ext)) {
$ext = explode(',', strtolower($ext));
} elseif (is_array($ext)) {
$ext = array_map('strtolower', $ext);
} else {
$ext = array();
}
if (in_array("sql", $ext) || in_array("csv", $ext) || in_array("json", $ext)) {
$this->ext = $ext;
} elseif (in_array("all", $ext)) {
$this->ext = array(
"sql",
"csv",
"json"
);
} else {
$this->ext = array(
"sql"
);
}
}
private function checkcompress($comp) {
if (!$this->res)
return false;
!is_bool($comp) && $comp == null ? $this->compress = true : '';
$this->res = true;
}
private function checklimit($limit) {
if (!$this->res)
return false;
is_int($limit) && $limit >= 1 ? $this->qlimit = $limit : $this->qlimit = 400;
$this->res = true;
}
private function checkfolder($folder) {
if (!$this->res)
return false;
!is_string($folder) ? $folder = "backup/database" : '';
$res = true;
!is_dir($folder) ? $res = mkdir($folder, 0764, true) : '';
!is_writable($folder) ? $res = chmod($folder, 0764, true) : '';
$this->folder = $folder;
$this->res = $res;
}
private function checkcon($con) {
if ($con == null) {
$this->err = -1;
$this->res = false;
} elseif (empty($con)) {
$this->err = -2;
$this->res = false;
} elseif (!is_object($con)) {
$this->err = -3;
$this->res = false;
} elseif ($con->connect_error) {
$this->log_ce = $con->connect_error;
$this->err = -4;
$this->res = false;
} else {
$this->con = $con;
$this->res = true;
}
}
}
?>