MySQLDump备份文件恢复

mysqldump备份的表结构(可参考https://studyjava.cn/post/1696),恢复到数据库中

package cv.datacenter.etl;

import java.io.*;
import java.util.List;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;

/**
 * @author zsljava
 * @date 2022年04月29日 4:01
 * @description TODO
 */
public class MySQLDump {

    private static String MYSQL_HOST = "192.168.1.24";
    private static String MYSQL_NAME = "root";
    private static String MYSQL_PWD = "root";
    private static String MYSQL_PORT = "3306";

    public static void main(String[] args) throws IOException, InterruptedException {
        File[] ls = FileUtil.ls("C:\\Users\\zsljava\\Desktop\\dbback");

        String sql = "D:\\Develop\\Tool\\phpstudy_pro\\Extensions\\MySQL5.7.26\\bin\\mysql -u"+MYSQL_NAME+" -h"+MYSQL_HOST+" -P"+MYSQL_PORT+" -p"+MYSQL_PWD+" < ";
        for (File f:ls) {
            if(f.isDirectory()){
                continue;
            }
            File unzip = ZipUtil.unzip(f);


            List<File> files = FileUtil.loopFiles(unzip);
            for (File file : files) {
//                System.out.println(sql + file.getAbsolutePath());
                String cmd = sql + file.getAbsolutePath();
//                String[] command = { "cmd", "/c", cmd};
                Process process = Runtime.getRuntime().exec("cmd.exe /c " + cmd);
                // 输出结果,必须写在 waitFor 之前
                String outStr = getStreamStr(process.getInputStream());
                // 错误结果,必须写在 waitFor 之前
                String errStr = getStreamStr(process.getErrorStream());
                int exitValue = process.waitFor(); // 退出值 0 为正常,其他为异常
                System.out.println("exitValue: " + exitValue);
                System.out.println("outStr: " + outStr);
                System.out.println("errStr: " + errStr);
                System.out.println("执行完成 = " + cmd);
            }
        }
    }


    public static String getStreamStr(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        br.close();
        return sb.toString();
    }
}


已有 0 条评论

    欢迎您,新朋友,感谢参与互动!