From 77d45c352855f6f834b3aea4d0394b19d0cd288a Mon Sep 17 00:00:00 2001 From: VIVIMAN Date: Tue, 26 Apr 2022 15:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E7=8A=B6=E6=80=81=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/insigma/entry/TabColObj.java | 12 +- .../java/com/insigma/service/Computer.java | 8 +- .../java/com/insigma/service/Database.java | 20 +- .../java/com/insigma/service/Middleware.java | 22 +- .../service/impl/LinuxTongWebKingBase.java | 33 +++ .../service/impl/WindowsTomcatMysql.java | 208 ++++++++++++++---- src/main/java/com/insigma/utils/FileUtil.java | 20 +- .../impl/LinuxTongWebKingBaseTest.java | 12 + .../service/impl/WindowsTomcatMysqlTest.java | 36 +++ 9 files changed, 323 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/insigma/entry/TabColObj.java b/src/main/java/com/insigma/entry/TabColObj.java index d672c9d..b38ed69 100644 --- a/src/main/java/com/insigma/entry/TabColObj.java +++ b/src/main/java/com/insigma/entry/TabColObj.java @@ -21,7 +21,17 @@ public class TabColObj { private String col; public static List getSQL(){ - return new ArrayList<>(); + return Arrays.asList( + "", + "", + "", + "", + "", + "", + "", + "", + "" + ); } public static List getData(){ return Arrays.asList( diff --git a/src/main/java/com/insigma/service/Computer.java b/src/main/java/com/insigma/service/Computer.java index 771003e..e872f55 100644 --- a/src/main/java/com/insigma/service/Computer.java +++ b/src/main/java/com/insigma/service/Computer.java @@ -18,5 +18,11 @@ public abstract class Computer { * 执行 备份操作 * @return 是否备份成功 */ - public abstract boolean runBak(); + public abstract boolean runBak() throws Exception; + /*** + * 执行 还原操作 + * @param path 还原文件路径 + * @return 是否备份成功 + */ + public abstract boolean runRestore(String path); } diff --git a/src/main/java/com/insigma/service/Database.java b/src/main/java/com/insigma/service/Database.java index 658d1c8..a582e39 100644 --- a/src/main/java/com/insigma/service/Database.java +++ b/src/main/java/com/insigma/service/Database.java @@ -30,7 +30,7 @@ public interface Database { * @return */ default boolean setDbSize(int ...size) throws Exception { - System.out.printf("设置出参数:%d,但是未检测到实现应用调用此方法!", size); + System.out.println(String.format("设置出参数:%d,但是未检测到实现应用调用此方法!", size)); return false; } /*** @@ -52,4 +52,22 @@ public interface Database { System.out.println("进行数据库注册服务,但是未检测到实现应用调用此方法!"); return false; } + + /** + * 实现数据库服务启动 + * @return + */ + default boolean startDbService() throws Exception { + System.out.println("进行数据库服务启动,但是未检测到实现应用调用此方法!"); + return false; + } + + /** + * 实现数据库服务停止 + * @return + */ + default boolean stopDbService() throws Exception { + System.out.println("进行数据库服务停止,但是未检测到实现应用调用此方法!"); + return false; + } } diff --git a/src/main/java/com/insigma/service/Middleware.java b/src/main/java/com/insigma/service/Middleware.java index 5c5a265..0415b76 100644 --- a/src/main/java/com/insigma/service/Middleware.java +++ b/src/main/java/com/insigma/service/Middleware.java @@ -13,7 +13,7 @@ public interface Middleware { * @return */ default boolean cleanMwCache() throws Exception { - System.out.printf("清除缓存,但是未检测到实现应用调用此方法!"); + System.out.println("清除缓存,但是未检测到实现应用调用此方法!"); return false; } /*** @@ -22,7 +22,7 @@ public interface Middleware { * @return */ default boolean setMwSize(int ...size) throws Exception { - System.out.printf("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", size); + System.out.println(String.format("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", size)); return false; } @@ -34,4 +34,22 @@ public interface Middleware { System.out.println("进行中间件注册服务,但是未检测到实现应用调用此方法!"); return false; } + + /** + * 实现中间件服务启动 + * @return + */ + default boolean startMwService() throws Exception { + System.out.println("进行中间件服务启动,但是未检测到实现应用调用此方法!"); + return false; + } + + /** + * 实现中间件服务停止 + * @return + */ + default boolean stopMwService() throws Exception { + System.out.println("进行中间件服务停止,但是未检测到实现应用调用此方法!"); + return false; + } } diff --git a/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java b/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java index 083e067..c41ac9d 100644 --- a/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java +++ b/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java @@ -40,6 +40,11 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa return false; } + @Override + public boolean runRestore(String path) { + return false; + } + @Override public boolean rebuildIndex(List index) { boolean retBool; @@ -184,6 +189,34 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa return true; } + @Override + public boolean startDbService() { + log.info("启动 数据库 服务..."); + runShell("systemctl start " + AppCfg.DB); + return true; + } + + @Override + public boolean stopDbService() { + log.info("停止 数据库 服务..."); + runShell("systemctl stop " + AppCfg.DB); + return true; + } + + @Override + public boolean startMwService() { + log.info("启动 中间件 服务..."); + runShell("systemctl start " + AppCfg.MW); + return true; + } + + @Override + public boolean stopMwService() { + log.info("停止 中间件 服务..."); + runShell("systemctl stop " + AppCfg.MW); + return true; + } + @Override public boolean registrationMwService() { // TODO 无法实现 diff --git a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java index ad6ead5..0f72e57 100644 --- a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java +++ b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java @@ -9,7 +9,10 @@ import com.insigma.service.*; import com.insigma.utils.*; import lombok.extern.slf4j.Slf4j; +import java.io.File; import java.sql.SQLException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.List; @@ -34,35 +37,101 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware } @Override - public boolean runBak() { - return false; + public boolean runBak() throws Exception { + boolean retBool; + log.info("创建备份文件目录..."); + String format = DateTimeFormatter.BASIC_ISO_DATE.format(LocalDate.now()); + boolean b = FileUtil.mkDirectory(AppCfg.HZB + File.separatorChar + format); + if(b){ + log.info("创建备份目录:{}", format); + WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE); + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING) { + log.info("备份照片文件..."); + copyFileByA57(format); + + log.info("停止 数据库 服务..."); + ws.stopService(); + + log.info("备份压缩数据库文件..."); + ZipUtil.zip7Z( + AppCfg.HZB + File.separatorChar + "mysql" + File.separatorChar + "data", + AppCfg.HZB + File.separatorChar + format + File.separatorChar + "data.gz"); + + log.info("备份压缩备份文件..."); + ZipUtil.zip7Z( + AppCfg.HZB + File.separatorChar + "format", + AppCfg.HZB + File.separatorChar + format + ".gz"); + + log.info("启动 数据库 服务..."); + ws.startService(); + } + retBool = true; + } else { + retBool = false; + } + return retBool; } - @Override - public boolean rebuildIndex(List index) { - boolean retBool; + private void copyFileByA57(String format) { log.info("创建链接..."); final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); - String sql; try { - for (IndexObj idx : index) { - try { - sql = String.format("DROP INDEX %s ON %s", idx.getIdx(), idx.getTab()); - log.debug(sql); - session.execute(sql); - }finally { - sql = String.format("ALTER TABLE %s ADD INDEX %s (%s) USING BTREE", idx.getTab(), idx.getIdx(), idx.getCol()); - log.debug(sql); - session.execute(sql); - } + String sql = "select concat('tomcat8/HZBPHOTOS/', PHOTOPATH, '/', PHOTONAME) fileName from a57 where exists (select 1 from a01 where a01.a0000=a57.a0000 and status=1)"; + List query = session.query(sql, String.class); + String from, to; + for (int i = 0, j = query.size(); i < j; i++) { + from = AppCfg.HZB + query.get(i); + to = AppCfg.HZB + format + query.get(i); + cn.hutool.core.io.FileUtil.copy(from, to, false); } - retBool = true; } catch (SQLException e) { - log.error("执行更新索引发生异常:{}", e.getMessage()); - session.close(); + log.error("执行查询报错:{}",e.getMessage()); + e.printStackTrace(); + } + } + + @Override + public boolean runRestore(String path) { + boolean retBool; + boolean b = cn.hutool.core.io.FileUtil.isFile(path); + if(b){ + log.info("判断文件:{}...存在:{}", path, b); + retBool = true; + }else{ + log.error("判断文件:{}...不存在,请检查!", path); retBool = false; } - session.close(); + return retBool; + } + + @Override + public boolean rebuildIndex(List index) { + boolean retBool = false; + WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE); + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ + log.info("创建链接..."); + final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); + String sql; + try { + for (IndexObj idx : index) { + try { + sql = String.format("DROP INDEX %s ON %s", idx.getIdx(), idx.getTab()); + log.debug(sql); + session.execute(sql); + }finally { + sql = String.format("ALTER TABLE %s ADD INDEX %s (%s) USING BTREE", idx.getTab(), idx.getIdx(), idx.getCol()); + log.debug(sql); + session.execute(sql); + } + } + retBool = true; + } catch (SQLException e) { + log.error("执行更新索引发生异常:{}", e.getMessage()); + session.close(); + retBool = false; + } + session.close(); + } return retBool; } @@ -96,34 +165,37 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware @Override public boolean cleanDbCache(List sqls, List tabColObjs) { - boolean retBool; - log.info("创建链接..."); - final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); - try { - log.info("执行 预置 SQL 脚本..."); - for (String sql : sqls) { - log.debug(sql); - session.execute(sql); - } - log.info("执行 清洗数据中特殊字符串 脚本..."); - String sql; - for (String lj : DbUtil.RUBBISH) { - for (TabColObj tab : tabColObjs) { - sql = String.format("update %s set %s=replace(%s, '%s', '') where %s is not null", - tab.getTab(), tab.getCol(), tab.getCol(), lj, tab.getCol()); + boolean retBool = false; + WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE); + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING) { + log.info("创建链接..."); + final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); + try { + log.info("执行 预置 SQL 脚本..."); + for (String sql : sqls) { log.debug(sql); session.execute(sql); } + log.info("执行 清洗数据中特殊字符串 脚本..."); + String sql; + for (String lj : DbUtil.RUBBISH) { + for (TabColObj tab : tabColObjs) { + sql = String.format("update %s set %s=replace(%s, '%s', '') where %s is not null", + tab.getTab(), tab.getCol(), tab.getCol(), lj, tab.getCol()); + log.debug(sql); + session.execute(sql); + } + } + retBool = true; + } catch (SQLException e) { + log.error("执行 清洗数据中特殊字符串 发生异常:{}", e.getMessage()); + e.printStackTrace(); + session.close(); + retBool = false; } - retBool = true; - } catch (SQLException e) { - log.error("执行 清洗数据中特殊字符串 发生异常:{}", e.getMessage()); - e.printStackTrace(); + log.info("执行 清洗数据中特殊字符串 完成..."); session.close(); - retBool = false; } - log.info("执行 清洗数据中特殊字符串 完成..."); - session.close(); return retBool; } @@ -243,6 +315,58 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware return true; } + @Override + public boolean startDbService() throws Exception { + log.info("启动 数据库 服务..."); + WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE); + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.STOPPED){ + ws.startService(); + return true; + } + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ + ws.restartService(); + return true; + } + return false; + } + + @Override + public boolean stopDbService() throws Exception { + log.info("停止 数据库 服务..."); + WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE); + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ + ws.stopService(); + return true; + } + return false; + } + + @Override + public boolean startMwService() throws Exception { + log.info("启动 中间件 服务..."); + WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE); + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.STOPPED){ + ws.startService(); + return true; + } + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ + ws.restartService(); + return true; + } + return false; + } + + @Override + public boolean stopMwService() throws Exception { + log.info("停止 中间件 服务..."); + WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE); + if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ + ws.stopService(); + return true; + } + return false; + } + public void openServer(){ runShell("SERVICES.MSC"); log.info("打开本地服务完成!"); diff --git a/src/main/java/com/insigma/utils/FileUtil.java b/src/main/java/com/insigma/utils/FileUtil.java index a1356d2..2dbb094 100644 --- a/src/main/java/com/insigma/utils/FileUtil.java +++ b/src/main/java/com/insigma/utils/FileUtil.java @@ -40,7 +40,25 @@ public class FileUtil { } } - + /** + * 按照路径创建目录 + * @param path 路径名称 + * @return + */ + public static boolean mkDirectory(String path) { + boolean bool; + try { + File file = new File(path); + if (!file.exists()) { + bool = file.mkdirs(); + } else { + bool = false; + } + } catch (Exception e) { + bool = false; + } + return bool; + } /** * 删除整个文件夹里的内容 diff --git a/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java b/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java index 2362242..cf83583 100644 --- a/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java +++ b/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java @@ -74,4 +74,16 @@ public class LinuxTongWebKingBaseTest { assertTrue(linuxTongWebKingBase.runShell("vim a.txt")); log.info("测试完成!"); } + + @Test + public void runBak() { + assertTrue(linuxTongWebKingBase.runBak()); + log.info("测试完成!"); + } + + @Test + public void runRestore() { + assertTrue(linuxTongWebKingBase.runRestore("")); + log.info("测试完成!"); + } } \ No newline at end of file diff --git a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java index e8c5d94..54e78dc 100644 --- a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java +++ b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java @@ -74,6 +74,42 @@ public class WindowsTomcatMysqlTest { log.info("测试完成!"); } + @Test + public void runBak() throws Exception { + assertTrue(windowsTomcatMysql.runBak()); + log.info("测试完成!"); + } + + @Test + public void runRestore() { + assertTrue(windowsTomcatMysql.runRestore("")); + log.info("测试完成!"); + } + + @Test + public void startDbService() throws Exception { + assertTrue(windowsTomcatMysql.startDbService()); + log.info("测试完成!"); + } + + @Test + public void stopDbService() throws Exception { + assertTrue(windowsTomcatMysql.stopDbService()); + log.info("测试完成!"); + } + + @Test + public void startMwService() throws Exception { + assertTrue(windowsTomcatMysql.startMwService()); + log.info("测试完成!"); + } + + @Test + public void stopMwService() throws Exception { + assertTrue(windowsTomcatMysql.stopMwService()); + log.info("测试完成!"); + } + @Test public void openServer() { windowsTomcatMysql.openServer();