From 9d54140617db81b59f118fb9c0d3dfb4d812a181 Mon Sep 17 00:00:00 2001 From: diantu Date: Wed, 8 Feb 2023 18:21:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A8=E6=95=B0=E6=8D=AE=E7=AD=9B=E9=80=89?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=80=A7=E4=BC=98=E5=8C=96,=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=8A=A8=E6=80=81=E6=8F=90=E7=A4=BA=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=95=B0=E6=8D=AE=E5=BA=93=E5=A4=87=E4=BB=BD=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/utils/DatabaseBackupUtils.java | 124 ++++++++++++++++++ .../db-ui/src/views/data/DataPreview.vue | 41 ++++-- 2 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 zyplayer-doc-db/src/main/java/com/zyplayer/doc/db/framework/utils/DatabaseBackupUtils.java diff --git a/zyplayer-doc-db/src/main/java/com/zyplayer/doc/db/framework/utils/DatabaseBackupUtils.java b/zyplayer-doc-db/src/main/java/com/zyplayer/doc/db/framework/utils/DatabaseBackupUtils.java new file mode 100644 index 00000000..4b0d67d3 --- /dev/null +++ b/zyplayer-doc-db/src/main/java/com/zyplayer/doc/db/framework/utils/DatabaseBackupUtils.java @@ -0,0 +1,124 @@ +package com.zyplayer.doc.db.framework.utils; + +import java.io.*; +import java.time.LocalDate; + +/** + * 数据库备份恢复工具类 + * + * @author diantu + * @since 2023年2月8日 + */ +public class DatabaseBackupUtils { + + /** + * MySQL数据库导出 + * + * @param hostIP MySQL数据库所在服务器地址IP + * @param userName 进入数据库所需要的用户名 + * @param password 进入数据库所需要的密码 + * @param savePath 数据库导出文件保存路径 + * @param fileName 数据库导出文件文件名 + * @param databaseName 要导出的数据库名 + * @return 返回true表示导出成功,否则返回false。 + */ + public static boolean exportDatabaseForMysql(String mysqldumpPath, String hostIP, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException { + File saveFile = new File(savePath); + if (!saveFile.exists()) {// 如果目录不存在 + saveFile.mkdirs();// 创建文件夹 + } + if(!savePath.endsWith(File.separator)){ + savePath = savePath + File.separator; + } + + PrintWriter printWriter = null; + BufferedReader bufferedReader = null; + try { + + printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8")); + Process process = Runtime.getRuntime().exec(" "+mysqldumpPath+"/mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " " + databaseName); + InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8"); + bufferedReader = new BufferedReader(inputStreamReader); + String line; + while((line = bufferedReader.readLine())!= null){ + printWriter.println(line); + } + printWriter.flush(); + if(process.waitFor() == 0){//0 表示线程正常终止。 + return true; + } + }catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + if (printWriter != null) { + printWriter.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return false; + } + + public static void backup(String savePath) { + File file = new File(savePath); + if (!file.exists()) { + file.mkdirs(); + } + String fileName = savePath + "/" + LocalDate.now() + ".sql"; + /** 默认使用linux*/ + //String cmdPrefix = "/bin/sh -c "; + String c1 = "/bin/sh"; + String c2 = "-c"; + String os_name = System.getProperty("os.name"); + // 判断是否是windows系统 + if (os_name.toLowerCase().startsWith("win")){ + //cmdPrefix = "cmd /c "; + c1 = "cmd"; + c2 = "/c"; + } + //参考示例:# /usr/local/mysql/bin/mysqldump -uroot -p123456 -P3306 shuju > shuju.sql + String cmd = "mysqldump" // mysqldump的绝对路径,配置环境变量,直接写mysqldump即可 + + " -h" + "127.0.0.1" // 数据库端口号 + + " -P" + "3306" // 数据库端口号 + + " -u" + "root" // 数据库用户名 + + " -p" + "root" // 数据库密码 + + " " + "zyplayer_doc_manage" // 数据库名 + + " > " + fileName; // 最终写入的文件路径 + try { + System.out.println("第一个参数 " + c1); + System.out.println("第二个参数 " + c2); + System.out.println("具体命令 " + cmd); + + //log.error("数据库备份START" + LocalDateTime.now()); + /** + * exec重载方法有一个参数的,window下执行正常,linux下无法完成备份。 + * 使用多参数重载方法都可以正常备份 + */ + Process process = Runtime.getRuntime().exec(new String[]{c1, c2, cmd}); + process.waitFor(); + //log.error("数据库备份END" + LocalDateTime.now()); + } catch (Exception e) { + e.printStackTrace(); + //log.error("数据库备份失败:{}", e.getMessage()); + } + } + + + public static void main(String[] args){ + try { + if (exportDatabaseForMysql("C:/Program Files/MySQL/MySQL Server 5.7/bin","127.0.0.1", "root", "root", "D:/backupDatabase", "2023-2-8.sql", "zyplayer_doc_manage")) { + System.out.println("数据库成功备份!!!"); + } else { + System.out.println("数据库备份失败!!!"); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + //backup("D:/backupDatabase"); + } +} diff --git a/zyplayer-doc-ui/db-ui/src/views/data/DataPreview.vue b/zyplayer-doc-ui/db-ui/src/views/data/DataPreview.vue index b2e7b5ce..fa2b168d 100644 --- a/zyplayer-doc-ui/db-ui/src/views/data/DataPreview.vue +++ b/zyplayer-doc-ui/db-ui/src/views/data/DataPreview.vue @@ -2,17 +2,30 @@
- +
+ + + + +
+ 筛选 + 取消执行 - - 筛选 + + 执行 + 重置 导出 @@ -67,8 +80,8 @@ :default-sort="tableSort"> - +