fix:数据备份

pg_adapter
wangxy 7 months ago
parent 0c1fff6f97
commit c81902e905

@ -1,11 +1,32 @@
package com.ruoyi.web.controller.monitor; package com.ruoyi.web.controller.monitor;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* druid * druid
* *
@ -23,4 +44,34 @@ public class DruidController extends BaseController
{ {
return redirect(prefix + "/index.html"); return redirect(prefix + "/index.html");
} }
@GetMapping("backups")
@ResponseBody
public AjaxResult backups(@RequestParam String username,
@RequestParam String password,
@RequestParam String hostname,
@RequestParam String port,
@RequestParam String databaseName) {
String sqlFileName = RuoYiConfig.getDataBaseBackUp() + ".sql";
File file = new File(File.separator + sqlFileName);
//目录生成
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
ProcessBuilder processBuilder = new ProcessBuilder("pg_dump", "-Fp", "-f", sqlFileName,
"\"host="+hostname, "port="+port, "user="+username, "password="+password, "dbname="+databaseName+"\"");
Process process = processBuilder.start();
if (process.waitFor() == 0) {
return AjaxResult.success("备份成功!");
} else {
return AjaxResult.error("备份失败!");
}
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.error("备份失败!");
}
}
} }

@ -121,4 +121,14 @@ public class RuoYiConfig
{ {
return getProfile() + "/upload"; return getProfile() + "/upload";
} }
/**
*
*
* @return java.lang.String
*/
public static String getDataBaseBackUp() {
return getProfile() + "/dbBackUp";
}
} }

Loading…
Cancel
Save