MySQL备份还原之Java实现

8 篇文章 0 订阅

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();
            }
        }
    }
 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值