|
@@ -0,0 +1,158 @@
|
|
|
+package com.qmth.distributed.print.business.backup;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
|
|
|
+import com.qmth.teachcloud.common.config.DictionaryConfig;
|
|
|
+import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: MySQL数据库备份
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: wangliang
|
|
|
+ * @Date: 2022/11/14
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class MySQLDatabaseBackup {
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(MySQLDatabaseBackup.class);
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ DictionaryConfig dictionaryConfig;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Java代码实现MySQL数据库导出
|
|
|
+ *
|
|
|
+ * @param hostIP
|
|
|
+ * @param userName
|
|
|
+ * @param password
|
|
|
+ * @param fileName
|
|
|
+ * @param databaseName
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public boolean exportDatabaseTool(String hostIP, String userName, String password, String fileName, String databaseName) throws Exception {
|
|
|
+// File saveFile = new File(savePath);
|
|
|
+// if (!saveFile.exists()) {// 如果目录不存在
|
|
|
+// saveFile.mkdirs();// 创建文件夹
|
|
|
+// }
|
|
|
+// if (!savePath.endsWith(File.separator)) {
|
|
|
+// savePath = savePath + File.separator;
|
|
|
+// }
|
|
|
+// File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
+ File file = new File("/Users/king/Downloads/file-temp/db/backup" + File.separator + fileName);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ file.createNewFile();
|
|
|
+ }
|
|
|
+
|
|
|
+ PrintWriter printWriter = null;
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
+ try {
|
|
|
+ printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
|
|
|
+ String execSql = "mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=" + StandardCharsets.UTF_8 + " " + databaseName;
|
|
|
+ Process process = Runtime.getRuntime().exec(execSql);
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8);
|
|
|
+ bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+ IOUtils.copy(bufferedReader, printWriter);
|
|
|
+ if (process.waitFor() == 0) {//0 表示线程正常终止。
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ } finally {
|
|
|
+ if (Objects.nonNull(bufferedReader)) {
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(printWriter)) {
|
|
|
+ printWriter.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除数据库数据
|
|
|
+ *
|
|
|
+ * @param hostIP
|
|
|
+ * @param userName
|
|
|
+ * @param password
|
|
|
+ * @param databaseName
|
|
|
+ * @param fileName
|
|
|
+ * @param schoolId
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void deleteDbTool(String hostIP, String userName, String password, String databaseName, String fileName, Long schoolId) throws Exception {
|
|
|
+// InputStream inputStream = MySQLDatabaseBackup.class.getClassLoader().getResourceAsStream(fileName);
|
|
|
+// Optional.ofNullable(inputStream).orElseThrow(() -> ExceptionResultEnum.ERROR.exception(fileName + "未找到"));
|
|
|
+// File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
+// if (!file.exists()) {
|
|
|
+// file.getParentFile().mkdirs();
|
|
|
+// file.createNewFile();
|
|
|
+// }
|
|
|
+// FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
+
|
|
|
+ Optional.ofNullable(schoolId).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("学校id不能为空"));
|
|
|
+ File file = new File("/Users/king/Downloads/file-temp/db/delete" + File.separator + DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_" + schoolId + "_delete.sql");
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ file.createNewFile();
|
|
|
+ }
|
|
|
+ FileUtils.copyInputStreamToFile(new FileInputStream(new File(fileName)), file);
|
|
|
+
|
|
|
+ ByteArrayOutputStream ou = new ByteArrayOutputStream();
|
|
|
+ IOUtils.copy(new FileInputStream(file), ou);
|
|
|
+ String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
|
|
|
+ if (Objects.nonNull(string)) {
|
|
|
+ string = string.replaceAll("\\#\\{schoolId\\}", String.valueOf(schoolId));
|
|
|
+ }
|
|
|
+ IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
+ String cmdarray[] = {"mysql -h" + hostIP + " -u" + userName + " -p" + password + " " + databaseName, "source " + file.getPath()};
|
|
|
+ Runtime runtime = Runtime.getRuntime();
|
|
|
+ OutputStream os = null;
|
|
|
+ OutputStreamWriter writer = null;
|
|
|
+ try {
|
|
|
+ Process process = runtime.exec(cmdarray[0]);//cmd之后执行数组的第一个条件进入数据库
|
|
|
+ //执行了第一条命令以后已经登录到mysql了
|
|
|
+ os = process.getOutputStream();
|
|
|
+ writer = new OutputStreamWriter(os);
|
|
|
+ IOUtils.write(cmdarray[1], writer);
|
|
|
+ } finally {
|
|
|
+ if (Objects.nonNull(writer)) {
|
|
|
+ writer.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(os)) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void main(String[] args) {
|
|
|
+// try {
|
|
|
+// String host = "127.0.0.1", userName = "root", password = "123456789", databaseName = "distributed-v3.2.0";
|
|
|
+// Long schoolId = 290869043907264512L;
|
|
|
+// if (exportDatabaseTool(host, userName, password, DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_backup.sql", databaseName)) {
|
|
|
+// System.out.println("数据库成功备份!!!");
|
|
|
+//// deleteDbTool(host, userName, password, databaseName, SystemConstant.PRINT_DELETE_DATA_FILE_NAME, schoolId);
|
|
|
+// deleteDbTool(host, userName, password, databaseName, "/Users/king/git/distributed-print-service/distributed-print-business/src/main/resources/db/4、delete-data.sql", schoolId);
|
|
|
+// System.out.println("删除数据成功!!!");
|
|
|
+// } else {
|
|
|
+// System.out.println("数据库备份失败!!!");
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+}
|