parent
fbcba7ae16
commit
acf5472a88
1 changed files with 127 additions and 0 deletions
@ -0,0 +1,127 @@ |
|||||||
|
package org.springblade.system.util; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springframework.http.HttpStatus; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.time.LocalDateTime; |
||||||
|
import java.time.format.DateTimeFormatter; |
||||||
|
import java.util.logging.Logger; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Description 数据库备份工具类 |
||||||
|
* @Author ytl |
||||||
|
* @Date 2022/9/28 0028 8:52 |
||||||
|
*/ |
||||||
|
|
||||||
|
@Slf4j |
||||||
|
public class SqlBackup { |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取操作系统类型,window 返回true,linux返回false |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static boolean isWindowsSystem() { |
||||||
|
String osName = System.getProperty("os.name").toLowerCase(); |
||||||
|
Boolean flag = null; |
||||||
|
if (osName.startsWith("windows")) { |
||||||
|
flag = true; |
||||||
|
} else if (osName.startsWith("linux")) { |
||||||
|
flag = false; |
||||||
|
} |
||||||
|
return flag; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取数据库名 |
||||||
|
*/ |
||||||
|
public static String getDataBaseName(String url) { |
||||||
|
return url.substring(url.indexOf("3306"), url.indexOf("?")).replaceAll("/", "").replaceAll("3306", ""); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取主机地址,ip:port |
||||||
|
*/ |
||||||
|
public static String getHost(String url) { |
||||||
|
String s = url.substring(url.indexOf("mysql"), url.indexOf("?")).replace("//", "").replace("mysql", "") |
||||||
|
.split("/")[0]; |
||||||
|
return s.substring(1,s.length()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 备份数据库 |
||||||
|
* @param url |
||||||
|
* @param name |
||||||
|
* @param password |
||||||
|
* @param path |
||||||
|
*/ |
||||||
|
public static void backup(String url,String name,String password,String path){ |
||||||
|
|
||||||
|
String host = getHost(url); |
||||||
|
String[] split = host.split(":"); |
||||||
|
// 获取ip
|
||||||
|
String ip = split[0]; |
||||||
|
// 获取端口号
|
||||||
|
String port = split[1]; |
||||||
|
// 获取数据库名称
|
||||||
|
String database_name = getDataBaseName(url); |
||||||
|
|
||||||
|
// 最终保存到磁盘中的数据库文件名称
|
||||||
|
StringBuilder mysqlFileName = new StringBuilder() |
||||||
|
.append(database_name) |
||||||
|
.append("_") |
||||||
|
.append(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))) |
||||||
|
.append(".sql"); |
||||||
|
// 备份命令
|
||||||
|
StringBuilder cmd = new StringBuilder() |
||||||
|
.append("mysqldump ") |
||||||
|
.append("--no-tablespaces ") |
||||||
|
.append("-h") |
||||||
|
.append(ip) |
||||||
|
.append(" -u") |
||||||
|
.append(name) |
||||||
|
.append(" -p") |
||||||
|
.append(password) |
||||||
|
// 排除MySQL备份表
|
||||||
|
.append(" --ignore-table ") |
||||||
|
.append(database_name) |
||||||
|
.append(".mysql_backups ") |
||||||
|
.append(database_name) |
||||||
|
.append(" > ") |
||||||
|
.append(path) |
||||||
|
.append(mysqlFileName); |
||||||
|
|
||||||
|
// 判断文件夹是否存在
|
||||||
|
File saveFile = new File(path); |
||||||
|
if (!saveFile.exists()) { |
||||||
|
saveFile.mkdirs(); |
||||||
|
} |
||||||
|
|
||||||
|
// 获取操作系统名称
|
||||||
|
String osName = System.getProperty("os.name").toLowerCase(); |
||||||
|
String[] command = new String[0]; |
||||||
|
if (isWindowsSystem()) { |
||||||
|
// Windows
|
||||||
|
command = new String[]{"cmd", "/c", String.valueOf(cmd)}; |
||||||
|
} else { |
||||||
|
// Linux
|
||||||
|
command = new String[]{"/bin/sh", "-c", String.valueOf(cmd)}; |
||||||
|
} |
||||||
|
|
||||||
|
// 获取Runtime实例
|
||||||
|
Process process = null; |
||||||
|
try { |
||||||
|
process = Runtime.getRuntime().exec(command); |
||||||
|
if (process.waitFor() == 0) { |
||||||
|
log.info("Mysql 数据库备份成功,备份文件名:{}", mysqlFileName); |
||||||
|
} else { |
||||||
|
int value = HttpStatus.INTERNAL_SERVER_ERROR.value(); |
||||||
|
log.error( "网络异常,数据库备份失败1"); |
||||||
|
} |
||||||
|
} catch (Exception e) { |
||||||
|
e.printStackTrace(); |
||||||
|
int value = HttpStatus.INTERNAL_SERVER_ERROR.value(); |
||||||
|
log.error( "网络异常,数据库备份失败2"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue