Browse Source

优化解决SQL数据备份功能

develop
zhaoxianglong 1 year ago
parent
commit
67c5c38289
  1. 9
      zc-business/src/main/java/com/zc/business/controller/DcMYSQLJob.java
  2. 8
      zc-business/src/main/java/com/zc/business/controller/DcMYSQLUtil.java

9
zc-business/src/main/java/com/zc/business/controller/DcMYSQLJob.java

@ -2,7 +2,6 @@ package com.zc.business.controller;
import com.ruoyi.common.config.RuoYiConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@ -20,7 +19,7 @@ import java.util.Date;
@Component
@Slf4j
public class DcMYSQLJob {
private final Environment environment;
//private final Environment environment;
private static final String IP = "10.0.81.202";
@ -30,9 +29,9 @@ public class DcMYSQLJob {
private static final String PASSWORD = "mysql123!@#";
public DcMYSQLJob(Environment environment) {
this.environment = environment;
}
//public DcMYSQLJob(Environment environment) {
// this.environment = environment;
//}
@Scheduled(cron = "0 0 22 * * ?")
//@Scheduled(cron = "0 10 * * * ?")

8
zc-business/src/main/java/com/zc/business/controller/DcMYSQLUtil.java

@ -16,14 +16,14 @@ public class DcMYSQLUtil {
*/
public static void backup(String host, String port, String userName, String password, String dbName, File file) {
String cmd = "mysqldump --single-transaction " + " -h" + host + " -P'" + port + "' -u" + userName + " -p" + password + " --databases --skip-extended-insert " + dbName + " > " + file.getPath();
String cmd = "";
String os = System.getProperties().getProperty("os.name");
if (os.contains("Windows")) {
// Windows 需要加上 cmd /c
cmd = "cmd /c " + cmd;
cmd = "cmd /c mysqldump --single-transaction " + " -h" + host + " -P" + port + " -u" + userName + " -p" + password + " --databases --skip-extended-insert " + dbName + " > " + file.getPath();
} else {
// Windows 需要加上 /usr/local/mysql/bin/
cmd = "/usr/local/mysql/bin/" + cmd;
// Linux 需要加上 /usr/local/mysql/bin/
cmd = "/usr/local/mysql/bin/mysqldump --single-transaction " + " -h" + host + " -P" + port + " -u" + userName + " -p'" + password + "' --databases --skip-extended-insert " + dbName + " > " + file.getPath();
}
System.out.printf("cmd命令为:%s%n", cmd);
try {

Loading…
Cancel
Save