use of io.jpom.model.data.BackupInfoModel in project Jpom by dromara.
the class BackupInfoService method backupToSql.
/**
* 备份数据库 SQL 文件
*
* @param tableNameList 需要备份的表名称列表,如果是全库备份,则不需要
*/
private void backupToSql(final List<String> tableNameList, BackupTypeEnum backupType) {
final String fileName = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), DatePattern.PURE_DATETIME_PATTERN);
// 设置默认备份 SQL 的文件地址
File file = FileUtil.file(DbConfig.getInstance().dbLocalPath(), Const.BACKUP_DIRECTORY_NAME, fileName + Const.SQL_FILE_SUFFIX);
final String backupSqlPath = FileUtil.getAbsolutePath(file);
// 数据源参数
final String url = DbConfig.getInstance().getDbUrl();
final String user = dbExtConfig.getUserName();
final String pass = dbExtConfig.getUserPwd();
JpomManifest instance = JpomManifest.getInstance();
// 先构造备份信息插入数据库
BackupInfoModel backupInfoModel = new BackupInfoModel();
String timeStamp = instance.getTimeStamp();
try {
DateTime parse = DateUtil.parse(timeStamp);
backupInfoModel.setBaleTimeStamp(parse.getTime());
} catch (Exception ignored) {
}
backupInfoModel.setName(fileName);
backupInfoModel.setVersion(instance.getVersion());
backupInfoModel.setBackupType(backupType.getCode());
backupInfoModel.setFilePath(backupSqlPath);
this.insert(backupInfoModel);
// 开启一个子线程去执行任务,任务完成之后修改对应的数据库备份信息
ThreadUtil.execute(() -> {
// 修改用的实体类
BackupInfoModel backupInfo = new BackupInfoModel();
BeanUtil.copyProperties(backupInfoModel, backupInfo);
try {
DefaultSystemLog.getLog().debug("start a new Thread to execute H2 Database backup...start");
IPlugin plugin = PluginFactory.getPlugin("db-h2");
Map<String, Object> map = new HashMap<>(10);
map.put("url", url);
map.put("user", user);
map.put("pass", pass);
map.put("backupSqlPath", backupSqlPath);
map.put("tableNameList", tableNameList);
plugin.execute("backupSql", map);
// h2BackupService.backupSql(url, user, pass, backupSqlPath, tableNameList);
// 修改备份任务执行完成
backupInfo.setFileSize(FileUtil.size(file));
backupInfo.setSha1Sum(SecureUtil.sha1(file));
backupInfo.setStatus(BackupStatusEnum.SUCCESS.getCode());
this.update(backupInfo);
DefaultSystemLog.getLog().debug("start a new Thread to execute H2 Database backup...success");
} catch (Exception e) {
// 记录错误日志信息,修改备份任务执行失败
DefaultSystemLog.getLog().error("start a new Thread to execute H2 Database backup...catch exception...", e);
backupInfo.setStatus(BackupStatusEnum.FAILED.getCode());
this.update(backupInfo);
}
});
}
use of io.jpom.model.data.BackupInfoModel in project Jpom by dromara.
the class BackupInfoController method uploadBackupFile.
/**
* 导入备份数据
*
* @return json
*/
@PostMapping(value = "/system/backup/upload")
@Feature(method = MethodFeature.UPLOAD)
@SystemPermission(superUser = true)
public Object uploadBackupFile() throws IOException {
MultipartFileBuilder multipartFileBuilder = createMultipart().addFieldName("file");
// 备份类型
// int backupType = Integer.parseInt(getParameter("backupType"));
// 存储目录
File directory = FileUtil.file(DbConfig.getInstance().dbLocalPath(), Const.BACKUP_DIRECTORY_NAME);
// 保存文件
multipartFileBuilder.setSavePath(FileUtil.getAbsolutePath(directory)).setUseOriginalFilename(false);
String backupSqlPath = multipartFileBuilder.save();
// 记录到数据库
final File file = new File(backupSqlPath);
String sha1Sum = SecureUtil.sha1(file);
BackupInfoModel backupInfoModel = new BackupInfoModel();
backupInfoModel.setSha1Sum(sha1Sum);
boolean exists = backupInfoService.exists(backupInfoModel);
if (exists) {
FileUtil.del(file);
return JsonMessage.getString(400, "导入的数据已经存在啦");
}
// backupInfoModel.setId(IdUtil.fastSimpleUUID());
backupInfoModel.setName(file.getName());
backupInfoModel.setBackupType(BackupTypeEnum.IMPORT.getCode());
backupInfoModel.setStatus(BackupStatusEnum.SUCCESS.getCode());
backupInfoModel.setFileSize(FileUtil.size(file));
backupInfoModel.setSha1Sum(sha1Sum);
backupInfoModel.setFilePath(backupSqlPath);
backupInfoService.insert(backupInfoModel);
return JsonMessage.toJson(200, "导入成功");
}
use of io.jpom.model.data.BackupInfoModel in project Jpom by dromara.
the class BackupInfoController method restoreBackup.
/**
* 还原备份数据
* 还原的时候不能异步了,只能等待备份还原成功或者失败
*
* @param id 备份 ID
* @return json
*/
@PostMapping(value = "/system/backup/restore")
@Feature(method = MethodFeature.EXECUTE)
public Object restoreBackup(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "数据 id 不能为空") String id) {
// 根据 id 查询备份信息
BackupInfoModel backupInfoModel = backupInfoService.getByKey(id);
Objects.requireNonNull(backupInfoModel, "备份数据不存在");
// 检查备份文件是否存在
File file = new File(backupInfoModel.getFilePath());
if (!FileUtil.exist(file)) {
return JsonMessage.toJson(400, "备份文件不存在");
}
// 清空 sql 加载记录
DbConfig.getInstance().clearExecuteSqlLog();
// 还原备份文件
boolean flag = backupInfoService.restoreWithSql(backupInfoModel.getFilePath());
if (flag) {
// 还原备份数据成功之后需要修改当前备份信息的状态(因为备份的时候该备份信息状态是备份中)
backupInfoModel.setFileSize(FileUtil.size(file));
backupInfoModel.setSha1Sum(SecureUtil.sha1(file));
backupInfoModel.setStatus(BackupStatusEnum.SUCCESS.getCode());
backupInfoService.update(backupInfoModel);
return JsonMessage.toJson(200, "还原备份数据成功");
}
return JsonMessage.toJson(400, "还原备份数据失败");
}
use of io.jpom.model.data.BackupInfoModel in project Jpom by dromara.
the class BackupInfoController method downloadBackup.
/**
* 下载备份数据
*
* @param id 备份 ID
*/
@GetMapping(value = "/system/backup/download")
@Feature(method = MethodFeature.DOWNLOAD)
public void downloadBackup(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "数据 id 不能为空") String id) {
// 根据 id 查询备份信息
BackupInfoModel backupInfoModel = backupInfoService.getByKey(id);
Objects.requireNonNull(backupInfoModel, "备份数据不存在");
// 检查备份文件是否存在
File file = new File(backupInfoModel.getFilePath());
if (!FileUtil.exist(file)) {
// DefaultSystemLog.getLog().error("文件不存在,无法下载...backupId: {}", id);
ServletUtil.write(getResponse(), JsonMessage.getString(404, "文件不存在,无法下载"), ContentType.JSON.toString());
return;
}
// 下载文件
ServletUtil.write(getResponse(), file);
}
use of io.jpom.model.data.BackupInfoModel in project Jpom by dromara.
the class BackupInfoService method createAutoBackup.
/**
* 创建自动备份数据
*/
private void createAutoBackup() {
// 自动备份
Integer autoBackupIntervalDay = dbExtConfig.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.autoBackup();
}
}
Aggregations