Răsfoiți Sursa

处理内网Mysql导入

wukai 2 ani în urmă
părinte
comite
96c7bc74b8

+ 68 - 0
sync-in/src/main/java/com/jjt/in/service/impl/InBaseService.java

@@ -0,0 +1,68 @@
+package com.jjt.in.service.impl;
+
+import com.jjt.common.constant.Constants;
+import com.jjt.system.service.ISysConfigService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.io.File;
+
+/**
+ * 数据同步Service业务层处理
+ *
+ * @author wukai
+ * @date 2023-06-06
+ */
+@Service
+public class InBaseService {
+    private static final Logger log = LoggerFactory.getLogger(InBaseService.class);
+    @Resource
+    private ISysConfigService sysConfigService;
+
+    private String getDir(String key) {
+        String dir = sysConfigService.selectConfigByKey(key);
+
+        if (!dir.endsWith(Constants.DIR_END)) {
+            dir += "/";
+        }
+
+        //如果没有目录,则创建目录
+        File file = new File(dir);
+        if (!file.exists()) {
+            boolean b = file.mkdirs();
+            log.info("创建目录", dir, b);
+        }
+        return dir;
+    }
+
+    /**
+     * 获取内网临时目录
+     *
+     * @return 临时目录
+     */
+    public String tmpDIr() {
+        return getDir("in.dir.tmp");
+    }
+
+    /**
+     * 获取内网同步目录
+     *
+     * @return
+     */
+    public String syncDIr(){
+        return getDir("in.dir.sync");
+    }
+
+    /**
+     * 获取内网备份目录
+     *
+     * @return
+     */
+    public String bakDir(){
+        return getDir("in.dir.bak");
+    }
+
+
+}

+ 1 - 1
sync-in/src/main/java/com/jjt/in/service/impl/InEsServiceImpl.java

@@ -28,7 +28,7 @@ import java.io.*;
  * @date 2023-06-06
  * @date 2023-06-06
  */
  */
 @Service
 @Service
