瀏覽代碼

数据库备份任务

wukai 1 年之前
父節點
當前提交
a9111f7c21

+ 15 - 0
doc-biz/src/main/java/com/doc/biz/service/IBackupService.java

@@ -0,0 +1,15 @@
+package com.doc.biz.service;
+
+/**
+ * 备份Service接口
+ *
+ * @author wukai
+ * @date 2023-11-14
+ */
+public interface IBackupService {
+    /**
+     * 数据库备份
+     */
+    void backup();
+
+}

+ 89 - 0
doc-biz/src/main/java/com/doc/biz/service/impl/BackupServiceImpl.java

@@ -0,0 +1,89 @@
+package com.doc.biz.service.impl;
+
+import com.doc.biz.service.IBackupService;
+import com.doc.common.config.RuoYiConfig;
+import com.doc.common.utils.DateUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+/**
+ * 备份Service接口
+ *
+ * @author wukai
+ * @date 2023-11-14
+ */
+@Service
+public class BackupServiceImpl implements IBackupService {
+    @Value("${spring.datasource.driver-class-name}")
+    private String driverClassName;
+    @Value("${spring.datasource.druid.master.url}")
+    private String url;
+    @Value("${spring.datasource.druid.master.username}")
+    private String user;
+    @Value("${spring.datasource.druid.master.password}")
+    private String pwd;
+
+    public static void main(String[] args) {
+        String url = "jdbc:mysql://192.168.188.88:3306/doc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
+        String[] pp = url.split("//")[1].split("/");
+        String dbName = pp[1].split("\\?")[0];
+        String host = pp[0].split(":")[0];
+        String port = pp[0].split(":")[1];
+        System.err.println(dbName);
+        System.err.println(host);
+        System.err.println(port);
+    }
+
+    /**
+     * 数据库备份
+     */
+    @Override
+    public void backup() {
+        String basePath = RuoYiConfig.getProfile() + "/db_back/";
+        try {
+            //创建目录及父目录
+            Files.createDirectories(Paths.get(basePath));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        String filePath = basePath + DateUtils.dateTimeNow() + ".sql";
+
+        String[] pp = url.split("//")[1].split("/");
+        String dbName = pp[1].split("\\?")[0];
+        String host = pp[0].split(":")[0];
+        String port = pp[0].split(":")[1];
+        String cmd = " /usr/local/mysql/bin/mysqldump -h" + host + " -P" + port + " -u" + user + " -p" + pwd + " --default-character-set=utf8 " + dbName + " -r " + filePath;
+        try {
+            Process exec = Runtime.getRuntime().exec(cmd);
+            if (exec.waitFor() == 0) {
+                System.out.println("数据库备份成功");
+            } else {
+                System.out.println("process.waitFor()=" + exec.waitFor());
+            }
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
+    /**
+     * 获取数据库名
+     */
+    private String getDataBaseName() {
+        return url.substring(url.indexOf("3306"), url.indexOf("?")).replaceAll("/", "").replaceAll("3306", "");
+    }
+
+    /**
+     * 获取主机地址
+     */
+    private String getHost() {
+        return url.substring(url.indexOf("mysql"), url.indexOf("3306")).replace(":", "").replace("//", "").replace("mysql", "");
+    }
+
+}

+ 21 - 0
doc-biz/src/main/java/com/doc/task/BackupTask.java

@@ -0,0 +1,21 @@
+package com.doc.task;
+
+import com.doc.biz.service.IBackupService;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * 定时任务调度测试
+ *
+ * @author ruoyi
+ */
+@Component("backupTask")
+public class BackupTask {
+    @Resource
+    private IBackupService backupService;
+
+    public void backup() {
+        backupService.backup();
+    }
+}

+ 26 - 0
sql/view.sql

@@ -0,0 +1,26 @@
+-- 创建视图
+CREATE VIEW V_DOC_DIR AS
+SELECT DIR_ID,
+       SPACE_ID,
+       DIR_TYPE,
+       DIR_ROLE,
+       DIR_NAME,
+       PARENT_ID,
+       IS_ENCRYPT,
+       ENCRYPT_LEVEL,
+       CREATE_BY,
+       CREATE_TIME,
+       UPDATE_BY,
+       UPDATE_TIME,
+       REMARK,
+       (WITH RECURSIVE VDIR AS (SELECT DIR_ID, DIR_NAME, PARENT_ID, DIR_NAME AS DPATH
+                                FROM DOC_DIR
+                                WHERE DIR_ID = A.DIR_ID
+                                UNION ALL
+                                SELECT C.DIR_ID, C.DIR_NAME, C.PARENT_ID, CONCAT(C.DIR_NAME, '/', P.DPATH) AS DPATH
+                                FROM DOC_DIR C
+                                         JOIN VDIR P ON C.DIR_ID = P.PARENT_ID)
+        SELECT DPATH
+        FROM VDIR
+        WHERE PARENT_ID = 0) AS DIR_PATH
+FROM DOC_DIR A