From 8ca27386c4aaa1d80ce4736d9fc096474d62616b Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Tue, 14 Dec 2021 11:56:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=9C=9F=E5=88=A0=E9=99=A4=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=A4=87=E4=BB=BD=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jpom/common/forward/NodeForward.java | 1 - .../system/BackupInfoController.java | 7 -- .../jpom/service/dblog/BackupInfoService.java | 81 +++++++++++++++---- .../io/jpom/system/ServerExtConfigBean.java | 10 +++ .../src/main/resources/bin/extConfig.yml | 2 + .../java/io/jpom/ApplicationStartTest.java | 13 ++- 6 files changed, 90 insertions(+), 24 deletions(-) diff --git a/modules/server/src/main/java/io/jpom/common/forward/NodeForward.java b/modules/server/src/main/java/io/jpom/common/forward/NodeForward.java index d0b226d4d..e7c4350e5 100644 --- a/modules/server/src/main/java/io/jpom/common/forward/NodeForward.java +++ b/modules/server/src/main/java/io/jpom/common/forward/NodeForward.java @@ -177,7 +177,6 @@ public class NodeForward { * @param nodeModel 插件端 */ private static AgentException responseException(Exception exception, NodeModel nodeModel) { - exception.printStackTrace(); String message = exception.getMessage(); Throwable cause = exception.getCause(); DefaultSystemLog.getLog().error("node [{}] connect failed...message: [{}]", nodeModel.getName(), message); diff --git a/modules/server/src/main/java/io/jpom/controller/system/BackupInfoController.java b/modules/server/src/main/java/io/jpom/controller/system/BackupInfoController.java index 840a787ef..d8958b1e0 100644 --- a/modules/server/src/main/java/io/jpom/controller/system/BackupInfoController.java +++ b/modules/server/src/main/java/io/jpom/controller/system/BackupInfoController.java @@ -94,13 +94,6 @@ public class BackupInfoController extends BaseServerController { @Feature(method = MethodFeature.DEL) @SystemPermission(superUser = true) public Object deleteBackup(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "数据 id 不能为空") String id) { - // 根据 id 查询备份信息 - BackupInfoModel backupInfoModel = backupInfoService.getByKey(id); - Objects.requireNonNull(backupInfoModel, "备份数据不存在"); - - // 删除对应的文件 - FileUtil.del(backupInfoModel.getFilePath()); - // 删除备份信息 backupInfoService.delByKey(id); return JsonMessage.toJson(200, "删除成功"); diff --git a/modules/server/src/main/java/io/jpom/service/dblog/BackupInfoService.java b/modules/server/src/main/java/io/jpom/service/dblog/BackupInfoService.java index 5e7397653..8fc9e5979 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/BackupInfoService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/BackupInfoService.java @@ -44,12 +44,14 @@ import io.jpom.service.h2db.H2BackupService; import io.jpom.system.ServerExtConfigBean; import io.jpom.system.db.DbConfig; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.io.File; import java.sql.SQLException; import java.util.List; +import java.util.Objects; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -73,23 +75,58 @@ public class BackupInfoService extends BaseDbService { * 检查数据库备份 */ public void checkAutoBackup() { - Integer autoBackupIntervalDay = ServerExtConfigBean.getInstance().getAutoBackupIntervalDay(); - if (autoBackupIntervalDay == null || autoBackupIntervalDay <= 0) { - return; - } - BackupInfoModel backupInfoModel = new BackupInfoModel(); - backupInfoModel.setBackupType(3); - List infoModels = super.queryList(backupInfoModel, 1, new Order("createTimeMillis", Direction.DESC)); - BackupInfoModel first = CollUtil.getFirst(infoModels); - if (first != null) { - Long createTimeMillis = first.getCreateTimeMillis(); - long interval = SystemClock.now() - createTimeMillis; - if (interval < TimeUnit.DAYS.toMillis(autoBackupIntervalDay)) { - return; + ServerExtConfigBean instance = ServerExtConfigBean.getInstance(); + // 创建备份 + this.createAutoBackup(instance); + // 删除历史备份 + this.deleteAutoBackup(instance); + } + + /** + * 删除历史 自动备份信息 + * + * @param instance 配置新 + */ + private void deleteAutoBackup(ServerExtConfigBean instance) { + Integer autoBackupReserveDay = instance.getAutoBackupReserveDay(); + if (autoBackupReserveDay != null && autoBackupReserveDay > 0) { + // + Entity entity = Entity.create(); + entity.set("backupType", 3); + entity.set("createTimeMillis", " < " + (SystemClock.now() - TimeUnit.DAYS.toMillis(autoBackupReserveDay))); + List entities = super.queryList(entity); + if (entities != null) { + for (Entity entity1 : entities) { + String id = entity1.getStr("id"); + this.delByKey(id); + } } } - // 执行数据库备份 - this.backupToSql(null, 3); + } + + /** + * 创建自动备份数据 + * + * @param instance 配置信息 + */ + private void createAutoBackup(ServerExtConfigBean instance) { + // 自动备份 + Integer autoBackupIntervalDay = instance.getAutoBackupIntervalDay(); + if (autoBackupIntervalDay != null && autoBackupIntervalDay > 0) { + BackupInfoModel backupInfoModel = new BackupInfoModel(); + backupInfoModel.setBackupType(3); + List infoModels = super.queryList(backupInfoModel, 1, new Order("createTimeMillis", Direction.DESC)); + BackupInfoModel first = CollUtil.getFirst(infoModels); + if (first != null) { + Long createTimeMillis = first.getCreateTimeMillis(); + long interval = SystemClock.now() - createTimeMillis; + if (interval < TimeUnit.DAYS.toMillis(autoBackupIntervalDay)) { + return; + } + } + // 执行数据库备份 + this.backupToSql(null, 3); + } } /** @@ -195,4 +232,18 @@ public class BackupInfoService extends BaseDbService { .distinct() .collect(Collectors.toList()); } + + @Override + public int delByKey(String keyValue) { + // 根据 id 查询备份信息 + BackupInfoModel backupInfoModel = super.getByKey(keyValue); + Objects.requireNonNull(backupInfoModel, "备份数据不存在"); + + // 删除对应的文件 + boolean del = FileUtil.del(backupInfoModel.getFilePath()); + Assert.state(del, "删除备份数据文件失败"); + + // 删除备份信息 + return super.delByKey(keyValue); + } } diff --git a/modules/server/src/main/java/io/jpom/system/ServerExtConfigBean.java b/modules/server/src/main/java/io/jpom/system/ServerExtConfigBean.java index dc122e221..c315e293d 100644 --- a/modules/server/src/main/java/io/jpom/system/ServerExtConfigBean.java +++ b/modules/server/src/main/java/io/jpom/system/ServerExtConfigBean.java @@ -94,6 +94,12 @@ public class ServerExtConfigBean implements DisposableBean { @Value("${db.autoBackupIntervalDay:1}") private Integer autoBackupIntervalDay; + /** + * 自动备份保留天数 小于等于 0,不自动删除自动备份数据 + */ + @Value("${db.autoBackupReserveDay:5}") + private Integer autoBackupReserveDay; + /** * author Hotstrip * 是否开启 web 访问数据库 @@ -292,6 +298,10 @@ public class ServerExtConfigBean implements DisposableBean { return autoBackupIntervalDay; } + public Integer getAutoBackupReserveDay() { + return autoBackupReserveDay; + } + /** * 单例 * diff --git a/modules/server/src/main/resources/bin/extConfig.yml b/modules/server/src/main/resources/bin/extConfig.yml index b4ebe06b8..5be287f19 100644 --- a/modules/server/src/main/resources/bin/extConfig.yml +++ b/modules/server/src/main/resources/bin/extConfig.yml @@ -50,6 +50,8 @@ db: cacheSize: # 自动备份间隔天数 小于等于 0 不自动备份 autoBackupIntervalDay: 1 + # 自动备份保留天数 小于等于 0,不自动删除自动备份数据 + autoBackupReserveDay: 5 # spring 相关配置 spring: h2: diff --git a/modules/server/src/test/java/io/jpom/ApplicationStartTest.java b/modules/server/src/test/java/io/jpom/ApplicationStartTest.java index d835246f5..76683bdbf 100644 --- a/modules/server/src/test/java/io/jpom/ApplicationStartTest.java +++ b/modules/server/src/test/java/io/jpom/ApplicationStartTest.java @@ -22,6 +22,7 @@ */ package io.jpom; +import io.jpom.service.dblog.BackupInfoService; import io.jpom.system.init.InitDb; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -31,6 +32,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; +import javax.annotation.Resource; + /** * @author Hotstrip * Test application start, then you can use such as service instance to test your methods @@ -38,12 +41,20 @@ import org.springframework.test.web.servlet.MockMvc; @SpringBootTest(classes = {JpomServerApplication.class, InitDb.class}) @AutoConfigureMockMvc public class ApplicationStartTest { - protected static Logger logger = LoggerFactory.getLogger(ApplicationStartTest.class); + protected static Logger logger = LoggerFactory.getLogger(ApplicationStartTest.class); @Autowired protected MockMvc mockMvc; + @Resource + private BackupInfoService backupInfoService; + @Test public void testApplicationStart() { logger.info("Jpom Server Application started....."); } + + @Test + public void testBackup() { + backupInfoService.checkAutoBackup(); + } }