try {
DataBaseMysqlAutoConfig config = SpringUtils.getBean(DataBaseMysqlAutoConfig.class);
StringBuilder strStmt = new StringBuilder();
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
// windows 创建数据库 麒麟不用创建数据库
if (isWindows) { // 创建数据库
strStmt.append("mysqladmin");
strStmt.append(" -h ").append(config.dbProperties().getHost());
strStmt.append(" -u ").append(config.dbProperties().getUsername());
strStmt.append(" -p").append(config.dbProperties().getPassword());
strStmt.append(" -P").append(config.dbProperties().getPort());
strStmt.append(" create ").append(config.dbProperties().getDatabase());
log.info("restoreMySQL mysqladmin strStmt=={}", strStmt.toString());
Runtime.getRuntime().exec(strStmt.toString());
}
// 恢复数据
strStmt.setLength(0);
strStmt.append(isWindows ? "mysql" : "/opt/xxx/xxx-mysql/bin/mysql");
strStmt.append(" -h").append(config.dbProperties().getHost());
strStmt.append(" -u").append(config.dbProperties().getUsername());
strStmt.append(" -p").append(config.dbProperties().getPassword());
strStmt.append(" -P").append(config.dbProperties().getPort());
strStmt.append(" ").append(config.dbProperties().getDatabase());
strStmt.append(" < \"").append(filePath).append("\"");
log.info("restoreMySQL = {}", strStmt.toString());
Runtime runtime = Runtime.getRuntime();
String[] cmd;
if (isWindows) {
cmd = new String[]{"cmd", "/c", strStmt.toString()};
} else {
cmd = new String[]{"/bin/sh", "-c", strStmt.toString()};
}
runtime.exec(cmd);
System.out.println("数据已从 " + filePath + " 导入到数据库中");
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
sql还原数据库文件
最新推荐文章于 2024-08-15 01:26:58 发布