JAVA方法实现MySQL数据库的备份和恢复

通常在MySQL数据库的备份和恢复的时候,多是采用在cmd中执行mysql命令来实现。

例如:

  mysqldump -h127.0.0.1 -uroot -p123456 test > d:/test.sql ---备份test数据库到 D 盘

  mysql -h127.0.0.1 -uroot -p123456 test< test.sql ---将D备份的数据库脚本,恢复到数据库中(数据库要存在!)

       在cmd调用命令行,其实是调用 mysql安装路径下面的bin目录下面的 msqldump.exe和mysql.exe来完成相应的工作

所以,在java代码中,我们也需要通过调用 mysqldump.exe和mysql.exe来完成备份和恢复的工作

Runtime.getRuntime().exec(String args);  java调用外部软件exe执行命令的api;

linux下:

String[] command = { "/bin/sh", "-c", command };

Process ps = Runtime.getRuntime().exec(command );

windows下:

String[] command = { "cmd", "/c", command};

Process ps = Runtime.getRuntime().exec(command );

在windows下相当于直接调用   /开始/搜索程序和文件  的指令,比如                              

                         Runtime.getRuntime().exec("notepad.exe");  -------打开windows下记事本。

package com.cxx.backupdb;
 
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * @Author: cxx
 * 数据库备份与还原
 * @Date: 2018/4/28 19:56
 */
public class DbOperate {
 
    /**
     * 备份数据库db
     * @param root
     * @param pwd
     * @param dbName
     * @param backPath
     * @param backName
     */
    public static void dbBackUp(String root,String pwd,String dbName,String backPath,String backName) throws Exception {
        String pathSql = backPath+backName;
        File fileSql = new File(pathSql);
        //创建备份sql文件
        if (!fileSql.exists()){
            fileSql.createNewFile();
        }
        //mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
        StringBuffer sb = new StringBuffer();
        sb.append("mysqldump");
        sb.append(" -h127.0.0.1");
        sb.append(" -u"+root);
        sb.append(" -p"+pwd);
        sb.append(" "+dbName+" >");
        sb.append(pathSql);
        System.out.println("cmd命令为:"+sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始备份:"+dbName);
        Process process = runtime.exec("cmd /c"+sb.toString());
        System.out.println("备份成功!");
    }
 
    /**
     * 恢复数据库
     * @param root
     * @param pwd
     * @param dbName
     * @param filePath
     * mysql -hlocalhost -uroot -p123456 db < /home/back.sql
     */
    public static void dbRestore(String root,String pwd,String dbName,String filePath){
        StringBuilder sb = new StringBuilder();
        sb.append("mysql");
        sb.append(" -h127.0.0.1");
        sb.append(" -u"+root);
        sb.append(" -p"+pwd);
        sb.append(" "+dbName+" <");
        sb.append(filePath);
        System.out.println("cmd命令为:"+sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始还原数据");
        try {
            Process process = runtime.exec("cmd /c"+sb.toString());
            InputStream is = process.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is,"utf8"));
            String line = null;
            while ((line=bf.readLine())!=null){
                System.out.println(line);
            }
            is.close();
            bf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("还原成功!");
    }
 
 
 
    public static void main(String[] args) throws Exception {
        String backName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+".sql";
        DbOperate.dbBackUp("root","123456","ks","F:/",backName);
        dbRestore("root","123456","db","F://2018-04-30-19-28-28.sql");
    }
 
}

 

你可能感兴趣的:(Java)