备份数据
@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("文件名已存在,请更换");
}
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("文件不存在,请检查");
}
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("还原成功");
}
}