use of io.jpom.plugin.IPlugin in project Jpom by dromara.
the class DbConfig method recoverDb.
/**
* 恢复数据库
*/
public void recoverDb() throws Exception {
File dbLocalPathFile = this.dbLocalPath();
if (!FileUtil.exist(dbLocalPathFile)) {
return;
}
String dbName = this.getDbName();
File recoverBackup = FileUtil.file(dbLocalPathFile, "recover_backup", DateTime.now().toString());
//
IPlugin plugin = PluginFactory.getPlugin("db-h2");
Map<String, Object> map = new HashMap<>(10);
map.put("dbName", dbName);
map.put("dbPath", dbLocalPathFile);
map.put("recoverBackup", recoverBackup);
File backupSql = (File) plugin.execute("recoverToSql", map);
// 清空记录
this.clearExecuteSqlLog();
// 记录恢复的 sql
this.recoverSqlFile = backupSql;
}
use of io.jpom.plugin.IPlugin in project Jpom by dromara.
the class DbConfig method executeRecoverDbSql.
/**
* 恢复数据库
*
* @param dsFactory 数据库连接
*/
public void executeRecoverDbSql(DSFactory dsFactory) throws Exception {
if (!FileUtil.isFile(this.recoverSqlFile)) {
return;
}
//
IPlugin plugin = PluginFactory.getPlugin("db-h2");
Map<String, Object> map = new HashMap<>(10);
map.put("backupSqlPath", FileUtil.getAbsolutePath(this.recoverSqlFile));
map.put("dataSource", dsFactory.getDataSource());
plugin.execute("restoreBackupSql", map);
}
use of io.jpom.plugin.IPlugin in project Jpom by dromara.
the class BackupInfoService method restoreWithSql.
/**
* 根据 SQL 文件还原数据库
* 还原数据库时只能同步,防止该过程中修改数据造成数据不一致
*
* @param backupSqlPath 备份 sql 文件地址
*/
public boolean restoreWithSql(String backupSqlPath) {
try {
long startTs = System.currentTimeMillis();
IPlugin plugin = PluginFactory.getPlugin("db-h2");
Map<String, Object> map = new HashMap<>(10);
map.put("backupSqlPath", backupSqlPath);
plugin.execute("restoreBackupSql", map);
// h2BackupService.restoreBackupSql(backupSqlPath);
long endTs = System.currentTimeMillis();
DefaultSystemLog.getLog().debug("restore H2 Database backup...success...cast {} ms", endTs - startTs);
return true;
} catch (Exception e) {
// 记录错误日志信息,返回数据库备份还原执行失败
DefaultSystemLog.getLog().error("restore H2 Database backup...catch exception...message: {}, cause: {}", e.getMessage(), e.getCause());
return false;
}
}
use of io.jpom.plugin.IPlugin in project Jpom by dromara.
the class MonitorItem method sendNotifyMsgToWebhook.
private void sendNotifyMsgToWebhook(MonitorNotifyLog monitorNotifyLog, NodeModel nodeModel, ProjectInfoCacheModel projectInfoCacheModel, String webhook) {
if (StrUtil.isEmpty(webhook)) {
return;
}
IPlugin plugin = PluginFactory.getPlugin("webhook");
Map<String, Object> map = new HashMap<>(10);
map.put("monitorId", monitorModel.getId());
map.put("monitorName", monitorModel.getName());
map.put("nodeId", monitorNotifyLog.getNodeId());
map.put("nodeName", nodeModel.getName());
map.put("runStatus", monitorNotifyLog.getStatus());
map.put("projectId", monitorNotifyLog.getProjectId());
if (projectInfoCacheModel != null) {
map.put("projectName", projectInfoCacheModel.getName());
}
map.put("title", monitorNotifyLog.getTitle());
map.put("content", monitorNotifyLog.getContent());
//
monitorNotifyLog.setNotifyStyle(MonitorModel.NotifyType.webhook.getCode());
monitorNotifyLog.setNotifyObject(webhook);
//
dbMonitorNotifyLogService.insert(monitorNotifyLog);
String logId = monitorNotifyLog.getId();
ThreadUtil.execute(() -> {
try {
plugin.execute(webhook, map);
dbMonitorNotifyLogService.updateStatus(logId, true, null);
} catch (Exception e) {
DefaultSystemLog.getLog().error("WebHooks 调用错误", e);
dbMonitorNotifyLogService.updateStatus(logId, false, ExceptionUtil.stacktraceToString(e));
}
});
}
use of io.jpom.plugin.IPlugin 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);
}
});
}
Aggregations