mirror of
https://gitee.com/dromara/Jpom.git
synced 2024-12-03 04:18:21 +08:00
定期删除自动备份数据
This commit is contained in:
parent
1a18cc628e
commit
8ca27386c4
@ -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);
|
||||
|
@ -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, "删除成功");
|
||||
|
@ -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<BackupInfoModel> {
|
||||
* 检查数据库备份
|
||||
*/
|
||||
public void checkAutoBackup() {
|
||||
Integer autoBackupIntervalDay = ServerExtConfigBean.getInstance().getAutoBackupIntervalDay();
|
||||
if (autoBackupIntervalDay == null || autoBackupIntervalDay <= 0) {
|
||||
return;
|
||||
}
|
||||
BackupInfoModel backupInfoModel = new BackupInfoModel();
|
||||
backupInfoModel.setBackupType(3);
|
||||
List<BackupInfoModel> 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<Entity> 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<BackupInfoModel> 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<BackupInfoModel> {
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单例
|
||||
*
|
||||
|
@ -50,6 +50,8 @@ db:
|
||||
cacheSize:
|
||||
# 自动备份间隔天数 小于等于 0 不自动备份
|
||||
autoBackupIntervalDay: 1
|
||||
# 自动备份保留天数 小于等于 0,不自动删除自动备份数据
|
||||
autoBackupReserveDay: 5
|
||||
# spring 相关配置
|
||||
spring:
|
||||
h2:
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user