mysql数据备份-java实现

import java.io.*;

public class DataBackupService {

    public static final String CMD = "mysqldump --databases -h%s -P%s -u%s -p%s %s";

    /**
     * 备份数据库 ,控制台执行命令格式
     * mysql的bin目录/mysqldump --databases  -h主机ip -P端口  -u用户名 -p密码 数据库名
     *
     * @param mysqlIp    mysql主机ip
     * @param mysqlPort  端口
     * @param userName   用户名
     * @param password   密码
     * @param database   数据库名
     */
    public void backup(String mysqlIp,
                       String mysqlPort,
                       String userName,
                       String password,
                       String database) {
        try {
            String cmd = String.format(CMD,mysqlIp,mysqlPort,userName,password,database);
            Process process = Runtime.getRuntime().exec(cmd);
            new InputStreamProcessor(process).start();
            new ErrorInputStream(process).start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static class InputStreamProcessor extends Thread{
        private final Process process;

        private final String path = String.format("d:\\%s_wms_ts_test.sql",System.currentTimeMillis());

        public InputStreamProcessor(Process process) {
            this.process = process;
        }

        @Override
        public void run() {
            try (InputStream is = process.getInputStream();
                 FileOutputStream out = new FileOutputStream(path);){
                byte[] b = new byte[1024];
                int len = -1;
                while ((len = is.read(b)) != -1) {
                    out.write(b, 0, len);
                }
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }

    static class ErrorInputStream extends Thread{
        private final Process process; // 控制台errorStream

        private final String path = String.format("d:\\%s_error_wms_ts_test.sql",System.currentTimeMillis());

        public ErrorInputStream(Process process) {
            this.process = process;
        }

        @Override
        public void run() {
            try (InputStream is = process.getErrorStream();
                 FileOutputStream out = new FileOutputStream(path);){
                byte[] b = new byte[1024];
                int len = -1;
                while ((len = is.read(b)) != -1) {
                    out.write(b, 0, len);
                }
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }

    public static void main(String[] args) {
        String mysqlIp = "127.0.0.1";
        String mysqlPort = "3306";
        String userName = "root";
        String password = "root";
        String database = "wms_ts_test";
        DataBackupService service = new DataBackupService();
        service.backup(mysqlIp,mysqlPort,userName,password,database);
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值