-public class InEsServiceImpl implements IInEsService {
+public class InEsServiceImpl extends InBaseService implements IInEsService {
     private static final Logger log = LoggerFactory.getLogger(InEsServiceImpl.class);
     private static final Logger log = LoggerFactory.getLogger(InEsServiceImpl.class);
     @Resource
     @Resource
     private ISysConfigService sysConfigService;
     private ISysConfigService sysConfigService;

+ 82 - 61
sync-in/src/main/java/com/jjt/in/service/impl/InProcessServiceImpl.java

@@ -6,10 +6,7 @@ import com.jjt.common.constant.Constants;
 import com.jjt.common.domain.FileDesc;
 import com.jjt.common.domain.FileDesc;
 import com.jjt.common.domain.IndexDO;
 import com.jjt.common.domain.IndexDO;
 import com.jjt.common.enums.SyncType;
 import com.jjt.common.enums.SyncType;
-import com.jjt.common.utils.CompressZip;
-import com.jjt.common.utils.FtpUtil;
-import com.jjt.common.utils.LinuxCommand;
-import com.jjt.common.utils.StringUtils;
+import com.jjt.common.utils.*;
 import com.jjt.in.service.IInEsService;
 import com.jjt.in.service.IInEsService;
 import com.jjt.in.service.IInProcessService;
 import com.jjt.in.service.IInProcessService;
 import com.jjt.system.service.ISysConfigService;
 import com.jjt.system.service.ISysConfigService;
@@ -39,7 +36,7 @@ import java.util.Comparator;
  * @date 2023-06-06
  * @date 2023-06-06
  */
  */
 @Service
 @Service
-public class InProcessServiceImpl implements IInProcessService {
+public class InProcessServiceImpl extends InBaseService implements IInProcessService {
     private static final Logger log = LoggerFactory.getLogger(InProcessServiceImpl.class);
     private static final Logger log = LoggerFactory.getLogger(InProcessServiceImpl.class);
     @Resource
     @Resource
     private ISysConfigService sysConfigService;
     private ISysConfigService sysConfigService;
@@ -52,7 +49,7 @@ public class InProcessServiceImpl implements IInProcessService {
     @Override
     @Override
     public void downloadFtpFile() {
     public void downloadFtpFile() {
         String fgapInfo = sysConfigService.selectConfigByKey("in.fgap.info");
         String fgapInfo = sysConfigService.selectConfigByKey("in.fgap.info");
-        String syncDir = sysConfigService.selectConfigByKey("in.dir.sync");
+        String syncDir = syncDIr();
         FtpUtil ftpUtil = JSONObject.parseObject(fgapInfo, FtpUtil.class);
         FtpUtil ftpUtil = JSONObject.parseObject(fgapInfo, FtpUtil.class);
         ftpUtil.download(syncDir);
         ftpUtil.download(syncDir);
     }
     }
@@ -71,22 +68,11 @@ public class InProcessServiceImpl implements IInProcessService {
     @Override
     @Override
     public void unzip() {
     public void unzip() {
         //获取内网同步正式目录
         //获取内网同步正式目录
-        String syncDir = sysConfigService.selectConfigByKey("in.dir.sync");
-        String tmpDir = sysConfigService.selectConfigByKey("in.dir.tmp");
-        if (!syncDir.endsWith(Constants.DIR_END)) {
-            syncDir += "/";
-        }
-        if (!tmpDir.endsWith(Constants.DIR_END)) {
-            tmpDir += "/";
-        }
-
+        String syncDir = syncDIr();
+        //获取内网临时目录
+        String tmpDir = tmpDIr();
         //获取内网同步备份目录
         //获取内网同步备份目录
-        String bakDir = sysConfigService.selectConfigByKey("in.dir.bak");
-        if (!bakDir.endsWith(Constants.DIR_END)) {
-            bakDir += "/";
-        }
-        File bak = new File(bakDir);
-        bak.mkdirs();
+        String bakDir = bakDir();
 
 
         try {
         try {
             // 创建File对象
             // 创建File对象
@@ -113,52 +99,21 @@ public class InProcessServiceImpl implements IInProcessService {
                                 String esDir = tmpDir + "es/" + desc.getName().split("\\.")[0].split("-")[2];
                                 String esDir = tmpDir + "es/" + desc.getName().split("\\.")[0].split("-")[2];
                                 esService.parseSyncFile(esDir);
                                 esService.parseSyncFile(esDir);
                                 break;
                                 break;
+                            case mysql:
+                                handleMysqlType(zipFile);
                             case shell:
                             case shell:
-                                String shellDir = desc.getTarget();
-                                if (StringUtils.isBlank(shellDir)) {
-                                    continue;
-                                }
-                                if (!shellDir.endsWith(Constants.DIR_END)) {
-                                    shellDir += "/";
-                                }
-                                File sd = new File(shellDir);
-                                sd.mkdirs();
-                                //复制文件到目标目录
-                                Files.copy(zipFile.toPath(), Paths.get(shellDir + zipFile.getName()));
-                                //给文件赋执行权限
-                                String dataCmd = "chmod +x " + shellDir + zipFile.getName();
-                                LinuxCommand.exec(dataCmd);
+                                handleShellType(desc.getTarget(), zipFile);
                                 break;
                                 break;
                             case php:
                             case php:
-                                String phpDir = desc.getTarget();
-                                if (StringUtils.isBlank(phpDir)) {
-                                    continue;
-                                }
-                                if (!phpDir.endsWith(Constants.DIR_END)) {
-                                    phpDir += "/";
-                                }
-                                File phpFile = new File(phpDir);
-                                phpFile.mkdirs();
-                                //解压文件到目标目录
-                                CompressZip.unzip(zipFile, phpFile);
+                                handlePhpType(desc.getTarget(), zipFile);
+                                break;
                             case mongo:
                             case mongo:
                             case other:
                             case other:
-                                String otherDir = desc.getTarget();
-                                if (StringUtils.isBlank(otherDir)) {
-                                    continue;
-                                }
-                                if (!otherDir.endsWith(Constants.DIR_END)) {
-                                    otherDir += "/";
-                                }
-                                File od = new File(otherDir);
-                                od.mkdirs();
-                                //复制文件到目标目录,如果存在则覆盖
-                                Files.copy(zipFile.toPath(), Paths.get(otherDir + zipFile.getName()), StandardCopyOption.REPLACE_EXISTING);
-                            case mysql:
+                                handleOtherType(desc.getTarget(), zipFile);
+                                break;
                             case crontab:
                             case crontab:
-                                String cronCmd = "crontab " + zipFile.getPath();
-                                log.info("添加crontab", cronCmd);
-                                LinuxCommand.exec(cronCmd);
+                                handleCrontabType(zipFile);
+                                break;
                             default:
                             default:
                                 break;
                                 break;
                         }
                         }
@@ -189,4 +144,70 @@ public class InProcessServiceImpl implements IInProcessService {
             e.printStackTrace();
             e.printStackTrace();
         }
         }
     }
     }
+
+
+    private void handleShellType(String shellDir, File zipFile) throws IOException {
+        if (StringUtils.isBlank(shellDir)) {
+            return;
+        }
+        if (!shellDir.endsWith(Constants.DIR_END)) {
+            shellDir += "/";
+        }
+        File sd = new File(shellDir);
+        sd.mkdirs();
+        // 复制文件到目标目录
+        Files.copy(zipFile.toPath(), Paths.get(shellDir + zipFile.getName()));
+        // 给文件赋执行权限
+        String dataCmd = "chmod +x " + shellDir + zipFile.getName();
+        LinuxCommand.exec(dataCmd);
+    }
+
+    private void handleMysqlType(File zipFile) throws IOException {
+        String params = sysConfigService.selectConfigByKey("in.mysql.info");
+
+        JSONObject jsonObject = JSONObject.parseObject(params);
+
+        String hostname = jsonObject.getString("host");
+        int port = jsonObject.getIntValue("port");
+
+        String user = jsonObject.getString("user");
+        String pass = jsonObject.getString("pass");
+        String db = jsonObject.getString("db");
+        //组装导入命令
+        String cmd = String.format("mysql -u%s -p%s %s < %s", user, pass, db, zipFile.getPath());
+        LinuxCommand.exec(cmd);
+    }
+
+    private void handlePhpType(String phpDir, File zipFile) throws IOException {
+        if (StringUtils.isBlank(phpDir)) {
+            return;
+        }
+        if (!phpDir.endsWith(Constants.DIR_END)) {
+            phpDir += "/";
+        }
+        File phpFile = new File(phpDir);
+        phpFile.mkdirs();
+        // 解压文件到目标目录
+        CompressZip.unzip(zipFile, phpFile);
+    }
+
+    private void handleOtherType(String otherDir, File zipFile) throws IOException {
+        if (StringUtils.isBlank(otherDir)) {
+            return;
+        }
+        if (!otherDir.endsWith(Constants.DIR_END)) {
+            otherDir += "/";
+        }
+        File od = new File(otherDir);
+        od.mkdirs();
+
+        // 复制文件到目标目录,如果存在则覆盖
+        Files.copy(zipFile.toPath(), Paths.get(otherDir + zipFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+    }
+
+    private void handleCrontabType(File zipFile) {
+        String cronCmd = "crontab " + zipFile.getPath();
+        log.info("添加crontab", cronCmd);
+        LinuxCommand.exec(cronCmd);
+    }
 }
 }