记录错误:
因为我的mysqldump.exe地址在C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe,结果这个路径上有Program Files,MySQL Server 5.7,这两个有空格啊啊啊啊。
解决方法:把mysqldump.exe复制到没有空格的路径,比如直接C:/mysqldump.exe就行了。
记录一下,感觉涉及mysql,路径上别有空格
这是我的实现:
package com.example.springboot_pet_home.controller;
import com.example.springboot_pet_home.common.Result;
import com.example.springboot_pet_home.dto.CartDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.util.List;
@RestController
@RequestMapping(value = "/baskups")
public class DatebaseController {
String mysqldump_path = "C:\\mysqldump.exe";
String mysql_path="C:\\mysql.exe";
String sql_ip=" ";
String sql_port=" ";
String sql_account=" ";
String sql_pwd=" ";
String dbName=" ";
String filePath="D:\\pet_home_sql_backup";
String filename=dbName + new java.util.Date().getTime() + ".sql";
String backup_command = mysqldump_path+
" -h" + sql_ip +
" -P" + sql_port +
" -u" + sql_account +
" -p" + sql_pwd +
dbName +
" > " + filePath +
"\\" + filename;
String restore_command= mysql_path+
" -h" + sql_ip +
" -P" + sql_port +
" -u" + sql_account +
" -p" + sql_pwd +
dbName +
" < " + filePath +
"\\" + filename;
@GetMapping("/backup")
public Result backup() {
// 设置路径对应的文件夹的写入权限
File backupDir = new File(filePath);
Result<List<CartDto>> result = new Result<>();
if (!backupDir.exists() && !backupDir.mkdirs()) {
result.setCode(400);
result.setMsg("error");
}
backupDir.setWritable(true, false);
try {
Process process = Runtime.getRuntime().exec(backup_command);
result.setCode(200);
result.setMsg(filename);
} catch (Exception e ) {
e.printStackTrace();
result.setCode(400);
result.setMsg("error");
}
return result;
}
@GetMapping("/restore")
public Result restore() {
// 设置路径对应的文件夹的写入权限
File backupDir = new File(filePath);
Result<List<CartDto>> result = new Result<>();
if (!backupDir.exists() && !backupDir.mkdirs()) {
result.setCode(400);
result.setMsg("error");
}
backupDir.setWritable(true, false);
try {
Process process = Runtime.getRuntime().exec(restore_command);
result.setCode(200);
result.setMsg(filename);
} catch (Exception e) {
e.printStackTrace();
result.setCode(400);
result.setMsg("error");
}
return result;
}
}