|
@@ -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", "");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|