数据库备份是没有问题的,而且在数据了较大的情况下速度也不是特别慢。具体代码:
public JsonView dobackup() throws Exception {
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String date = sDateFormat.format(new java.util.Date());
if (exportDatabaseTool("localhost", "msql用户名", "mysql密码",
"C:/BACKUPDATABASE", "数据库名" + date + ".sql",
"flight_health")) {
System.out.println("数据库备份成功!!!");
} else {
System.out.println("数据库备份失败!!!");
}
return new JsonView();
}
public static boolean exportDatabaseTool(String hostIP, String userName,
String password, String savePath, String fileName,
String databaseName) throws Exception {
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目录不存在
saveFile.mkdirs();// 创建文件夹
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
try {
printWriter = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(savePath + fileName), "utf8"));
Process process = Runtime.getRuntime().exec(
" mysqldump -h" + hostIP + " -u" + userName + " -p"
+ password + " --set-charset=UTF8 " + databaseName);
InputStreamReader inputStreamReader = new InputStreamReader(
process.getInputStream(), "utf8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
printWriter.println(line);
}
printWriter.flush();
if (process.waitFor() == 0) {// 0 表示线程正常终止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
数据库恢复,问题比较大,之前是使用和数据库备份类似的恢复方式,虽然最后会返回数据库恢复成功!!但是实际并没有正确执行代码,参考了网上很多的相似方法都不行,都会报管道错误问题问题或者是不报问题也不执行代码 之后参考 大神版本
public void recover(MultipartFile file) throws Exception {
Map<String, Object> detailMap = new HashMap();
String filename = file.getOriginalFilename();
String filePath = request.getSession().getServletContext().getRealPath("/") + "downloadfile/"
+ file.getOriginalFilename(); //创建文件
file.transferTo(new File(filePath)); // 转存文件
Runtime rt = Runtime.getRuntime();
Process pro = rt.exec(getCommand(filePath));
BufferedReader br = new BufferedReader(new InputStreamReader(pro.getErrorStream()));
String errorLine = null;
while ((errorLine = br.readLine()) != null) {
System.out.println(errorLine);
}
br.close();
int result = pro.waitFor();
if (result != 0) {
throw new Exception("数据库恢复失败! ");
}
}
private String[] getCommand(String filePath) {
String[] cmd = new String[3];
String os = System.getProperties().getProperty("os.name");
if (os.startsWith("Win")) {
cmd[0] = "cmd.exe";
cmd[1] = "/c";
} else {
cmd[0] = "/bin/sh";
cmd[1] = "-c";
}
StringBuilder arg = new StringBuilder();
arg.append("mysql ");
arg.append("-uroot ");
arg.append("-proot 数据库名"); //之前没有数据库名会报错说 找不到该数据库
arg.append(" < ");
arg.append(getFile(filePath));
cmd[2] = arg.toString();
return cmd;
}
private String getFile(String filePath) {
File dir = new File(filePath);
return dir.getAbsolutePath().toString();
}