package com.xxs.util;
import com.xxs.common.Constant;
import com.xxs.exception.BackupException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.time.LocalDateTime;
import java.util.Map;
/**
* MySQL 数据库备份
*/
public class MySQLDbBackupUtil {
private static final Logger log = LoggerFactory.getLogger(MySQLDbBackupUtil.class);
/**
* 备份数据库
*
* @param param 备份参数
* @param tableNames 备份表名
*/
public static void backup(Map<String,String> param, String... tableNames) throws BackupException {
try{
File backupFile = new File(param.get(Constant.FILE_PATH));
if (!backupFile.getParentFile().exists()) {
backupFile.getParentFile().mkdirs();
}
String tableName;
StringBuilder stringBuilder = new StringBuilder(" ");
for (String name : tableNames) {
if (!"".equals(name.trim())) {
stringBuilder.append(name).append(" ");
}
}
tableName = stringBuilder.toString();
String cmd1 = "/bin/sh";
String cmd2 = "-c";
String os_name = System.getProperty("os.name");
log.info("--------------------开始时间:【{}】, 系统开启数据库备份--------------------", LocalDateTime.now());
StringBuilder stmt = new StringBuilder("mysqldump --defaults-extra-file=/etc/my.cnf --default-character-set=utf8 ").
append(param.get(Constant.DB_NAME));
if (tableNames.length > 0) {
log.info("备份环境信息:【{}】, 数据库:【{}】, 表【{}】", os_name, param.get(Constant.DB_NAME), tableName);
stmt.append(" --tables").append(tableName);
} else {
log.info("备份环境信息:【{}】, 数据库:【{}】", os_name, param.get(Constant.DB_NAME));
}
stmt.append(" > ").append(param.get(Constant.FILE_PATH));
log.info(String.format("cmd命令为:%s", stmt));
String[] cmd = { cmd1, cmd2 , stmt.toString() };
Process process = Runtime.getRuntime().exec(cmd);
if(process.waitFor() == 0){//0 表示线程正常终止。
log.info(String.format(" 【数据库:%s】 >>> 【文件:%s】 备份成功!", param.get(Constant.DB_NAME), param.get(Constant.FILE_PATH)));
}else{
log.info(String.format(" 【数据库:%s】 >>> 【文件:%s】 备份失败!", param.get(Constant.DB_NAME), param.get(Constant.FILE_PATH)));
throw new BackupException("数据库备份失败");
}
log.info("--------------------结束时间:【{}】, 系统结束数据库备份--------------------", LocalDateTime.now());
}catch (Exception e){
log.error(e.getMessage(),e);
throw new BackupException("数据库备份异常");
}
}
/**
* 恢复数据库
* @param param 恢复参数
*/
public static void restore(Map<String,String> param) throws BackupException {
String cmd1 = "/bin/sh";
String cmd2 = "-c";
String os_name = System.getProperty("os.name");
String stmt2 = "mysql --defaults-extra-file=/etc/my.cnf "+param.get(Constant.DB_NAME)+" < " + param.get(Constant.FILE_PATH);
String[] cmd = { cmd1, cmd2, stmt2 };
try {
log.info(--------------------开始时间:【{}】, 系统开启数据库恢复--------------------", LocalDateTime.now());
log.info("恢复环境信息:【{}】, 数据库:【{}】", os_name, param.get(Constant.DB_NAME));
log.info(String.format("cmd命令为:%s", stmt2));
Process process = Runtime.getRuntime().exec(cmd);
if(process.waitFor() == 0){//0 表示线程正常终止。
log.info(String.format(" 【文件:%s】 >>> 【数据库:%s】 恢复成功!", param.get(Constant.FILE_PATH), param.get(Constant.DB_NAME)));
}else{
log.info(String.format(" 【文件:%s】 >>> 【数据库:%s】 恢复失败!", param.get(Constant.FILE_PATH), param.get(Constant.DB_NAME)));
throw new BackupException("数据库恢复失败");
}
log.info("--------------------结束时间:【{}】, 系统结束数据库恢复--------------------", LocalDateTime.now());
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new BackupException("数据库恢复异常");
}
}
}
java程序实现MySQL数据库备份与恢复工具类
最新推荐文章于 2024-07-19 00:12:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)