Java对备份还原数据库(MysqlDump实现)

首先介绍MysqlDump命令

--多库备份命令

mysqldump -uroot -proot -h127.0.0.1 --default-character-set=utf8 --databases test1 test -r C:\xxx.sql

--多库还原命令

mysql -e "source c:\XXX.sql" -u root -proot -h127.0.0.1 -P3306 -f --default-character-set=utf-8

一.Java使用MysqlDump备份

public synchronized boolean exportSql(DBConfigInDTO dbConfigInDTO) {
        Process process = null;
        InputStream inStream = null;
        OutputStreamWriter writer = null;
        try {
            Runtime runtime = Runtime.getRuntime();
            //TODO 第一步,获取相关参数
            DBConfigInDTO dbConfigIn = getCommand(dbConfigInDTO);
            dbConfigIn.setDbPath(dbConfigIn.getDbPath() + sdtFileType);
            StringBuffer sbuff = new StringBuffer();
            sbuff.append(mysqlDumpInPath + "mysqldump");
            sbuff.append(" -u").append(dbConfigIn.getUsername());
            sbuff.append(" -p").append(dbConfigIn.getPassword());    //密码是用的小p
            sbuff.append(" -h").append(dbConfigIn.getHost());
            sbuff.append(" -P").append(dbConfigIn.getPort());        //而端口是用的大P
            sbuff.append(" --default-character-set=").append(dbConfigInDTO.getCharacterSet());
            sbuff.append(" --databases");
            sbuff.append(" ").append( dbConfigIn.getDbName());
            sbuff.append(" -r ").append(dbConfigIn.getDbPath());
            process = runtime.exec(sbuff.toString());   //这里简单一点异常我就直接往上抛
            //TODO 第二步,获取Process输入的输出流生成文件
            inStream = process.getInputStream();
            writer = new OutputStreamWriter(new FileOutputStream(dbConfigIn.getDbPath()), "utf-8");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                writer.write(line + "\n");
            }
            //TODO 第三步,获取执行日志
            String infoLine;
            StringBuffer infoMsg = new StringBuffer();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while((infoLine = bufferedReader.readLine()) != null) {
                infoMsg.append(infoLine).append("\n");
            }
            String str = infoMsg.toString();
            //TODO 第四步,返回导出结果信息
            if(process.waitFor() == 0){//0 表示线程正常终止。
                dbConfigInDTO.setMsg(str);
                if (str == null || str.toUpperCase().contains("ERROR")) {
                    throw new RuntimeException(str);
                } else {
                    dbConfigInDTO.setIsSuccess("yes");
                    return true;
                }
            } else {
                throw new RuntimeException(str);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        finally {
            if(null != process) {
                process.destroy();
            }
            if(null != inStream) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != writer) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

二.Java使用Mysql命令还原数据库


public synchronized boolean importSql(DBConfigInDTO dbConfigInDTO) {
        OutputStreamWriter osWriter = null;
        OutputStream os = null;
        Process process = null;
        try {
            DBConfigInDTO dbConfigIn = getCommand(dbConfigInDTO);
            //TODO 第一步,获取相关参数
            String command = new StringBuffer()
                    .append(mysqlDumpOutPath)
                    .append("mysql")
                    .append(" -e").append(" \"").append("source ").append(dbConfigIn.getDbPath()).append("\"")
                    .append(" -u").append(dbConfigIn.getUsername())
                    .append(" -p").append(dbConfigIn.getPassword())
                    .append(" -h").append(dbConfigIn.getHost())
                    .append(" -P").append(dbConfigIn.getPort())
                    .append(" -f")
                    .append(" --default-character-set=").append(dbConfigInDTO.getCharacterSet())
                    .toString();

            Runtime runtime = Runtime.getRuntime();
            process = runtime.exec(command);
            //TODO 第二步,获取执行日志
            String line;
            StringBuffer infoMsg = new StringBuffer();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while((line = bufferedReader.readLine()) != null) {
                infoMsg.append(line).append("\n");
            }
            int processComplete = process.waitFor();
            String str = infoMsg.toString();
            //TODO 第三步,返回导出结果信息
            if (processComplete == 0) {
                dbConfigInDTO.setMsg(str);
                if (str == null || str.toUpperCase().contains("ERROR")) {
                    throw new RuntimeException(str);
                } else {
                    dbConfigInDTO.setIsSuccess("yes");
                    return true;
                }
            } else {
                throw new RuntimeException(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
            if(null != osWriter) {
                try {
                    osWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

你可能感兴趣的:(Java对备份还原数据库(MysqlDump实现))