1
0

first commit

This commit is contained in:
2020-08-06 14:58:51 +08:00
commit 17096657dc
780 changed files with 92857 additions and 0 deletions

View File

@@ -0,0 +1,182 @@
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\common\service;
use think\Config;
use think\Loader;
use think\Session;
use tools\Format;
class Database extends _Init
{
/**
* 获取表信息
* @return array
*/
public static function getTables()
{
$list = array_map('array_change_key_case', Loader::db()->query('SHOW TABLE STATUS'));
foreach ($list as $key => $value) {
$list[$key]['data_length'] = Format::byte($value['data_length']);
$list[$key]['index_length'] = Format::byte($value['index_length']);
}
return $list;
}
/**
* 备份数据库
* @param [type] $tables [description]
* @param [type] $id [description]
* @param [type] $start [description]
* @return [type] [description]
*/
public static function backup($tables = null, $id = null, $start = null)
{
if (IS_POST && !empty($tables) && is_array($tables)) {
//初始化
$config = Config::get('backdata');
// 检查是否有正在执行的任务
if (!is_writeable($config['path'])) {
return '备份目录不存在或不可写,请检查后重试!';
}
$lock = "{$config['path']}backup.lock";
if (is_file($lock)) {
return '检测到一个任务正在执行,请稍后再试!';
} else {
file_put_contents($lock, time()); //创建锁文件
}
Session::set('backup_config', $config);
//生成备份文件信息
$file = array(
'name' => date('Ymd-His', time()),
'part' => 1,
);
Session::set('backup_file', $file);
//缓存要备份的表
Session::set('backup_tables', $tables);
//创建备份文件
$Database = new \tools\Database($file, $config);
if (false !== $Database->create()) {
Logs::write('备份数据库', ['table' => $tables]);
return [
'msg' => '初始化成功!',
'data' => ['id' => 0, 'start' => 0],
];
} else {
return '初始化失败,备份文件创建失败!';
}
} elseif (IS_GET && is_numeric($id) && is_numeric($start)) {
//备份数据
$tables = Session::get('backup_tables');
//备份指定表
$Database = new \tools\Database(Session::get('backup_file'), Session::get('backup_config'));
$start = $Database->backup($tables[$id], $start);
if (false === $start) {
return '备份出错!';
} elseif (0 === $start) {
//下一表
if (isset($tables[++$id])) {
return [
'msg' => '备份完成!',
'data' => ['id' => $id, 'start' => 0],
];
} else {
//备份完成,清空缓存
unlink(Session::get('backup_config.path') . 'backup.lock');
Session::delete('backup_tables');
Session::delete('backup_file');
Session::delete('backup_config');
return [
'msg' => '备份完成!',
'data' => '',
];
}
} else {
$rate = floor(100 * ($start[0] / $start[1]));
return [
'msg' => '"正在备份..({$rate}%)"',
'data' => ['id' => $id, 'start' => $start[0]],
];
}
}
}
/**
* [del description]
* @param [type] $time [description]
* @return [type] [description]
*/
public static function del($time)
{
$path = [];
if (is_array($time)) {
foreach ($time as $value) {
$name = date('Ymd-His', $value) . '-*.sql*';
$file = Config::get('backdata.path') . $name;
$path = array_merge($path, glob($file));
}
} else {
$name = date('Ymd-His', $time) . '-*.sql*';
$file = Config::get('backdata.path') . $name;
$path = glob($file);
}
try {
array_map("unlink", $path);
Logs::write('删除备份文件', ['files' => $path]);
return true;
} catch (\Exception $e) {
return '备份文件删除失败,请检查权限!';
}
}
/**
* 备份文件卷列表
* @return array
*/
public static function backupList()
{
$path = Config::get('backdata.path');
$list = [];
if (is_dir($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)) {
$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'] += $file->getSize();
} else {
$info['part'] = $part;
$info['size'] = $file->getSize();
}
$extension = strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
$info['compress'] = ($extension === 'SQL') ? '-' : $extension;
$info['time'] = strtotime("{$date} {$time}");
$list["{$date} {$time}"] = $info;
}
krsort($list);
}
}
array_walk($list, function ($val, $key) use (&$list) {
$list[$key]['size'] = Format::byte($val['size']);
});
return $list;
}
}