地质所 沉降监测网建设项目
zmk
2024-05-15 9e3afc6d0fa514f986d3fea40fa23124e6fb5070
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
package com.javaweb.web.controller.system;
 
 
import com.google.common.collect.Lists;
import com.javaweb.common.annotation.Log;
import com.javaweb.common.config.Global;
import com.javaweb.common.core.controller.BaseController;
import com.javaweb.common.core.domain.AjaxResult;
import com.javaweb.common.core.page.TableDataInfo;
import com.javaweb.common.enums.BusinessType;
import com.javaweb.common.utils.file.ReadDirector;
import com.javaweb.quartz.domain.SysJob;
import com.javaweb.quartz.service.ISysJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 数据库备份与恢复操作
 *
 * @author wujiyue
 * @date 2019-11-04
 */
@Controller
@RequestMapping("/system/backup")
public class BackupController extends BaseController {
    private String prefix = "system/backup";
    @Autowired
    private ISysJobService jobService;
 
    @RequiresPermissions("system:backup")
    @GetMapping()
    public String backup(ModelMap modelMap) {
        return prefix + "/backup";
    }
    @PostMapping("/files")
    @ResponseBody
    public TableDataInfo files() throws Exception
    {
        String folder= Global.getDbBackupPath();
        List<String> fs= ReadDirector.getFiles(folder);
 
        List<Map<String,Object>> files= Lists.newArrayList();
        Map<String,Object> temp=null;
        int i=0;
        String name="";
        String hourSeconds="";
        for(String s:fs){
            if(s.endsWith(".sql")){
                i++;
                temp = new HashMap<>();
                temp.put("rowNum", i);
                name = s;
                temp.put("name", name);
                name = name.replace("ry_plus_", "");
                hourSeconds = name.substring(11, 19);
                hourSeconds = hourSeconds.replace("_", ":");
                name = name.substring(0, 10);
 
                name = name.replace("_", "-");
                name = name + " " + hourSeconds;
                temp.put("time", name);
                files.add(temp);
            }
        }
        return getDataTable(files);
    }
 
    @RequiresPermissions("system:backup:backup")
    @Log(title = "数据库备份", businessType = BusinessType.DB_BACKUP)
    @PostMapping("/backup")
    @ResponseBody
    public AjaxResult backup() throws SchedulerException {
        SysJob job=new SysJob();
        //查看sys_job表的数据库备份任务的id
        job.setJobId(4L);
        job.setJobGroup("SYSTEM");
        jobService.run(job);
        return success();
    }
 
    /**
     * 数据库恢复
     */
    @RequiresPermissions("system:backup:recovery")
    @PostMapping("/recovery")
    @Log(title = "还原数据库", businessType = BusinessType.DB_RECOVERY)
    @ResponseBody
    public AjaxResult recovery(String name) {
        //在考虑这个功能有没有必要实现
        return error("该功能尚在研发中!");
    }
 
    /**
     * 删除备份文件
     */
    @RequiresPermissions("system:backup:delete")
    @Log(title = "删除备份文件", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String name) {
        String dir= Global.getDbBackupPath();
        String filePath=dir+File.separator+name;
        File file=new File(filePath);
        if(file.exists()){
            file.delete();
        }
        return success();
    }
 
 
}