/**
* Java代码实现MySQL数据库导出
*
* @param hostIP MySQL数据库所在服务器地址IP
* @param userName 进入数据库所需要的用户名
* @param password 进入数据库所需要的密码
* @param savePath 数据库导出文件保存路径
* @param fileName 数据库导出文件文件名
* @param databaseName 要导出的数据库名
* @return 返回true表示导出成功,否则返回false。
* @author GaoHuanjie
*/
public static boolean exportDatabaseTool(String hostIP, String hostPort, String userName, String password, String savePath, String fileName, String databaseName)throws InterruptedException {
File saveFile =new File(savePath);
if (!saveFile.exists()) {// 如果目录不存在
saveFile.mkdirs();// 创建文件夹
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}
PrintWriter printWriter =null;
BufferedReader bufferedReader =null;
try {
Runtime runtime = Runtime.getRuntime();
String path = ResourceUtils.getURL("classpath:static").getPath().replace('/','\\');
path = path.substring(1);
log.info(path);
//String cmd = "mysqldump -h127.0.0.1 -uroot -P3308 -p123456 archives";
String cmd ="mysqldump -h" + hostIP +" -u" + userName +" -P" + hostPort +" -p" + password +" " + databaseName;
cmd = path +"\\" + cmd;
log.info(cmd);
Process process = runtime.exec(cmd);
InputStreamReader inputStreamReader =new InputStreamReader(process.getInputStream(),"utf8");
bufferedReader =new BufferedReader(inputStreamReader);
printWriter =new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName),"utf8"));
String line;
while ((line = bufferedReader.readLine()) !=null) {
printWriter.println(line);
}
printWriter.flush();
if (process.waitFor() ==0) {//0 表示线程正常终止。
return true;
}
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (bufferedReader !=null) {
bufferedReader.close();
}
if (printWriter !=null) {
printWriter.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
/**
* Java实现MySQL数据库导入
*
* @param hostIP MySQL数据库所在服务器地址IP
* @param userName 数据库用户名
* @param password 进入数据库所需要的密码
* @param importFilePath 数据库文件路径
* @param sqlFileName 数据库文件名
* @param databaseName 要导入的数据库名
* @return 返回true表示导入成功,否则返回false。
* @author GaoHuanjie
*/
public static boolean importDatabase(String hostIP, String hostPort, String userName, String password, String importFilePath, String sqlFileName, String databaseName) {
File saveFile =new File(importFilePath);
if (!saveFile.exists()) {// 如果目录不存在
saveFile.mkdirs();// 创建文件夹
}
if (!importFilePath.endsWith(File.separator)) {
importFilePath = importFilePath + File.separator;
}
StringBuilder stringBuilder =new StringBuilder();
stringBuilder.append("mysql").append(" -h").append(hostIP);
stringBuilder.append(" -u").append(userName).append(" -P").append(hostPort).append(" -p").append(password);
stringBuilder.append(" ").append(databaseName);
stringBuilder.append(" <").append(importFilePath).append(sqlFileName);
try {
Process process = Runtime.getRuntime().exec("cmd /c " + stringBuilder.toString());//必须要有“cmd /c ”
if (process.waitFor() ==0) {// 0 表示线程正常终止。
return true;
}
}catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args)throws IOException {
try {
if (exportDatabaseTool("127.0.0.1","3308","root","123456","D:/backupDatabase","archive-2019-01-15.sql","archives")) {
System.out.println("数据库成功备份!!!");
}else {
System.out.println("数据库备份失败!!!");
}
}catch (InterruptedException e) {
e.printStackTrace();
}
if (importDatabase("127.0.0.1","3308","root","123456","D:\\backupDatabase","archive-2019-01-15.sql","archives")) {
System.out.println("数据库导入成功!!!");
}else {
System.out.println("数据库导入失败!!!");
}
Runtime runtime = Runtime.getRuntime();
runtime.exec("mysqldump -h127.0.0.1 -uroot -P3308 -p123456 archives >d:\\123456.sql");
}