|
|
@@ -0,0 +1,326 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Cleanup\AdminControllers;
|
|
|
+
|
|
|
+use App\Module\Cleanup\Models\CleanupBackup;
|
|
|
+use App\Module\Cleanup\Repositories\CleanupBackupRepository;
|
|
|
+use App\Module\Cleanup\Enums\BACKUP_TYPE;
|
|
|
+use App\Module\Cleanup\Enums\COMPRESSION_TYPE;
|
|
|
+use App\Module\Cleanup\Enums\BACKUP_STATUS;
|
|
|
+use UCore\DcatAdmin\AdminController;
|
|
|
+use Dcat\Admin\Form;
|
|
|
+use Dcat\Admin\Grid;
|
|
|
+use Dcat\Admin\Show;
|
|
|
+use Dcat\Admin\Layout\Content;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 备份管理控制器
|
|
|
+ *
|
|
|
+ * @route /admin/cleanup/backups
|
|
|
+ */
|
|
|
+class CleanupBackupController extends AdminController
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 页面标题
|
|
|
+ */
|
|
|
+ protected $title = '备份管理';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据仓库
|
|
|
+ */
|
|
|
+ protected function repository()
|
|
|
+ {
|
|
|
+ return CleanupBackupRepository::class;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表页面
|
|
|
+ */
|
|
|
+ protected function grid(): Grid
|
|
|
+ {
|
|
|
+ return Grid::make(new CleanupBackupRepository(), function (Grid $grid) {
|
|
|
+ // 基础设置
|
|
|
+ $grid->column('id', 'ID')->sortable();
|
|
|
+ $grid->column('backup_name', '备份名称')->sortable();
|
|
|
+
|
|
|
+ // 关联信息
|
|
|
+ $grid->column('plan.plan_name', '关联计划')->sortable();
|
|
|
+ $grid->column('task.task_name', '关联任务')->sortable();
|
|
|
+
|
|
|
+ // 备份类型
|
|
|
+ $grid->column('backup_type', '备份类型')->using([
|
|
|
+ 1 => 'SQL',
|
|
|
+ 2 => 'JSON',
|
|
|
+ 3 => 'CSV',
|
|
|
+ ])->label([
|
|
|
+ 1 => 'primary',
|
|
|
+ 2 => 'info',
|
|
|
+ 3 => 'warning',
|
|
|
+ ])->sortable();
|
|
|
+
|
|
|
+ // 压缩类型
|
|
|
+ $grid->column('compression_type', '压缩类型')->using([
|
|
|
+ 1 => '无压缩',
|
|
|
+ 2 => 'GZIP',
|
|
|
+ 3 => 'ZIP',
|
|
|
+ ])->sortable();
|
|
|
+
|
|
|
+ // 备份状态
|
|
|
+ $grid->column('backup_status', '备份状态')->using([
|
|
|
+ 1 => '进行中',
|
|
|
+ 2 => '已完成',
|
|
|
+ 3 => '已失败',
|
|
|
+ ])->label([
|
|
|
+ 1 => 'info',
|
|
|
+ 2 => 'success',
|
|
|
+ 3 => 'danger',
|
|
|
+ ])->sortable();
|
|
|
+
|
|
|
+ // 大小信息
|
|
|
+ $grid->column('backup_size', '备份大小')->display(function ($value) {
|
|
|
+ return \App\Module\Cleanup\Helpers\FormatHelper::formatBytes($value);
|
|
|
+ })->sortable();
|
|
|
+
|
|
|
+ $grid->column('original_size', '原始大小')->display(function ($value) {
|
|
|
+ return \App\Module\Cleanup\Helpers\FormatHelper::formatBytes($value);
|
|
|
+ })->sortable();
|
|
|
+
|
|
|
+ // 统计信息
|
|
|
+ $grid->column('tables_count', '表数量')->sortable();
|
|
|
+ $grid->column('records_count', '记录数量')->display(function ($value) {
|
|
|
+ return number_format($value);
|
|
|
+ })->sortable();
|
|
|
+
|
|
|
+ // 时间信息
|
|
|
+ $grid->column('started_at', '开始时间')->sortable();
|
|
|
+ $grid->column('completed_at', '完成时间')->sortable();
|
|
|
+ $grid->column('expires_at', '过期时间')->sortable();
|
|
|
+
|
|
|
+ // 筛选器
|
|
|
+ $grid->filter(function (Grid\Filter $filter) {
|
|
|
+ $filter->equal('backup_type', '备份类型')->select([
|
|
|
+ 1 => 'SQL',
|
|
|
+ 2 => 'JSON',
|
|
|
+ 3 => 'CSV',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $filter->equal('compression_type', '压缩类型')->select([
|
|
|
+ 1 => '无压缩',
|
|
|
+ 2 => 'GZIP',
|
|
|
+ 3 => 'ZIP',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $filter->equal('backup_status', '备份状态')->select([
|
|
|
+ 1 => '进行中',
|
|
|
+ 2 => '已完成',
|
|
|
+ 3 => '已失败',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $filter->equal('plan_id', '关联计划')->select(
|
|
|
+ \App\Module\Cleanup\Models\CleanupPlan::pluck('plan_name', 'id')->toArray()
|
|
|
+ );
|
|
|
+
|
|
|
+ $filter->like('backup_name', '备份名称');
|
|
|
+ $filter->between('backup_size', '备份大小');
|
|
|
+ $filter->between('created_at', '创建时间')->datetime();
|
|
|
+ $filter->between('expires_at', '过期时间')->datetime();
|
|
|
+ });
|
|
|
+
|
|
|
+ // 行操作
|
|
|
+ $grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
|
+ $row = $actions->row;
|
|
|
+
|
|
|
+ if ($row->backup_status == 2) { // 已完成
|
|
|
+ $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\DownloadBackupAction());
|
|
|
+ $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\RestoreBackupAction());
|
|
|
+ }
|
|
|
+
|
|
|
+ $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\ViewBackupFilesAction());
|
|
|
+
|
|
|
+ if ($row->backup_status != 1) { // 非进行中状态可以删除
|
|
|
+ $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\DeleteBackupAction());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 批量操作
|
|
|
+ $grid->batchActions([
|
|
|
+ new \App\Module\Cleanup\AdminControllers\Actions\BatchDeleteBackupAction(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 工具栏
|
|
|
+ $grid->tools([
|
|
|
+ new \App\Module\Cleanup\AdminControllers\Actions\CleanExpiredBackupsAction(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 设置每页显示数量
|
|
|
+ $grid->paginate(20);
|
|
|
+
|
|
|
+ // 默认排序
|
|
|
+ $grid->model()->orderBy('created_at', 'desc');
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 详情页面
|
|
|
+ */
|
|
|
+ protected function detail($id): Show
|
|
|
+ {
|
|
|
+ return Show::make($id, new CleanupBackupRepository(), function (Show $show) {
|
|
|
+ $show->field('id', 'ID');
|
|
|
+ $show->field('backup_name', '备份名称');
|
|
|
+ $show->field('plan.plan_name', '关联计划');
|
|
|
+ $show->field('task.task_name', '关联任务');
|
|
|
+
|
|
|
+ $show->field('backup_type', '备份类型')->using([
|
|
|
+ 1 => 'SQL',
|
|
|
+ 2 => 'JSON',
|
|
|
+ 3 => 'CSV',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $show->field('compression_type', '压缩类型')->using([
|
|
|
+ 1 => '无压缩',
|
|
|
+ 2 => 'GZIP',
|
|
|
+ 3 => 'ZIP',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $show->field('backup_status', '备份状态')->using([
|
|
|
+ 1 => '进行中',
|
|
|
+ 2 => '已完成',
|
|
|
+ 3 => '已失败',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $show->field('backup_path', '备份路径');
|
|
|
+
|
|
|
+ // 大小信息
|
|
|
+ $show->field('backup_size', '备份大小')->display(function ($value) {
|
|
|
+ return \App\Module\Cleanup\Helpers\FormatHelper::formatBytes($value);
|
|
|
+ });
|
|
|
+ $show->field('original_size', '原始大小')->display(function ($value) {
|
|
|
+ return \App\Module\Cleanup\Helpers\FormatHelper::formatBytes($value);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 统计信息
|
|
|
+ $show->field('tables_count', '表数量');
|
|
|
+ $show->field('records_count', '记录数量')->display(function ($value) {
|
|
|
+ return number_format($value);
|
|
|
+ });
|
|
|
+
|
|
|
+ $show->field('backup_hash', '备份哈希');
|
|
|
+ $show->field('backup_config', '备份配置')->json();
|
|
|
+
|
|
|
+ // 时间信息
|
|
|
+ $show->field('started_at', '开始时间');
|
|
|
+ $show->field('completed_at', '完成时间');
|
|
|
+ $show->field('expires_at', '过期时间');
|
|
|
+
|
|
|
+ $show->field('error_message', '错误信息');
|
|
|
+ $show->field('created_by', '创建者ID');
|
|
|
+ $show->field('created_at', '创建时间');
|
|
|
+ $show->field('updated_at', '更新时间');
|
|
|
+
|
|
|
+ // 显示备份文件
|
|
|
+ $show->relation('files', '备份文件', function ($model) {
|
|
|
+ $grid = new Grid(new \App\Module\Cleanup\Models\CleanupBackupFile());
|
|
|
+ $grid->model()->where('backup_id', $model->id);
|
|
|
+
|
|
|
+ $grid->column('table_name', '表名');
|
|
|
+ $grid->column('file_name', '文件名');
|
|
|
+ $grid->column('file_size', '文件大小')->display(function ($value) {
|
|
|
+ return \App\Module\Cleanup\Helpers\FormatHelper::formatBytes($value);
|
|
|
+ });
|
|
|
+ $grid->column('backup_type', '备份类型')->using([
|
|
|
+ 1 => 'SQL',
|
|
|
+ 2 => 'JSON',
|
|
|
+ 3 => 'CSV',
|
|
|
+ ]);
|
|
|
+ $grid->column('compression_type', '压缩类型')->using([
|
|
|
+ 1 => '无压缩',
|
|
|
+ 2 => 'GZIP',
|
|
|
+ 3 => 'ZIP',
|
|
|
+ ]);
|
|
|
+ $grid->column('created_at', '创建时间');
|
|
|
+
|
|
|
+ $grid->disableActions();
|
|
|
+ $grid->disableCreateButton();
|
|
|
+ $grid->disableFilter();
|
|
|
+ $grid->disablePagination();
|
|
|
+
|
|
|
+ return $grid;
|
|
|
+ });
|
|
|
+
|
|
|
+ // 显示SQL备份内容(如果是SQL备份)
|
|
|
+ if ($show->getModel()->backup_type == 1) {
|
|
|
+ $show->relation('sqlBackups', 'SQL备份内容', function ($model) {
|
|
|
+ $grid = new Grid(new \App\Module\Cleanup\Models\CleanupSqlBackup());
|
|
|
+ $grid->model()->where('backup_id', $model->id);
|
|
|
+
|
|
|
+ $grid->column('table_name', '表名');
|
|
|
+ $grid->column('records_count', '记录数量')->display(function ($value) {
|
|
|
+ return number_format($value);
|
|
|
+ });
|
|
|
+ $grid->column('content_size', '内容大小')->display(function ($value) {
|
|
|
+ return \App\Module\Cleanup\Helpers\FormatHelper::formatBytes($value);
|
|
|
+ });
|
|
|
+ $grid->column('created_at', '创建时间');
|
|
|
+
|
|
|
+ $grid->disableActions();
|
|
|
+ $grid->disableCreateButton();
|
|
|
+ $grid->disableFilter();
|
|
|
+ $grid->disablePagination();
|
|
|
+
|
|
|
+ return $grid;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 表单(只读,备份不允许手动创建/编辑)
|
|
|
+ */
|
|
|
+ protected function form(): Form
|
|
|
+ {
|
|
|
+ return Form::make(new CleanupBackupRepository(), function (Form $form) {
|
|
|
+ $form->display('id', 'ID');
|
|
|
+ $form->display('backup_name', '备份名称');
|
|
|
+ $form->display('plan.plan_name', '关联计划');
|
|
|
+ $form->display('task.task_name', '关联任务');
|
|
|
+
|
|
|
+ $form->display('backup_type', '备份类型')->using([
|
|
|
+ 1 => 'SQL',
|
|
|
+ 2 => 'JSON',
|
|
|
+ 3 => 'CSV',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $form->display('compression_type', '压缩类型')->using([
|
|
|
+ 1 => '无压缩',
|
|
|
+ 2 => 'GZIP',
|
|
|
+ 3 => 'ZIP',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $form->display('backup_status', '备份状态')->using([
|
|
|
+ 1 => '进行中',
|
|
|
+ 2 => '已完成',
|
|
|
+ 3 => '已失败',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $form->display('backup_path', '备份路径');
|
|
|
+ $form->display('backup_size', '备份大小');
|
|
|
+ $form->display('original_size', '原始大小');
|
|
|
+ $form->display('tables_count', '表数量');
|
|
|
+ $form->display('records_count', '记录数量');
|
|
|
+ $form->display('backup_hash', '备份哈希');
|
|
|
+
|
|
|
+ $form->display('started_at', '开始时间');
|
|
|
+ $form->display('completed_at', '完成时间');
|
|
|
+ $form->display('expires_at', '过期时间');
|
|
|
+ $form->display('error_message', '错误信息');
|
|
|
+ $form->display('created_at', '创建时间');
|
|
|
+ $form->display('updated_at', '更新时间');
|
|
|
+
|
|
|
+ // 禁用所有操作
|
|
|
+ $form->disableCreatingCheck();
|
|
|
+ $form->disableEditingCheck();
|
|
|
+ $form->disableViewCheck();
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|