Procházet zdrojové kódy

完成PHP shell crontab解析

wukai před 2 roky
rodič
revize
b38751b50d

+ 4 - 0
sync-common/src/main/java/com/jjt/common/domain/FileDesc.java

@@ -32,4 +32,8 @@ public class FileDesc {
      * 目标服务器
      */
     private List<String> server;
+    /**
+     * 目标目录
+     */
+    private String target;
 }

+ 60 - 3
sync-in/src/main/java/com/jjt/in/service/impl/InProcessServiceImpl.java

@@ -9,6 +9,7 @@ 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.in.service.IInEsService;
 import com.jjt.in.service.IInProcessService;
 import com.jjt.system.service.ISysConfigService;
@@ -24,7 +25,10 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.CopyOption;
 import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.util.Arrays;
 import java.util.Comparator;
 
@@ -75,6 +79,15 @@ public class InProcessServiceImpl implements IInProcessService {
         if (!tmpDir.endsWith(Constants.DIR_END)) {
             tmpDir += "/";
         }
+
+        //获取内网同步备份目录
+        String bakDir = sysConfigService.selectConfigByKey("in.dir.bak");
+        if (!bakDir.endsWith(Constants.DIR_END)) {
+            bakDir += "/";
+        }
+        File bak = new File(bakDir);
+        bak.mkdirs();
+
         try {
             // 创建File对象
             File dirFile = new File(syncDir);
@@ -98,16 +111,60 @@ public class InProcessServiceImpl implements IInProcessService {
                                 File targetDir = new File(tmpDir + "es/");
                                 CompressZip.unzip(zipFile, targetDir);
                                 String esDir = tmpDir + "es/" + desc.getName().split("\\.")[0].split("-")[2];
-
                                 esService.parseSyncFile(esDir);
                                 break;
-                            case php:
+                            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);
                                 break;
+                            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);
                             case mongo:
-                                break;
+                            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:
+                            case crontab:
+                                String cronCmd = "crontab " + zipFile.getPath();
+                                log.info("添加crontab", cronCmd);
+                                LinuxCommand.exec(cronCmd);
                             default:
                                 break;
                         }
+                        //移动文件到备份目录
+                        Files.move(file.toPath(), Paths.get(bakDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                        Files.move(zipFile.toPath(), Paths.get(bakDir + zipFile.getName()), StandardCopyOption.REPLACE_EXISTING);
                     } else {
                         System.err.println("这个是错误滴" + desc.toString());
                     }

+ 2 - 2
sync-in/src/main/java/com/jjt/in/task/EsInTask.java → sync-in/src/main/java/com/jjt/in/task/InEsTask.java

@@ -10,8 +10,8 @@ import javax.annotation.Resource;
  *
  * @author ruoyi
  */
-@Component("esInTask")
-public class EsInTask {
+@Component("inEsTask")
+public class InEsTask {
     @Resource
     private IInEsService inEsService;
 

+ 27 - 0
sync-in/src/main/java/com/jjt/in/task/InProcessTask.java

@@ -0,0 +1,27 @@
+package com.jjt.in.task;
+
+import com.jjt.in.service.IInEsService;
+import com.jjt.in.service.IInProcessService;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * Es定时任务
+ *
+ * @author ruoyi
+ */
+@Component("inProcessTask")
+public class InProcessTask {
+    @Resource
+    private IInProcessService processService;
+
+    public void sync() {
+        processService.unzip();
+    }
+
+    public void esImport() {
+        System.out.println("执行有参方法:");
+    }
+
+}

+ 13 - 0
sync-out/src/test/java/com/test/Test.java

@@ -0,0 +1,13 @@
+package com.test;
+
+import java.io.File;
+
+public class Test {
+    public static void main(String[] args) {
+        String x = "D:\\data\\sync\\in\\bak\\test.sh";
+        File xf = new File(x);
+        System.err.println(xf.getAbsolutePath());
+        System.err.println(xf.getPath());
+        xf.mkdirs();
+    }
+}