PHP code example of tp5er / tp5-databackup
1. Go to this page and download the library: Download tp5er/tp5-databackup library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tp5er / tp5-databackup example snippets
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected] >
// +----------------------------------------------------------------------
use think\facade\Route;
Route::get('think', function () {
return 'hello,ThinkPHP6!';
});
Route::get('hello/:name', 'index/hello');
\tp5er\Backup\Route::route();
~~~
### 使用方式3: 通过队列的方法
#### 还原数据
~~~
$data["database"]="mysql";
$data["opt"]="import";
$data["filename"]="fastadmin-mysql-20240416184903.sql";
backup_queue($data);
~~~
#### 备份数据/修复表/优化表
~~~
$data["database"]="mysql";
$data["opt"]="backup" //backup,repair,optimize;
$data["table"]=["fa_category","fa_auth_rule"];
backup_queue($data);
~~~
### 使用方式4: 通过命令行
~~~
//进入交互模式进行相关操作
php think backup:choice
//备份整个数据库表结构和表数据
php think backup:database
//还原数据
php think backup:import fastadmin-mysql-20240416184903.sql
//列出所有备份文件
php think backup:list
//在执行本代码中会使用到缓存这里是清理缓存的命令
php think backup:cleanup
~~~
### 使用方式5: 自定义(1.x升级到2.x)
> 1.x和2.x 方法对比
| 1.x | 2.x | 描述 |
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------ |
| [点击跳转](https://github.com/pkg6/tp5-databackup/blob/ac29c488bca8a4a123b105ef2959a6cde1f67649/src/Backup.php#L54) | [点击跳转](https://github.com/pkg6/tp5-databackup/blob/main/config/backup.php) | 配置项 |
| $db= new \tp5er\Backup\Backup([$config](https://github.com/pkg6/tp5-databackup/blob/ac29c488bca8a4a123b105ef2959a6cde1f67649/src/Backup.php#L54)); | 无需初始化使用facade方式 | 初始化 |
| $db->dataList() | Backup::tables() | 数据类表列表 |
| $db->fileList() | Backup::files() | 备份文件列表 |
| $tables="数据库表列表"; $start= $db->setFile($file)->backup($tables[$id], 0); | | 备份表 |
| $db->setFile($file)->import($start) | Backup::import($file) | 导入表 |
| delFile($time) | unlink($filename) | 删除备份文件 |
| downloadFile($time) | backup_download($filename) | 下载备份文件 |
| $db->repair($tables) | Backup::repair($tables) | 修复表 |
| $db->optimize($tables) | Backup::optimize($tables) | 优化表 |
| https://github.com/pkg6/tp5-databackup/tree/1.x/tp5-demo | 进行了内置 [控制器](https://github.com/pkg6/tp5-databackup/tree/main/src/controller) 和 [视图](https://github.com/pkg6/tp5-databackup/tree/main/src/views/backup) | 案例代码 |
> 备份表
>
> 1.x 是控制器中将需备份到表结构写到缓存/cookie中,然后根据索引找到表
>
> $tables="数据库表列表";
>
> $start= $db->setFile($file)->backup($tables[$id], 0);
>
> 2.x 将需备份到表写到缓存中,无需您来处理,只需要您进行按步骤操作
>
> 第一步将备份到表进行缓存,关键方法: Backup::backupStep1($tables)
>
> 第二步备份表结构和数据,关键方法:Backup::backupStep2($tableIndex,$page),返回page直到page=0 循环结束表和表数据备份完毕
>
> 第三步清理缓存,关键方法Backup::cleanup()
## 事件`参考代码`
~~~
namespace tp5er\Backup;
use think\facade\Event as tpEvent;
class Event
{
//在备份第一步使用事件
const backupStep1 = "tp5er.backup.step1";
//在备份第二步使用初始事件
const backupStep2 = "tp5er.backup.step2";
//在备份第二步备份数据使用的事件
const backupStep2Data = "tp5er.backup.step2.data";
/**
* @return void
*/
public static function event()
{
tpEvent::listen(Event::backupStep1, function ($data) {
/**
* @var BackupInterface $backup
*/
list($backup, $filename, $table, $ret) = $data;
//在备份第一步执行
//Your TODO
});
tpEvent::listen(Event::backupStep2, function ($data) {
/**
* @var BackupInterface $backup
*/
list($backup, $filename, $table) = $data;
//Your TODO
});
tpEvent::listen(Event::backupStep2Data, function ($data) {
/**
* @var BackupInterface $backup
*/
list($backup, $filename, $sql, $lastPageOrIsBackupData) = $data;
//Your TODO
});
}