|
@@ -0,0 +1,102 @@
|
|
|
|
|
+package com.ruoyi.web.controller.system;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+
|
|
|
|
|
+import com.ruoyi.common.config.RuoYiConfig;
|
|
|
|
|
+import com.ruoyi.common.core.controller.BaseController;
|
|
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 数据备份
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author ruoyi
|
|
|
|
|
+ */
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/system/backup")
|
|
|
|
|
+public class SysBackupController extends BaseController
|
|
|
|
|
+{
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(SysBackupController.class);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 手动触发数据备份
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('system:backup:execute')")
|
|
|
|
|
+ @PostMapping("/execute")
|
|
|
|
|
+ public AjaxResult executeBackup()
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ String backupDir = RuoYiConfig.getProfile() + "/backup";
|
|
|
|
|
+ File dir = new File(backupDir);
|
|
|
|
|
+ if (!dir.exists())
|
|
|
|
|
+ {
|
|
|
|
|
+ dir.mkdirs();
|
|
|
|
|
+ }
|
|
|
|
|
+ String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
|
|
|
|
+ String backupFile = backupDir + "/backup_" + timestamp + ".sql";
|
|
|
|
|
+
|
|
|
|
|
+ // 执行pg_dump备份
|
|
|
|
|
+ String dbUrl = "82.156.81.16";
|
|
|
|
|
+ String dbPort = "16804";
|
|
|
|
|
+ String dbName = "performance_distribution";
|
|
|
|
|
+ String dbUser = "postgres";
|
|
|
|
|
+
|
|
|
|
|
+ ProcessBuilder pb = new ProcessBuilder(
|
|
|
|
|
+ "pg_dump",
|
|
|
|
|
+ "-h", dbUrl,
|
|
|
|
|
+ "-p", dbPort,
|
|
|
|
|
+ "-U", dbUser,
|
|
|
|
|
+ "-d", dbName,
|
|
|
|
|
+ "-f", backupFile,
|
|
|
|
|
+ "--no-password"
|
|
|
|
|
+ );
|
|
|
|
|
+ pb.environment().put("PGPASSWORD", "Tjch@123");
|
|
|
|
|
+ Process process = pb.start();
|
|
|
|
|
+ int exitCode = process.waitFor();
|
|
|
|
|
+
|
|
|
|
|
+ if (exitCode == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.info("数据备份成功: {}", backupFile);
|
|
|
|
|
+ return success("数据备份成功,备份文件: " + backupFile);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("数据备份失败,退出码: {}", exitCode);
|
|
|
|
|
+ return error("数据备份失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("数据备份异常", e);
|
|
|
|
|
+ return error("数据备份异常: " + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取备份文件列表
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('system:backup:list')")
|
|
|
|
|
+ @GetMapping("/list")
|
|
|
|
|
+ public AjaxResult listBackups()
|
|
|
|
|
+ {
|
|
|
|
|
+ String backupDir = RuoYiConfig.getProfile() + "/backup";
|
|
|
|
|
+ File dir = new File(backupDir);
|
|
|
|
|
+ if (!dir.exists())
|
|
|
|
|
+ {
|
|
|
|
|
+ return success();
|
|
|
|
|
+ }
|
|
|
|
|
+ File[] files = dir.listFiles((d, name) -> name.endsWith(".sql"));
|
|
|
|
|
+ return success(files);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|