Commit 17ad49ba authored by Administrator's avatar Administrator

2024-3.19

parent a0f36d5d
package me.zhengjie.gemho.controller.tab;
import me.zhengjie.annotation.Log;
import me.zhengjie.gemho.util.PostOrPutResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -8,9 +10,16 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>
......@@ -23,15 +32,34 @@ import java.util.Date;
@RestController
@RequestMapping("tab/back")
public class DataBack {
@Value(value = "${filepath}")
@Value(value = "${backPath}")
String filepath;
@Autowired
private DataSource dataSource;
@Value(value = "${spring.datasource.druid.password}")
String password;
@Value(value = "${environment.path}")
private String envPath;
@Log("备份数据库")
@PostMapping
public ResponseEntity<Object> deal() {
System.out.println("备份数据库开始执行");
try {
Connection connection = dataSource.getConnection();
DatabaseMetaData metaData = connection.getMetaData();
String name = connection.getCatalog();
String userName = metaData.getUserName();
Pattern pattern = Pattern.compile("^(.*?)@");
Matcher matcher = pattern.matcher(userName);
if (matcher.find()) {
userName = matcher.group(1);
System.out.println(userName);
}
String url = metaData.getURL();
String ipAddress = extractIpAddress(url);
String backName = new SimpleDateFormat("yyMMddHHmmss").format(new Date()) + ".sql";
dbBackup("root", "jinghe2021//", "mdm_1_db", "d:/file/", backName);
dbBackup(envPath,userName, password, name, filepath, backName,ipAddress);
} catch (Exception ex) {
System.out.println("备份异常");
ex.printStackTrace();
......@@ -63,7 +91,7 @@ public class DataBack {
return ps;
}
public static void dbBackup(String username, String authenticate, String dbName, String destination, String backName) {
public static void dbBackupComd(String username, String authenticate, String dbName, String destination, String backName,String url) {
File backupDir = new File(destination);
if (!backupDir.exists()) {
backupDir.mkdirs();
......@@ -76,8 +104,8 @@ public class DataBack {
//mysqldump -h localhost -u root -p db_name > db_name.sql
StringBuffer buffer = new StringBuffer();
buffer.append("mysqldump");
buffer.append(" -h 8.142.46.126 ");
buffer.append(" --column-statistics=0 ");
buffer.append(" -h "+url+" ");
//buffer.append(" --column-statistics=0 ");
buffer.append(" -u" + username);
buffer.append(" -p" + authenticate);
buffer.append(" " + dbName + " -r ");
......@@ -94,17 +122,52 @@ public class DataBack {
ex.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("备份数据库开始执行");
public static void dbBackup(String envPath,String username, String authenticate, String dbName, String destination, String backName, String url) {
File backupDir = new File(destination);
if (!backupDir.exists()) {
backupDir.mkdirs();
}
try {
String backName = new SimpleDateFormat("yyMMddHHmmss").format(new Date()) + ".sql";
dbBackup("root", "jinghe2021//", "mdm_1_db", "d:/file/", backName);
File sqlFile = new File(backupDir, backName);
if (!sqlFile.exists()) {
sqlFile.createNewFile();
}
String cmd = envPath+"mysqldump -h " + url + " -u" + username + " -p" + authenticate + " " + dbName + " -r " + sqlFile;
System.out.println("cmd命令为:" + cmd);
ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", cmd);
processBuilder.redirectError(new File(destination+"error.log"));
Process process = processBuilder.start();
// 读取命令执行的输出(包括标准输出和标准错误)
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("备份成功!");
} else {
System.out.println("备份失败,Exit Code: " + exitCode);
}
} catch (Exception ex) {
System.out.println("备份异常");
ex.printStackTrace();
}
System.out.println("备份数据库结束");
}
private String extractIpAddress(String jdbcUrl) {
String ipAddress = null;
// 使用正则表达式提取 IP 地址部分
Pattern pattern = Pattern.compile("//(.*?):");
Matcher matcher = pattern.matcher(jdbcUrl);
if (matcher.find()) {
ipAddress = matcher.group(1);
}
return ipAddress;
}
}
......
......@@ -102,6 +102,7 @@ public class StData implements Serializable {
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@TableField(exist = false)
public Date jctime;
@TableField(exist = false)
public String danwei;
@TableField(exist = false)
......
......@@ -90,7 +90,7 @@ public class AuthorizationController {
networkTime=LocalDateTime.now();
}
//和日期比较
String expirationTimeString="2023-12-26 07:00:00";
String expirationTimeString="2099-03-26 07:00:00";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime expirationTime = LocalDateTime.parse(expirationTimeString,dateTimeFormatter);
boolean after = networkTime.isAfter(expirationTime);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment