Explorar el Código

文件备份实现

wanggaokun hace 1 año
padre
commit
4fb01b44ce

+ 8 - 6
PHM-admin/phm-admin/src/main/resources/application-dev.yml

@@ -1,21 +1,23 @@
 easy-es:
-  address: 127.0.0.1:9200
+  address: 110.41.34.83:9200
   banner: false # 默认为true 打印banner 若您不期望打印banner,可配置为false
-  username: #es用户名,若无则删去此行配置
-  password: #es密码,若无则删去此行配置
+  username: elastic #es用户名,若无则删去此行配置
+  password: kO2cWeeUH36qqIjf1dm7 #es密码,若无则删去此行配置
 # 数据源配置
 spring:
   redis:
-    host: 124.71.193.202
+    host: 110.41.34.83
+    password: Z;G4AS:Vor'YF#p?
+    username:
   datasource:
     type: com.alibaba.druid.pool.DruidDataSource
     driverClassName: dm.jdbc.driver.DmDriver
     druid:
       # 主库数据源
       master:
-        url: jdbc:dm://124.71.193.202:5236?schema=PHM&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+        url: jdbc:dm://110.41.34.83:5236?schema=PHM&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
         username: SYSDBA
-        password: SYSDBA
+        password: QEkqT1dLOKig6hHu
       # 从库数据源
       slave:
         # 从数据源开关/默认关闭

+ 1 - 1
PHM-admin/phm-admin/src/main/resources/application-local.yml

@@ -13,7 +13,7 @@ spring:
     druid:
       # 主库数据源
       master:
-        url: jdbc:dm://124.71.193.202:5236?schema=PHM&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+        url: jdbc:dm://110.41.34.83:5236?schema=PHM&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
         username: SYSDBA
         password: SYSDBA
       # 从库数据源

+ 3 - 2
PHM-admin/phm-admin/src/main/resources/application-pro.yml

@@ -6,14 +6,15 @@ easy-es:
 # 数据源配置
 spring:
   redis:
-    host: 124.71.193.202
+    host: 110.41.34.83
+    password: Z;G4AS:Vor'YF#p?
   datasource:
     type: com.alibaba.druid.pool.DruidDataSource
     driverClassName: dm.jdbc.driver.DmDriver
     druid:
       # 主库数据源
       master:
-        url: jdbc:dm://124.71.193.202:5236?schema=PHM&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+        url: jdbc:dm://110.41.34.83:5236?schema=PHM&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
         username: SYSDBA
         password: SYSDBA
       # 从库数据源

+ 5 - 1
PHM-admin/phm-admin/src/main/resources/application.yml

@@ -12,6 +12,10 @@ phm:
   profile: D:/phm/uploadPath
   # 文件路径 示例( Windows配置D:/phm/dataPath/csv,Linux配置 /home/phm/dataPath/csv)
   csvFilePath: D:/phm/dataPath/csv
+  #  对应备份目录
+  profileBackup: D:/phm_backup/uploadPath
+  #  对应备份目录
+  csvFileBackupPath: D:/phm_backup/dataPath/csv
   # 获取ip地址开关
   addressEnabled: false
   # 验证码类型 math 数字计算 char 字符验证
@@ -72,7 +76,7 @@ spring:
   # redis 配置
   redis:
     # 地址
-#    host: localhost
+    #    host: localhost
     # 端口,默认为6379
     port: 6379
     # 数据库索引

+ 24 - 0
PHM-admin/phm-common/src/main/java/com/phm/common/config/PHMConfig.java

@@ -1,5 +1,6 @@
 package com.phm.common.config;
 
+import lombok.Getter;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
@@ -41,6 +42,16 @@ public class PHMConfig {
      */
     private static String csvFilePath;
 
+    /**
+     * 上传路径 备份目录
+     */
+    private static String profileBackup;
+
+    /**
+     * csv数据路径 备份目录
+     */
+    private static String csvFileBackupPath;
+
     /**
      * 获取地址开关
      */
@@ -94,11 +105,24 @@ public class PHMConfig {
     public static String getCsvFilePath() {
         return csvFilePath;
     }
+    public static String getProfileBackup() {
+        return profileBackup;
+    }
+    public static String getCsvFileBackupPath() {
+        return csvFileBackupPath;
+    }
 
     public void setCsvFilePath(String csvFilePath) {
         PHMConfig.csvFilePath = csvFilePath;
     }
 
+    public void setProfileBackup(String profileBackup) {
+        PHMConfig.profileBackup = profileBackup;
+    }
+
+    public void setCsvFileBackupPath(String csvFileBackupPath) {
+        PHMConfig.csvFileBackupPath = csvFileBackupPath;
+    }
 
     public static boolean isAddressEnabled() {
         return addressEnabled;

+ 14 - 3
PHM-admin/phm-manage/src/main/java/com/phm/manage/controller/BackupRestoreController.java

@@ -1,11 +1,15 @@
 package com.phm.manage.controller;
 
-import com.phm.common.core.controller.BaseController;
-import com.phm.manage.domain.common.CommonResult;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import com.phm.common.config.PHMConfig;
+import com.phm.common.core.controller.BaseController;
+import com.phm.manage.domain.common.CommonResult;
+import com.phm.manage.service.IBackupRestoreService;
+
 /**
  * @Description BackupRestoreController
  * @Author WGK
@@ -14,6 +18,9 @@ import org.springframework.web.bind.annotation.RestController;
 @RestController
 @RequestMapping("/manage")
 public class BackupRestoreController extends BaseController {
+    @Autowired
+    private IBackupRestoreService backupRestoreService;
+
     /**
      * 备份文件
      *
@@ -21,7 +28,8 @@ public class BackupRestoreController extends BaseController {
      */
     @PostMapping("/file/backup")
     public CommonResult<String> backupFile() {
-        // TODO 服务都需要开发
+        backupRestoreService.fileCopyBackup(PHMConfig.getProfile(), PHMConfig.getProfileBackup());
+        backupRestoreService.fileCopyBackup(PHMConfig.getCsvFilePath(), PHMConfig.getCsvFileBackupPath());
         return CommonResult.buildSuccess();
     }
 
@@ -32,6 +40,8 @@ public class BackupRestoreController extends BaseController {
      */
     @PostMapping("/file/restore")
     public CommonResult<String> restoreFile() {
+        backupRestoreService.fileCopyRestore(PHMConfig.getProfileBackup(), PHMConfig.getProfile());
+        backupRestoreService.fileCopyRestore(PHMConfig.getCsvFileBackupPath(), PHMConfig.getCsvFilePath());
         return CommonResult.buildSuccess();
     }
 
@@ -44,6 +54,7 @@ public class BackupRestoreController extends BaseController {
     public CommonResult<String> backupDb() {
         return CommonResult.buildSuccess();
     }
+
     /**
      * 恢复数据库表数据
      *

+ 13 - 0
PHM-admin/phm-manage/src/main/java/com/phm/manage/service/IBackupRestoreService.java

@@ -0,0 +1,13 @@
+package com.phm.manage.service;
+
+import java.io.IOException;
+
+/**
+ * @Description IBackupRestoreService
+ * @Author WGK
+ * @Date 2023/11/30 14:02
+ */
+public interface IBackupRestoreService {
+    boolean fileCopyBackup(String sourceFolderPath, String targetFolderPath);
+    boolean fileCopyRestore(String sourceFolderPath, String targetFolderPath);
+}

+ 58 - 0
PHM-admin/phm-manage/src/main/java/com/phm/manage/service/impl/BackupRestoreService.java

@@ -0,0 +1,58 @@
+package com.phm.manage.service.impl;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+
+import org.springframework.stereotype.Service;
+
+import com.phm.manage.service.IBackupRestoreService;
+
+import cn.hutool.core.io.FileUtil;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * @Description BackupRestoreService
+ * @Author WGK
+ * @Date 2023/11/30 14:02
+ */
+@Service
+@Slf4j
+public class BackupRestoreService implements IBackupRestoreService {
+    @Override
+    public boolean fileCopyBackup(String sourceFolderPath, String targetFolderPath) {
+        copyFile(sourceFolderPath, targetFolderPath, false);
+        return true;
+    }
+
+    private static void copyFile(String sourceFolderPath, String targetFolderPath, boolean isAllCopy) {
+        // 获取源文件夹和目标文件夹中的文件列表
+        List<File> sourceFiles = FileUtil.loopFiles(sourceFolderPath);
+        List<File> targetFiles = FileUtil.loopFiles(targetFolderPath);
+
+        // 执行增量复制操作
+        for (File sourceFile : sourceFiles) {
+            // 源文件相对于源文件夹的相对路径
+            String relativePath = FileUtil.subPath(sourceFolderPath, sourceFile.getAbsolutePath());
+
+            // 目标文件路径
+            Path targetPath = Paths.get(targetFolderPath, relativePath);
+
+            // 目标文件
+            File targetFile = targetPath.toFile();
+            // 如果源文件是新添加或者修改过的,则进行复制
+            if (isAllCopy || !targetFiles.contains(targetFile)
+                || sourceFile.lastModified() > targetFile.lastModified()) {
+                FileUtil.copy(sourceFile, targetFile, true);
+            }
+        }
+    }
+
+    @Override
+    public boolean fileCopyRestore(String sourceFolderPath, String targetFolderPath) {
+        // 执行复制操作
+        copyFile(sourceFolderPath, targetFolderPath, true);
+        return true;
+    }
+}