日期:2014-05-16  浏览次数:20716 次

Java进行Mysql的备份与恢复操作
1 进行恢复操作之前,需要使用Mysql client命令行工具将目标数据库先创建好;
2 导出的结果为一个*.sql文件,详细代码如下所示:

package com.justin.mysql;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class MysqlBackup {
	private static String mysqlBinPath = "C:/Program Files/MySQL/MySQL Server 5.5/bin";//MySql安装路径
	private static String exprotSqlPath = "d:/tmp/MySql/backup/emis.sql";//导出文件存放位置
	private static String importSqlPath = "d:/tmp/MySql/backup/emis.sql";//待导入文件存放位置
	private static String userName = "root";//数据库用户名
	private static String password = "root123";//数据库密码
	private static String dbToExport = "emis";//待导出的数据库实例名
	private static String dbToImport = "db_backup";//待导入数据的数据库实例名

	/**
	 * 用途:数据导出
	 */
	public static void backup() {
		try {
			Runtime rt = Runtime.getRuntime();
			// 调用 调用mysql的安装目录的命令
			Process mysqlProcess = rt.exec(mysqlBinPath + "//mysqldump -h localhost -u" + userName + " -p" + password + "  " + dbToExport);
			// 设置导出编码为utf-8。这里必须是utf-8
			// 把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。
			//注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行
			InputStream mysqlInputStream = mysqlProcess.getInputStream();// 控制台的输出信息作为输入流
			InputStreamReader mysqlIsr = new InputStreamReader(mysqlInputStream, "utf-8");
			// 设置输出流编码为utf-8。这里必须是utf-8,否则从流中读入的是乱码
			String inStr;
			StringBuffer sb = new StringBuffer("");
			String outStr;
			// 组合控制台输出信息字符串
			BufferedReader mysqlBf = new BufferedReader(mysqlIsr);
			while ((inStr = mysqlBf.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();

			// 要用来做导入用的sql目标文件:
			FileOutputStream fos = new FileOutputStream(exprotSqlPath);
			OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
			osw.write(outStr);
			osw.flush();
			mysqlInputStream.close();
			mysqlIsr.close();
			mysqlBf.close();
			osw.close();
			fos.close();
			System.out.println("**********Mysql数据库导出成功************");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 用途:恢复数据库
	 */
	public static void restore() {
		try {
			Runtime rt = Runtime.getRuntime();
			// 调用 mysql 的 cmd:      
			Process mysqlProcess = rt.exec(mysqlBinPath + "//mysql.exe -u" + userName + " -p" + password + " " + dbToImport);
			OutputStream mysqlOs = mysqlProcess.getOutputStream();//控制台的输入信息作为输出流      
			String inStr;
			StringBuffer sb = new StringBuffer("");
			String outStr;
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(importSqlPath), "utf8"));
			while ((inStr = br.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();
			OutputStreamWriter writer = new OutputStreamWriter(mysqlOs, "utf8");
			writer.write(outStr);
			// 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免      
			writer.flush();
			// 别忘记关闭输入输出流      
			mysqlOs.close();
			br.close();
			writer.close();
			System.out.println("*************成功将指定的sql备份文件导入数据库****************");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Start to backup()");
		backup();
		System.out.println("Backup() have finished");
		System.out.println("************************************");
		System.out.println("Start to restore()");
		restore();
		System.out.println("restore() have finished");
	}

}