You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.2 KiB
76 lines
2.2 KiB
<?php
|
|
namespace app\admin\command;
|
|
|
|
use app\admin\model\SqlFiles;
|
|
use think\console\Command;
|
|
use think\console\Input;
|
|
use think\console\Output;
|
|
use think\Db;
|
|
use think\Log;
|
|
class Backup extends Command
|
|
{
|
|
|
|
|
|
|
|
//定义任务名和描述
|
|
protected function configure(){
|
|
|
|
$this->setName('BackUp')->setDescription("php think backup 备份mysql数据库");
|
|
|
|
}
|
|
|
|
//调用该类时,会自动运行execute方法
|
|
public function execute(Input $input, Output $output){
|
|
set_time_limit(0);
|
|
ini_set('memory_limit','5000M');
|
|
$allData = count(SqlFiles::where(1)->select());
|
|
if ($allData == 10){
|
|
$getFirst = SqlFiles::where(1)->find();
|
|
SqlFiles::where(['id'=>$getFirst['id']])->delete();
|
|
$delFile = dirname(dirname(dirname(__DIR__)))."/backup/sql/".$getFirst['name'].".sql";
|
|
unlink($delFile);
|
|
}
|
|
$data = SqlFiles::where(1)->order("id","desc")->find();
|
|
$num = $data['id']+1;
|
|
// backup($path = '备份路径', $tableArray = [需要备份的表集合], $bool = '是否同时备份数据 默认false',['is_compress' => '是否写入内容文件进行压缩','is_download' => '是否进行下载'])
|
|
|
|
// 配置项-----必传值
|
|
$config = [
|
|
// 服务器地址
|
|
'host' => "127.0.0.1",
|
|
// 数据库名
|
|
'database' => "hospital_web",
|
|
// 用户名
|
|
'user' => "root",
|
|
// 密码
|
|
'password' =>"123456",
|
|
// 端口
|
|
'port' => "3306",
|
|
// 字符编码
|
|
'charset' => "utf8"
|
|
];
|
|
// 备份
|
|
$dir = "./backup/sql";//备份路径
|
|
$createData = [
|
|
'name'=>'hospital_web'.$num,
|
|
'create_time'=>time()
|
|
];
|
|
$res = SqlFiles::create($createData);
|
|
if (!$res){
|
|
echo "save failed";die;
|
|
}
|
|
$data = \cocolait\sql\Backup::instance($config)->backUp($dir,[],true,['is_compress' => 0],$num);
|
|
var_dump($data);
|
|
|
|
// 还原
|
|
// $data = \cocolait\sql\Backup::instance($config)->recover('test1.sql',$dir);
|
|
// print_r($data);die;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} |