@Scheduled(cron = "0 0 2 * * ? ") @GetMapping("/coco") // 备份 public static void Database() throws UnsupportedEncodingException { StringBuilder cmd = new StringBuilder(); //mysql bin目录 cmd.append("D:\\mysql57\\bin").append("\\mysqldump").append(" --opt") //本机 .append(" --host=").append("localhost") //本库 .append(" --databases ").append("archivesv2") .append(" --user=").append("root") .append(" --password=").append("") //输出到某个文件夹 .append(" --result-file=").append("D:\\sqlData\\DatabaseSource.sql") .append(" --default-character-set=utf8 "); String s = new String(cmd.toString().getBytes("ISO-8859-1"),"UTF-8"); try { //调用外部执行exe文件的javaAPI Runtime.getRuntime().exec(s); } catch (IOException e) { e.printStackTrace(); } }
@GetMapping("/recovery") // 恢复 public AjaxResult recovery (){ try { Runtime runtime = Runtime.getRuntime(); Process process = runtime .exec( "D:\\mysql57\\bin\\mysql -hlocalhost -uroot -p --default-character-set=utf8 " + "archivesv2"); OutputStream outputStream = process.getOutputStream(); BufferedReader br = new BufferedReader( new InputStreamReader( //执行那一条(.sql文件) new FileInputStream( "D:\\sqlData\\DatabaseSource.sql" ), "utf-8" )); String str = null ; StringBuffer sb = new StringBuffer(); while ((str = br.readLine()) != null ) { sb.append(str + "\r\n" ); } str = sb.toString(); OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8" ); writer.write(str); writer.flush(); outputStream.close(); br.close(); writer.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return AjaxResult.success("恢复成功"); }