1
YM
2025-04-10 c4782840b8893dcc0bcf262153374237f40f6b9e
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="p-page">
    <table class="layui-hide" id="backup" lay-filter="backup"></table>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <span class="layui-btn layui-btn-sm layui-btn-normal" lay-event="backup">数据备份</span><span class="layui-btn layui-btn-sm" lay-event="optimize">数据表优化</span><span class="layui-btn layui-btn-danger layui-btn-sm" lay-event="repair">数据表修复</span>
      </div>
    <span id="dataTips" style="font-size:12px; margin-left:10px"></span>  
</script>
{/block}
<!-- /主体 -->
<!-- 脚本 -->
{block name="script"}
<script>
    function gouguInit() {
        var table = layui.table, form = layui.form;
        var tableIns = table.render({
            elem: '#backup',
            title: '数据备份',
            toolbar: '#toolbarDemo',
            defaultToolbar: false,
            url: "/home/database/database", //数据接口
            page: false,
            cols: [
                [ //表头
                    { type: 'checkbox' },
                    {
                        field: 'name',
                        title: '数据表',
                        width: 200
                    },
                    {
                        field: 'status',
                        title: '状态',
                        width: 100,
                        templet: function (d) {
                            return '<span id="table_' + d.name + '">-</span>';
                        }
                    },
                    {
                        field: 'engine',
                        title: '存储引擎',
                        align: 'center',
                        width: 80
                    }, {
                        field: 'row_format',
                        title: '行格式',
                        align: 'center',
                        width: 80
                    }, {
                        field: 'rows',
                        title: '行数',
                        align: 'center',
                        width: 60,
                    }, {
                        field: 'data_size',
                        title: '字节数',
                        align: 'center',
                        width: 80
                    }, {
                        field: 'data_length',
                        title: '数据大小',
                        align: 'center',
                        width: 80
                    }, {
                        field: 'comment',
                        title: '数据表注释'
                    }, {
                        field: 'create_time',
                        title: '创建时间',
                        width: 160,
                        align: 'center'
                    }
                ]
            ],
            done: function (res, curr, count) {
                $('#dataTips').html(res.msg);
            }
        });
 
        //递归备份表
        function backup(tab, status, table) {
            layer.closeAll();
            layer.msg("备份中...,请勿关闭本页面", { time: 200000 });
            $.get("/home/database/backup", tab, function (data) {
                if (data.code == 0) {
                    showmsg(data.data.tab.table, data.msg);
                    if (data.data.tab.start == 'ok') {
                        layer.msg('备份完成');
                        window.onbeforeunload = function () { return null }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    layer.msg('立即备份');
                }
            }, "json");
        }
 
        //修改备份状态
        function showmsg(table, msg) {
            console.log(table);
            $("#table_" + table).addClass('span-color-2').html(msg);
        }
        //监听行工具事件
        table.on('toolbar(backup)', function (obj) {
            var checkData = table.checkStatus(obj.config.id).data;
            var len = checkData.length;
            var tables = [];
            if (len == 0) {
                layer.msg('请先选择表');
                return false;
            }
            for (var i = 0; i < len; i++) {
                tables.push(checkData[i].name);
            }
            if (obj.event === 'backup') {
                layer.confirm('确认要备份选中的' + len + '个数据表吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        url: "/home/database/backup",
                        type: 'post',
                        data: { 'tables': tables },
                        success: function (res) {
                            if (res.code == 0) {
                                layer.msg("开始备份,请不要关闭本页面!");
                                window.onbeforeunload = function () { return "正在备份数据库,请不要关闭!" }
                                backup(res.data.tab);
                            }
                            if (res.code == 2) {
                                layer.confirm('检测到有一个备份任务未完成,请先清除未完成的备份', {
                                    icon: 3,
                                    title: '提示'
                                }, function (index) {
                                    $.ajax({
                                        url: "/home/database/del",
                                        data: { 'lock': 1 },
                                        success: function (res) {
                                            layer.msg(res.msg);
                                        }
                                    })
                                })
                            }
                            else {
                                layer.msg(res.msg);
                            }
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'optimize') {
                layer.confirm('确认要优化选中的' + len + '个数据表吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        url: "/home/database/optimize",
                        type: 'post',
                        data: { 'tables': tables },
                        success: function (res) {
                            layer.msg(res.msg);
                            if (res.code == 0) {
                                setTimeout(function () {
                                    location.reload();
                                }, 2000)
                            }
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'repair') {
                layer.confirm('确认要修复选中的' + len + '个数据表吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        url: "/home/database/repair",
                        type: 'post',
                        data: { 'tables': tables },
                        success: function (res) {
                            layer.msg(res.msg);
                            if (res.code == 0) {
                                setTimeout(function () {
                                    location.reload();
                                }, 2000)
                            }
                        }
                    })
                    layer.close(index);
                });
            }
        });
    }
</script>
{/block}
<!-- /脚本 -->