-
Notifications
You must be signed in to change notification settings - Fork 7
/
Backup.php
596 lines (560 loc) · 17.3 KB
/
Backup.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
/*
* This file is part of the tp5er/tp5-databackup.
*
* (c) pkg6 <https://github.com/pkg6>
*
* This source file is subject to the MIT license that is bundled.
*/
// | Author: tp5er <[email protected]>
// | QQ: 1751212020
// +----------------------------------------------------------------------
namespace tp5er\Backup;
use think\App;
use think\Db;
/**
* Class Backup.
*/
class Backup
{
/**
* 文件指针.
*
* @var resource
*/
private $fp;
/**
* 备份文件信息 part - 卷号,name - 文件名.
*
* @var array
*/
private $file;
/**
* 当前打开文件大小.
*
* @var int
*/
private $size = 0;
/**
* 数据库配置.
*
* @var int
*/
private $dbconfig = [];
/**
* 备份配置.
*
* @var int
*/
private $config = [
'path' => '',
//数据库备份路径
'part' => 20971520,
//数据库备份卷大小
'compress' => 0,
//数据库备份文件是否启用压缩 0不压缩 1 压缩
'level' => 9,
];
/**
* 数据库备份构造方法.
*
* @param array $config 备份配置信息
*
* @throws \Exception
*/
public function __construct($config = [])
{
$this->config = array_merge($this->config, $config);
if (empty($this->config["path"])) {
$this->config["path"] = \app()->getRootPath() . "backup/";
}
//初始化文件名
$this->setFile();
//初始化数据库连接参数
$this->setDbConn();
//检查文件是否可写
if ( ! $this->checkPath($this->config['path'])) {
throw new \Exception("The current directory is not writable");
}
}
/**
* 设置脚本运行超时时间
* 0表示不限制,支持连贯操作.
*/
public function setTimeout($time = null)
{
if ( ! is_null($time)) {
set_time_limit($time) || ini_set("max_execution_time", $time);
}
return $this;
}
/**
* 设置数据库连接必备参数.
*
* @param array $dbconfig 数据库连接配置信息
*
* @return object
*/
public function setDbConn($dbconfig = [])
{
if (empty($dbconfig)) {
$this->dbconfig = config('database');
//$this->dbconfig = Config::get('database');
} else {
$this->dbconfig = $dbconfig;
}
return $this;
}
/**
* 设置备份文件名.
*
* @param Array $file 文件名字
*
* @return $this
*/
public function setFile($file = null)
{
if (is_null($file)) {
$this->file = ['name' => date('Ymd-His'), 'part' => 1];
} else {
if ( ! array_key_exists("name", $file) && ! array_key_exists("part", $file)) {
$this->file = $file['1'];
} else {
$this->file = $file;
}
}
return $this;
}
/**
* 数据类连接.
*
* @return \think\db\ConnectionInterface
*/
public static function connect()
{
if (APP::VERSION >= "6.0.0") {
return \think\facade\Db::connect();
} else {
return Db::connect();
}
}
/**
* 数据库表列表.
*
* @param $table
* @param $type
*
* @return array
*/
public function dataList($table = null, $type = 1)
{
$db = self::connect();
if (is_null($table)) {
$list = $db->query("SHOW TABLE STATUS");
} else {
if ($type) {
$list = $db->query("SHOW FULL COLUMNS FROM {$table}");
} else {
$list = $db->query("show columns from {$table}");
}
}
return array_map('array_change_key_case', $list);
//$list;
}
/**
* 数据库备份文件列表.
*
* @return array
*/
public function fileList()
{
if ( ! is_dir($this->config['path'])) {
mkdir($this->config['path'], 0755, true);
}
$path = realpath($this->config['path']);
$flag = \FilesystemIterator::KEY_AS_FILENAME;
$glob = new \FilesystemIterator($path, $flag);
$list = [];
foreach ($glob as $name => $file) {
if (preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql(?:\\.gz)?$/', $name)) {
$name1 = $name;
$name = sscanf($name, '%4s%2s%2s-%2s%2s%2s-%d');
$date = "{$name[0]}-{$name[1]}-{$name[2]}";
$time = "{$name[3]}:{$name[4]}:{$name[5]}";
$part = $name[6];
if (isset($list["{$date} {$time}"])) {
$info = $list["{$date} {$time}"];
$info['part'] = max($info['part'], $part);
$info['size'] = $info['size'] + $file->getSize();
} else {
$info['part'] = $part;
$info['size'] = $file->getSize();
}
$extension = strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
$info['name'] = $name1;
$info['compress'] = $extension === 'SQL' ? '-' : $extension;
$info['time'] = strtotime("{$date} {$time}");
$list["{$date} {$time}"] = $info;
}
}
return $list;
}
/**
* @param $type
* @param $time
*
* @return array|false|mixed|string
*
* @throws \Exception
*/
public function getFile($type = '', $time = 0)
{
if ( ! is_numeric($time)) {
throw new \Exception("{$time} Illegal data type");
}
switch ($type) {
case 'time':
$name = date('Ymd-His', $time) . '-*.sql*';
$path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name;
return glob($path);
case 'timeverif':
$name = date('Ymd-His', $time) . '-*.sql*';
$path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name;
$files = glob($path);
$list = [];
foreach ($files as $name) {
$basename = basename($name);
$match = sscanf($basename, '%4s%2s%2s-%2s%2s%2s-%d');
$gz = preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql.gz$/', $basename);
$list[$match[6]] = [$match[6], $name, $gz];
}
$last = end($list);
if (count($list) === $last[0]) {
return $list;
} else {
throw new \Exception("File {$files['0']} may be damaged, please check again");
}
// no break
case 'pathname':
return "{$this->config['path']}{$this->file['name']}-{$this->file['part']}.sql";
case 'filename':
return "{$this->file['name']}-{$this->file['part']}.sql";
case 'filepath':
return $this->config['path'];
default:
return [
'pathname' => "{$this->config['path']}{$this->file['name']}-{$this->file['part']}.sql",
'filename' => "{$this->file['name']}-{$this->file['part']}.sql",
'filepath' => $this->config['path'],
'file' => $this->file
];
}
}
/**
* 删除备份文件.
*
* @param $time
*
* @return mixed
*
* @throws \Exception
*/
public function delFile($time)
{
if ($time) {
$file = $this->getFile('time', $time);
array_map("unlink", $this->getFile('time', $time));
if (count($this->getFile('time', $time))) {
throw new \Exception("File {$file} deleted failed");
} else {
return $time;
}
} else {
throw new \Exception("{$time} Time parameter is incorrect");
}
}
/**
* 下载备份.
*
* @Author: 浪哥 <[email protected]>
*
* @param string $time
* @param int $part
*
* @return array|mixed|string
*
* @throws \Exception
*/
public function downloadFile($time, $part = 0)
{
$file = $this->getFile('time', $time);
$fileName = $file[$part];
if (file_exists($fileName)) {
ob_end_clean();
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($fileName));
header('Content-Disposition: attachment; filename=' . basename($fileName));
readfile($fileName);
} else {
throw new \Exception("{$time} File is abnormal");
}
}
/**
* @param $start
* @param $time
*
* @return array|false|int
*
* @throws \Exception
*/
public function import($start, $time)
{
//还原数据
$db = self::connect();
$this->file = $this->getFile('time', $time);
if ($this->config['compress']) {
$gz = gzopen($this->file[0], 'r');
$size = 0;
} else {
$size = filesize($this->file[0]);
$gz = fopen($this->file[0], 'r');
}
$sql = '';
if ($start) {
$this->config['compress'] ? gzseek($gz, $start) : fseek($gz, $start);
}
for ($i = 0; $i < 1000; $i++) {
$sql .= $this->config['compress'] ? gzgets($gz) : fgets($gz);
if (preg_match('/.*;$/', trim($sql))) {
if (false !== $db->execute($sql)) {
$start += strlen($sql);
} else {
return false;
}
$sql = '';
} elseif ($this->config['compress'] ? gzeof($gz) : feof($gz)) {
return 0;
}
}
return [$start, $size];
}
/**
* 写入初始数据.
*
* @return bool true - 写入成功,false - 写入失败
*/
public function Backup_Init()
{
$hostname = $this->dbconfig["hostname"] ?? $this->dbconfig["connections"][$this->dbconfig["default"]]["hostname"];
$hostport = $this->dbconfig["hostport"] ?? $this->dbconfig["connections"][$this->dbconfig["default"]]["hostport"];
$database = $this->dbconfig["database"] ?? $this->dbconfig["connections"][$this->dbconfig["default"]]["database"];
$sql = "-- -----------------------------\n";
$sql .= "-- Think MySQL Data Transfer \n";
$sql .= "-- \n";
$sql .= "-- Host : " . $hostname . "\n";
$sql .= "-- Port : " . $hostport . "\n";
$sql .= "-- Database : " . $database . "\n";
$sql .= "-- \n";
$sql .= "-- Part : #{$this->file['part']}\n";
$sql .= "-- Date : " . date("Y-m-d H:i:s") . "\n";
$sql .= "-- -----------------------------\n\n";
$sql .= "SET FOREIGN_KEY_CHECKS = 0;\n\n";
return $this->write($sql);
}
/**
* 备份表结构.
*
* @param string $table 表名
* @param int $start 起始行数
*
* @return bool false - 备份失败
*/
public function backup($table, $start)
{
$db = self::connect();
// 备份表结构
if (0 == $start) {
$result = $db->query("SHOW CREATE TABLE `{$table}`");
//视图数据 不进行备份
if ( ! empty($result[0]["Create View"])) {
return 0;
}
$sql = "\n";
$sql .= "-- -----------------------------\n";
$sql .= "-- Table structure for `{$table}`\n";
$sql .= "-- -----------------------------\n";
$sql .= "DROP TABLE IF EXISTS `{$table}`;\n";
$sql .= trim($result[0]['Create Table'] ?? $result[0]['Create View']) . ";\n\n";
if (false === $this->write($sql)) {
return false;
}
}
//数据总数
$result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`");
$count = $result['0']['count'];
//备份表数据
if ($count) {
//写入数据注释
if (0 == $start) {
$sql = "-- -----------------------------\n";
$sql .= "-- Records of `{$table}`\n";
$sql .= "-- -----------------------------\n";
$this->write($sql);
}
//备份数据记录
$result = $db->query("SELECT * FROM `{$table}` LIMIT {$start}, 1000");
foreach ($result as $row) {
$values = [];
foreach ($row as $val) {
if (is_numeric($val)) {
$values[] = $val;
} elseif (is_null($val)) {
$values[] = 'NULL';
} else {
$values[] = "'" . str_replace(["\r", "\n"], ['\\r', '\\n'], addslashes($val)) . "'";
}
}
$sql = "INSERT INTO `{$table}` VALUES (" . implode(", ", $values) . ");\n";
if (false === $this->write($sql)) {
return false;
}
}
//还有更多数据
if ($count > $start + 1000) {
//return array($start + 1000, $count);
return $this->backup($table, $start + 1000);
}
}
//备份下一表
return 0;
}
/**
* 优化表.
*
* @param String $tables 表名
*
* @return String $tables
*
* @throws \Exception
*/
public function optimize($tables = null)
{
if ($tables) {
$db = self::connect();
if (is_array($tables)) {
$tables = implode('`,`', $tables);
$list = $db->query("OPTIMIZE TABLE `{$tables}`");
} else {
$list = $db->query("OPTIMIZE TABLE `{$tables}`");
}
if ($list) {
return $tables;
} else {
throw new \Exception("data sheet'{$tables}'Repair mistakes please try again!");
}
} else {
throw new \Exception("Please specify the table to be repaired!");
}
}
/**
* 修复表.
*
* @param String $tables 表名
*
* @return String $tables
*
* @throws \Exception
*/
public function repair($tables = null)
{
if ($tables) {
$db = self::connect();
if (is_array($tables)) {
$tables = implode('`,`', $tables);
$list = $db->query("REPAIR TABLE `{$tables}`");
} else {
$list = $db->query("REPAIR TABLE `{$tables}`");
}
if ($list) {
return $list;
} else {
throw new \Exception("data sheet'{$tables}'Repair mistakes please try again!");
}
} else {
throw new \Exception("Please specify the table to be repaired!");
}
}
/**
* 写入SQL语句.
*
* @param string $sql 要写入的SQL语句
*
* @return bool true - 写入成功,false - 写入失败!
*/
private function write($sql)
{
$size = strlen($sql);
//由于压缩原因,无法计算出压缩后的长度,这里假设压缩率为50%,
//一般情况压缩率都会高于50%;
$size = $this->config['compress'] ? $size / 2 : $size;
$this->open($size);
return $this->config['compress'] ? @gzwrite($this->fp, $sql) : @fwrite($this->fp, $sql);
}
/**
* 打开一个卷,用于写入数据.
*
* @param int $size 写入数据的大小
*/
private function open($size)
{
if ($this->fp) {
$this->size += $size;
if ($this->size > $this->config['part']) {
$this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp);
$this->fp = null;
$this->file['part']++;
session('backup_file', $this->file);
$this->Backup_Init();
}
} else {
$backuppath = $this->config['path'];
$filename = "{$backuppath}{$this->file['name']}-{$this->file['part']}.sql";
if ($this->config['compress']) {
$filename = "{$filename}.gz";
$this->fp = @gzopen($filename, "a{$this->config['level']}");
} else {
$this->fp = @fopen($filename, 'a');
}
$this->size = filesize($filename) + $size;
}
}
/**
* 检查目录是否可写.
*
* @param string $path 目录
*
* @return bool
*/
protected function checkPath($path)
{
if (is_dir($path)) {
return true;
}
if (mkdir($path, 0755, true)) {
return true;
} else {
return false;
}
}
/**
* 析构方法,用于关闭文件资源.
*/
public function __destruct()
{
if ($this->fp) {
$this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp);
}
}
}