在开发业务系统过程中,数据库是非常重要的一环。因此,对数据库进行备份和恢复是非常必要的操作。本文将结合thinkphp6框架实例,介绍如何使用thinkphp6实现数据库备份与恢复。
一、数据库备份
1.1 环境准备
在进行数据库备份之前,需要确认如下几点:
1、需要设置好mysql数据库的bin目录地址,并把其路径加入系统Path变量中;
点击下载“嗨格式数据恢复大师”;
2、需要安装好mysqldump命令行工具;
3、确认在数据库所在的机器上,执行备份的用户,有对数据库执行mysqldump命令的权限。
1.2 数据库备份实现
1.2.1 配置备份参数
在config文件夹下创建database.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
<?php return [
// 数据库类型
type => mysql,
// 数据库连接DSN配置
dsn => ,
// 服务器地址
hostname => localhost,
// 数据库名
database => test,
// 数据库用户名
username => root,
// 数据库密码
password => root,
// 数据库连接端口
hostport => 3306,
// 数据库连接参数
params => [],
// 数据库编码默认采用utf8
charset => utf8,
// 数据库表前缀
prefix => think_,
// 数据库调试模式
debug => false,
// 数据库备份路径,没有则自动创建
path => ,
// 数据库备份卷大小,单位为字节,设为0表示不限制备份大小
part => 20971520,
// 数据库备份文件压缩格式,这里是gzip
compress => gzip,
// 数据库备份文件名
filename => ,
// 数据库备份文件是否需要压缩
zip => true,
// 数据库备份文件是否需要分卷备份
split => true,
// 数据库备份时是否将存储过程和触发器一起备份
level => 9,
// 数据库备份文件的存储路径,最好为绝对路径,这也是最关键的路径
path => /data/mysql/,
];
1.2.2 编写备份代码
在app/controller下创建BackupController.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
acadeDb;
class BackupController
{
protected $backupConfig;
public function __construct()
{
$this->backupConfig = config(database);
}
public function backup()
{
// 防止备份数据过程超时
set_time_limit(0);
$database = $this->backupConfig[database];
$filename = date(Ymd-His, time()) . ".sql";
$path = $this->backupConfig[path].$filename;
// 检查目录是否存在或者是否有权限写入
if(!is_dir($this->backupConfig[path])){
mkdir($this->backupConfig[path], 0755, true);
}else{
if(!is_writeable($this->backupConfig[path])){
chmod($this->backupConfig[path], 0755);
}
}
// 备份所有数据表
$result = Db::query("SHOW TABLES");
$tables = array();
foreach($result as $index => $row){
$tables[] = $row[Tables_in_.$database];
}
// 备份所有表结构和表数据
$content = ;
foreach($tables as $table){
$content = $content . "/*" . PHP_EOL;
$content = $content . "表名:" . $table . PHP_EOL;
$content = $content . "表结构:" . PHP_EOL;
$content = $content . "*/" . PHP_EOL;
$content = $content . $this->backupTableSchema($table);
$content = $content . "/*" . PHP_EOL;
$content = $content . "表数据:" . PHP_EOL;
$content = $content . "*/" . PHP_EOL;
$content = $content . $this->buildInsertSql($table);
}
// 是否需要压缩
if ($this->backupConfig[zip]) {
$zip = new ZipArchive();
$zipfilename = $this->backupConfig[path] . date(Ymd-His, time()) . ".zip";
if ($zip->open($zipfilename, ZipArchive::OVERWRITE) === TRUE) {
$zip->addFile($path,$filename);
$zip->close();
// 删除非压缩的文件
unlink($path);
} else {
// 备份失败
}
}
}
// 备份表结构
protected function backupTableSchema($table)
{
$database = $this->backupConfig[database];
$result = Db::query("SHOW CREATE TABLE `" . $table . "`");
$create = $result[0][Create Table] . ";" . PHP_EOL.PHP_EOL;
return $create;
}
// 备份表数据
protected function buildInsertSql($table)
{
$database = $this->backupConfig[database];
$result = Db::query("SELECT * FROM `" . $table . "`");
$insert = ;
foreach ($result as $key => $value) {
$keys = array_keys($value);
$values = array_map(array(Db::class, quote), array_values($value));
$values = join(",", $values);
$insert .= "INSERT INTO `" . $table . "` (`" . join("`,`", $keys) . "`) VALUES (" . $values . ");" . PHP_EOL;
}
$insert .= PHP_EOL;
return $insert;
}
}
1.2.3 执行备份
在浏览器中输入以下url地址即可执行备份:
1
http://localhost/backup/backup
1.3 数据库恢复
1.3.1 编写恢复代码
在app/controller下创建RecoveryController.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
acadeDb;
class RecoveryController
{
protected $backupConfig;
public function __construct()
{
$this->backupConfig = config(database);
}
public function recovery()
{
// 防止还原数据过程超时
set_time_limit(0);
ini_set(memory_limit, 1024M);
$filename = input(get.filename);
// 读取备份文件
if ($this->backupConfig[zip]) {
$zip = new ZipArchive();
if ($zip->open($this->backupConfig[path].$filename) === true) {
$filename = $zip->getNameIndex(0);
$zip->extractTo($this->backupConfig[path]);
$zip->close();
}
}
$content = file_get_contents($this->backupConfig[path] . $filename);
// 使用";"分割内容
$statements = explode(";", $content);
// 开始事务
Db::startTrans();
foreach ($statements as $index => $stmt) {
if (trim($stmt) === ) {
continue;
}
$results = Db::query($stmt);
if ($results === false) {
Db::rollback();
return false;
}
}
// 提交事务
Db::commit();
// 删除非压缩的文件
unlink($this->backupConfig[path] . $filename);
return true;
}
}
1.3.2 执行恢复
在浏览器中输入以下url地址即可执行恢复:
1
http://localhost/recovery/recovery?filename=20200101-121212.sql.zip
以上为ThinkPHP6实现数据库备份与恢复的实现方法,读者可以将代码应用到自己的项目中,灵活运用其中的技巧,让我们的业务更加健壮可靠。
以上就是如何使用ThinkPHP6实现数据库备份与恢复的详细内容,更多请关注php中文网其它相关文章!