1.备份
/**
* 备份数据库
* @param host 数据库地址,例如127.0.0.1
* @param user 数据库账号,例如 root
* @param password 数据库登录密码,例如123456
* @param database 要备份的数据库名,例如mydb
* @param sql_path 备份后的数据文件存储地址,例如E:\\db_backup\\1016.sql
* @param bat_path 执行备份命令的bat文件存储地址,例如D:\\my_bat\\sql\\backup.bat
* @throws Exception
*/
public static void backup(String host, String user, String password, String database, String sql_path, String bat_path) throws Exception {
// mysqldump -h127.0.0.1 -uroot -p123456 mydb > E:\\db_backup\\1016.sql
//组装bat命令
StringBuffer sb = new StringBuffer();
sb.append("mysqldump ");
sb.append(" -h" + host);
sb.append(" -u" + user);
sb.append(" -p" + password);
sb.append(" " + database + " > ");
sb.append(sql_path);
//创建bat文件 并写入内容
File bat_file = new File(bat_path);
bat_file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(bat_path));
bw.write(sb.toString());
bw.flush();
bw.close();
//执行bat,并输出过程信息
CommandUtil commandUtil = new CommandUtil();
commandUtil.executeCommand(bat_path);
List<String> stdoutList = commandUtil.getStdoutList();
List<String> erroroutList = commandUtil.getErroroutList();
System.out.println("----正常过程信息如下-----");
for (String info : stdoutList) {
System.out.println("----" + info);
}
System.out.println("----错误提示如下-----");
for (String errInfo : erroroutList) {
System.out.println("----" + errInfo);
}
System.out.println("----------备份结束---");
}
2.还原
注意:执行此方法前,需确保要还原的数据库存在
/**
* 还原数据库
* @param user 数据库账号,例如 root
* @param password 数据库登录密码,例如123456
* @param database 要备份的数据库名,例如mydb
* @param sql_path 备份后的数据文件存储地址,例如E:\\db_backup\\1016.sql
* @param bat_path 执行备份命令的bat文件存储地址,例如D:\\my_bat\\sql\\recover.bat
* @throws IOException
* @throws Exception
*/
public static void recover2(String user, String password, String database, String sql_path, String bat_path) throws IOException {
// mysql -uroot -p123456 mydb < E:\\db_backup\\1016.sql
//组装bat命令
StringBuffer sb = new StringBuffer();
sb.append("mysql ");
sb.append(" -u" + user);
sb.append(" -p" + password);
sb.append(" " + database + " < ");
sb.append(sql_path);
//创建bat文件 并写入内容
File bat_file = new File(bat_path);
bat_file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(bat_path));
bw.write(sb.toString());
bw.flush();
bw.close();
//执行bat,并输出过程信息
CommandUtil commandUtil = new CommandUtil();
commandUtil.executeCommand(bat_path);
List<String> stdoutList = commandUtil.getStdoutList();
List<String> erroroutList = commandUtil.getErroroutList();
System.out.println("----正常过程信息如下-----");
for (String info : stdoutList) {
System.out.println("----" + info);
}
System.out.println("----错误提示如下-----");
for (String errInfo : erroroutList) {
System.out.println("----" + errInfo);
}
System.out.println("----------还原结束---");
}
3.工具类
package com.cn.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CommandUtil {
private List<String> stdoutList = new ArrayList<String>();
private List<String> erroroutList = new ArrayList<String>();
public void executeCommand(String command) {
stdoutList.clear();
erroroutList.clear();
Process p = null;
try {
p = Runtime.getRuntime().exec(command);
ThreadUtil stdoutUtil = new ThreadUtil(p.getInputStream(), stdoutList);
ThreadUtil erroroutUtil = new ThreadUtil(p.getErrorStream(), erroroutList);
stdoutUtil.start();
erroroutUtil.start();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public List<String> getStdoutList() {
return stdoutList;
}
public List<String> getErroroutList() {
return erroroutList;
}
}
class ThreadUtil implements Runnable {
private String character = "GB2312";
private List<String> list;
private InputStream inputStream;
public ThreadUtil(InputStream inputStream, List<String> list) {
this.inputStream = inputStream;
this.list = list;
}
public void start() {
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.start();
}
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream, character));
String line = null;
while ((line = br.readLine()) != null) {
if (line != null) {
list.add(line);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}