备份,还原数据

备份数据

/**
     * 备份数据
     */
    @PostMapping("/backup")
    public AjaxResult backupData(String content) throws Exception {
         Map<String, String> map = new HashMap<String, String>() {
            {
                put("人员", "mis_personnel");
                put("用户", "sys_user");
                put("角色", "sys_role");
                put("计量保障单位", "mis_inspected_unit");
                put("计量保障目录", "mis_measurement_guarantee_catalogue");
                put("计量保障设备", "mis_metering_support_equipment");
            }
        };
                String [] tableNamesString=content.split(",");
                int length=tableNamesString.length;
                String [] tableNames=new String[length];
                for (int i=0;i<length;i++) {
                    map.containsKey(tableNames[i]);
                    tableNames[i]= map.get(tableNamesString[i]);
                }
                StringBuffer sb = new StringBuffer();
                for(int i = 0; i < tableNames.length; i++){
                    sb. append(tableNames[i]+",");
                }
                sb.deleteCharAt(sb.length() - 1); //去除最后一个逗号
                String str = sb.toString();
                 File file = new File("D:\\test");
                 if ( !file.exists() ){
                         file.mkdir();
                    }
                 String fileNameStr=str.replace(",","_");
                 File datafile = new File(file+File.separator+""+fileNameStr+".sql");
                 if( datafile.exists() ){
                        System.out.println("文件名已存在,请更换");
                        return AjaxResult.error("文件名已存在,请更换");
                     }
                 //拼接cmd命令
                 str=str.replace(","," ");
                 Process exec = Runtime.getRuntime().exec("cmd /c mysqldump -u root -pquanji123. mis_db "+str+" > "+datafile);
                 if( exec.waitFor() == 0){
                         System.out.println("数据库备份成功,备份路径为:"+datafile);
                     }
                 return AjaxResult.success("备份成功");
             }

还原数据

	@PostMapping("/reduction")
    public AjaxResult reductionData(Long id,String content) throws Exception {

                Map<String, String> map = new HashMap<String, String>() {
                    {
                        put("人员", "mis_personnel");
                        put("用户", "sys_user");
                        put("角色", "sys_role");
                        put("计量保障单位", "mis_inspected_unit");
                        put("计量保障目录", "mis_measurement_guarantee_catalogue");
                        put("计量保障设备", "mis_metering_support_equipment");
                    }
                };
                if (id==null){
                    return AjaxResult.error("请选择需要还原的数据!");
                }else{
                String [] tableNamesString=content.split(",");
                int length=tableNamesString.length;
                String [] tableNames=new String[length];
                for (int i=0;i<length;i++) {
                    map.containsKey(tableNames[i]);
                    tableNames[i]= map.get(tableNamesString[i]);
                }
                StringBuffer sb = new StringBuffer();
                for(int i = 0; i < tableNames.length; i++){
                    sb. append(tableNames[i]+",");
                }
                sb.deleteCharAt(sb.length() - 1); //去除最后一个逗号
                String str = sb.toString();
                String fileNameStr=str.replace(",","_");
                File datafile = new File("D:\\test\\"+""+fileNameStr+".sql");
                if( !datafile.exists() ){
                    System.out.println("文件不存在,请检查");
                    return AjaxResult.error("文件不存在,请检查");
                }
                //拼接cmd命令
                Process exec = Runtime.getRuntime().exec("cmd /c mysql -u root -pquanji123. mis_db  < "+datafile);
                if( exec.waitFor() == 0){
                    System.out.println("数据库还原成功,还原的文件为:"+datafile);
                }
                return AjaxResult.success("还原成功");
                }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值