@Log4j2
public class DbBackupUtils {
/**
* 备份数据库语句
*
* @param hostIP
* @param userName
* @param password
* @param databaseName
* @return
*/
public static String[] getBackUpCommand(String hostIP, String userName, String password, String databaseName, String url) {
String[] cmd = new String[3];
String os = System.getProperties().getProperty("os.name");
if (os.startsWith("Win")) {
cmd[0] = "cmd.exe";
cmd[1] = "/c";
} else {
cmd[0] = "/bin/sh";
cmd[1] = "-c";
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("mysqldump").append(" -h").append(hostIP);
stringBuilder.append(" -u ").append(userName).append(" -p").append(password);
stringBuilder.append(" --databases ").append(databaseName);
stringBuilder.append(" > ").append(url);
cmd[2] = stringBuilder.toString();
return cmd;
}
/**
* 恢复数据库语句
*
* @return
*/
public static String[] getCommand(String userName, String psw, String ip, String db, String url) {
String[] cmd = new String[3];
String os = System.getProperties().getProperty("os.name");
if (os.startsWith("Win")) {
cmd[0] = "cmd.exe";
cmd[1] = "/c";
} else {
cmd[0] = "/bin/sh";
cmd[1] = "-c";
}
StringBuilder arg = new StringBuilder();
arg.append("mysql ");
arg.append("-h" + ip + " ");
arg.append("-u" + userName + " ");
arg.append("-p" + psw + " ");
arg.append(db + " < ");
arg.append(url);
cmd[2] = arg.toString();
return cmd;
}
/**
* 备份方法
*
* @param file
* @param command
* @throws
*/
public static boolean backup(File file, String[] command) {
log.info("备份文件地址:" + file.getPath());
for (int i = 0; i < command.length; i++) {
log.info("备份命令:" + command[i]);
}
final Process process;
try {
/**
* ##############特别关注的点#############
*
* 创建一个线程用户获取错误流,并且这个错误流是有返回结果的,所以用的是Callable来实现
*/
//-u后面是用户名,-p是密码-p后面最好不要有空格,-test是数据库的名字
Runtime runtime = Runtime.getRuntime();
log.info("开始备份数据库中... command: " + JSON.toJSONString(command));
process = runtime.exec(command);
log.info("数据库备份结束...");
// 新增一个线程用于处理错误流,如果有错误就返回true,没有就返回false
ErrorStreamUtil errThread = new ErrorStreamUtil(process);
FutureTask<Boolean> result = new FutureTask<Boolean>(errThread);
new Thread(result).start();
if (result.get()) {
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* mysql的还原方法
*
* @param command 命令行
* @param savePath 还原路径
* @return
*/
public static boolean recover(String[] command, String savePath) {
log.info("文件所在位置:" + savePath);
log.info("cmd命令:" + command[2]);
Runtime runtime = Runtime.getRuntime();
try {
log.info("开始恢复数据库中... command: " + JSON.toJSONString(command));
Process process = runtime.exec(command);
log.info("数据库恢复结束...");
// 新增一个线程用于处理错误流,如果有错误就返回true,没有就返回false
ErrorStreamUtil errThread = new ErrorStreamUtil(process);
FutureTask<Boolean> result = new FutureTask<Boolean>(errThread);
new Thread(result).start();
/*// 执行sql文件
InputStreamRestoreUtil restoreUtil = new InputStreamRestoreUtil(process, new File(savePath));
Thread thread = new Thread(restoreUtil);
thread.start();
thread.join();*/
if (result.get()) {
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
log.error("Mysql恢复出错: {}", e);
return false;
}
}
/**
* 删除文件
*
* @param file
*/
public static void deleteFiles(File file) {
// 判断文件是否存在
if (file.exists()) {
if (file.isFile()) {
file.delete();
} else {
File[] files = file.listFiles();
for (File f : files) {
deleteFiles(f);
}
}
} else {
}
}
}
数据库恢复与备份工具类
最新推荐文章于 2023-01-25 11:16:31 发布