add H2BackupService class

This commit is contained in:
Eleven 2021-11-17 14:13:19 +08:00
parent 8c14631aa2
commit cd0116c13a
2 changed files with 77 additions and 11 deletions

View File

@ -0,0 +1,74 @@
package io.jpom.service.h2db;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.ds.DSFactory;
import cn.jiangzeyin.common.DefaultSystemLog;
import org.h2.tools.Shell;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* H2 数据库备份与还原 service
* @author Hotstrip
* @since 2021-11-17
*/
@Service
public class H2BackupService {
/**
* 备份 SQL
* @param url jdbc url
* @param user user
* @param password password
* @param backupSqlPath backup SQL file path, absolute path
* @param tableNameList backup table name list, if need backup all table, use null
*/
public void backupSql(String url, String user, String password,
String backupSqlPath, List<String> tableNameList) throws SQLException {
// 加载数据源
DataSource dataSource = DSFactory.get();
Assert.notNull(dataSource, "Backup sql error...H2 DataSource not null");
Connection connection = dataSource.getConnection();
// 备份 SQL
String sql = StrUtil.format("SCRIPT DROP to '{}'", backupSqlPath);
// 判断是否部分部分表
if (!CollectionUtils.isEmpty(tableNameList)) {
String tableNames = StrUtil.join(",", tableNameList.toArray());
sql = StrUtil.format("{} TABLE {}", sql, tableNames);
}
DefaultSystemLog.getLog().info("backup SQL is: {}", sql);
// 执行 SQL 备份脚本
Shell shell = new Shell();
/**
* url 表示 h2 数据库的 jdbc url
* user 表示登录的用户名
* password 表示登录密码
* driver jdbc 驱动
* sql 是备份的 sql 语句
* - 案例script drop to ${fileName1} table ${tableName1},${tableName2}...
* - script drop to 表示备份数据库drop 表示建表之前会先删除表
* - ${fileName1} 表示备份之后的文件名
* - table 表示需要备份的表名称后面跟多个表名用英文逗号分割
*/
String[] params = new String[] {
"-url", url,
"-user", user,
"-password", password,
"-driver", "org.h2.Driver",
"-sql", sql
};
shell.runTool(connection, params);
}
// 还原备份 SQL
}

View File

@ -5,27 +5,19 @@ import cn.hutool.core.lang.Console;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.sql.SqlLog;
import cn.hutool.setting.Setting;
import cn.jiangzeyin.common.spring.SpringUtil;
import io.jpom.ApplicationStartTest;
import io.jpom.JpomApplication;
import io.jpom.JpomServerApplication;
import io.jpom.common.JpomManifest;
import io.jpom.system.ExtConfigBean;
import io.jpom.system.ServerExtConfigBean;
import io.jpom.system.db.DbConfig;
import io.jpom.system.init.InitDb;
import org.h2.command.dml.RunScriptCommand;
import org.h2.engine.Database;
import org.h2.tools.Restore;
import org.h2.tools.RunScript;
import org.h2.tools.Script;
import org.h2.tools.Shell;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.SQLException;