From c81902e9054bdf99c542a0c6d75d8f70784723bc Mon Sep 17 00:00:00 2001 From: wangxy <1356089412@qq.com> Date: Fri, 13 Sep 2024 11:40:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=95=B0=E6=8D=AE=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/monitor/DruidController.java | 55 ++++++++++++++++++- .../com/ruoyi/common/config/RuoYiConfig.java | 10 ++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/DruidController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/DruidController.java index a51bf20..3020d00 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/DruidController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/DruidController.java @@ -1,11 +1,32 @@ 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.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; 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 监控 * @@ -23,4 +44,34 @@ public class DruidController extends BaseController { 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("备份失败!"); + } + } + } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java index 46a5734..37655d9 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java @@ -121,4 +121,14 @@ public class RuoYiConfig { return getProfile() + "/upload"; } + + /** + * + * 数据备份路径方法 + * @return java.lang.String + */ + + public static String getDataBaseBackUp() { + return getProfile() + "/dbBackUp"; + } }