From c467370d8741bc05ca7c22d4253c58ac0a979ef5 Mon Sep 17 00:00:00 2001
From: rk <94314517@qq.com>
Date: 星期二, 09 六月 2026 09:46:25 +0800
Subject: [PATCH] 功能优化
---
server/visits/admin_timer/src/main/java/com/doumee/api/DatabaseController.java | 97 +++++++++++++++++++++++++++++-------------------
1 files changed, 59 insertions(+), 38 deletions(-)
diff --git a/server/visits/admin_timer/src/main/java/com/doumee/api/DatabaseController.java b/server/visits/admin_timer/src/main/java/com/doumee/api/DatabaseController.java
index e2c58ae..6be889d 100644
--- a/server/visits/admin_timer/src/main/java/com/doumee/api/DatabaseController.java
+++ b/server/visits/admin_timer/src/main/java/com/doumee/api/DatabaseController.java
@@ -1,7 +1,7 @@
package com.doumee.api;
import com.doumee.biz.system.SystemDictDataBiz;
-import com.doumee.core.model.ApiResponse;
+import com.doumee.service.business.third.model.ApiResponse;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Date;
import com.doumee.core.utils.DateUtil;
@@ -10,7 +10,8 @@
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -18,10 +19,12 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.concurrent.Executor;
/**
* @author 姹熻箘韫�
@@ -34,50 +37,68 @@
public class DatabaseController extends BaseController {
@Autowired
+ @Qualifier("asyncExecutor")
+ private Executor asyncExecutor;
+ @Autowired
private PlatformJobService platformJobService;
@Autowired
private SystemDictDataBiz systemDictDataBiz;
@ApiOperation("鏁版嵁搴撳浠斤紝淇濈暀鏈�杩�7涓浠絪ql")
- @GetMapping("/backupDatabase")
+ @PostMapping("/backupDatabase")
public ApiResponse backupDatabase() {
- try {
- String timestamp = DateUtil.getNowLongTime();
-// String path = "/usr/local/jars/db/";
-// String backupPath = path + timestamp + ".sql";
- String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_DIR).getCode();
- String code = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_CDOE).getCode() ;
- String backupPath = path + timestamp + ".sql";
- code= code.replace("${param}",backupPath);
- log.error("鏁版嵁搴撳浠�================: " + code);
- ProcessBuilder builder = new ProcessBuilder(code);
- // 閲嶅畾鍚戦敊璇祦鍒版爣鍑嗚緭鍑烘祦
- builder.redirectErrorStream(true);
- // stdout
- Process process = builder.start();
- new Thread(new ProcessHandleRunnable(process)).start();
- process.waitFor(); // wait if needed
+ log.info("瀹氭椂浠诲姟鎵ц寮�濮嬶細backupDatabase-鏁版嵁搴撳浠�");
+ asyncExecutor.execute(() -> {
+ try {
+ String timestamp = DateUtil.getNowLongTime();
+ String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_DIR).getCode();
+ String code = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_CDOE).getCode() ;
+ String backupPath = path + timestamp + ".sql";
+ code= code.replace("${param}",backupPath);
+ log.info("鏁版嵁搴撳浠藉懡浠�: {}", code);
+ ProcessBuilder builder = new ProcessBuilder(code);
+ builder.redirectErrorStream(true);
+ Process process = builder.start();
+ new Thread(new ProcessHandleRunnable(process)).start();
+ process.waitFor();
- /* // 浣跨敤mysqldump鍛戒护杩涜鏁版嵁搴撳浠�
- Process process = Runtime.getRuntime().exec(code);
- process.getErrorStream();
- process.getInputStream();
- process.waitFor();*/
-
- // 妫�鏌ュ浠芥槸鍚︽垚鍔�
- if (new File(backupPath).exists()) {
- log.info("鏁版嵁搴撳浠芥垚鍔�: " + backupPath);
- return ApiResponse.success("鏁版嵁搴撳浠芥垚鍔�: " + backupPath);
- } else {
- log.error("鏁版嵁搴撳浠藉け璐�.");
+ if (new File(backupPath).exists()) {
+ log.info("鏁版嵁搴撳浠芥垚鍔�: {}", backupPath);
+ } else {
+ log.error("鏁版嵁搴撳浠藉け璐�.");
+ }
+ deleteOldFiles(path,7);
+ } catch (Exception e) {
+ log.error("鏁版嵁搴撳浠藉け璐�", e);
}
- deleteOldFiles(path,7);//淇濈暀鏈�杩�7涓枃浠�
+ });
+ return ApiResponse.success("鏁版嵁搴撳浠藉凡鎻愪氦");
+ }
+ public void run() {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
+ String currentDateTime = dateFormat.format(new Date());
+ String fileName = "backup_" + currentDateTime + ".sql";
+
+ try {
+ Class.forName("com.mysql.cj.jdbc.Driver");
+ Connection connection = DriverManager.getConnection("localhost:3306/antaiwuliu", "root", "Atwl@2024");
+ Statement statement = connection.createStatement();
+
+ String query = "SELECT * INTO OUTFILE '" + fileName + "' FROM your_table";
+ statement.execute(query);
+
+ System.out.println("Database backup successful to " + fileName);
+
+ statement.close();
+ connection.close();
} catch (Exception e) {
e.printStackTrace();
- log.error("鏁版嵁搴撳浠藉け璐�."+e.getMessage());
}
- return ApiResponse.failed("鏁版嵁搴撳浠藉け璐�" );
+ }
+
+ public static void main(String[] args) {
+ new DatabaseController().run();;
}
static class ProcessHandleRunnable implements Runnable {
--
Gitblit v1.9.3