数据库恢复与备份工具类

@Log4j2
public class DbBackupUtils {
    /**
     * 备份数据库语句
     *
     * @param hostIP
     * @param userName
     * @param password
     * @param databaseName
     * @return
     */
    public static String[] getBackUpCommand(String hostIP, String userName, String password, String databaseName, String url) {
        String[] cmd = new String[3];
        String os = System.getProperties().getProperty("os.name");
        if (os.startsWith("Win")) {
            cmd[0] = "cmd.exe";
            cmd[1] = "/c";
        } else {
            cmd[0] = "/bin/sh";
            cmd[1] = "-c";
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("mysqldump").append(" -h").append(hostIP);
        stringBuilder.append(" -u ").append(userName).append(" -p").append(password);
        stringBuilder.append(" --databases ").append(databaseName);
        stringBuilder.append(" > ").append(url);
        cmd[2] = stringBuilder.toString();
        return cmd;
    }


    /**
     * 恢复数据库语句
     *
     * @return
     */
    public static String[] getCommand(String userName, String psw, String ip, String db, String url) {
        String[] cmd = new String[3];
        String os = System.getProperties().getProperty("os.name");
        if (os.startsWith("Win")) {
            cmd[0] = "cmd.exe";
            cmd[1] = "/c";
        } else {
            cmd[0] = "/bin/sh";
            cmd[1] = "-c";
        }
        StringBuilder arg = new StringBuilder();
        arg.append("mysql ");
        arg.append("-h" + ip + " ");
        arg.append("-u" + userName + " ");
        arg.append("-p" + psw + " ");
        arg.append(db + " < ");
        arg.append(url);
        cmd[2] = arg.toString();
        return cmd;
    }

    /**
     * 备份方法
     *
     * @param file
     * @param command
     * @throws
     */
    public static boolean backup(File file, String[] command) {
        log.info("备份文件地址:" + file.getPath());
        for (int i = 0; i < command.length; i++) {
            log.info("备份命令:" + command[i]);
        }
        final Process process;
        try {
            /**
             *  ##############特别关注的点#############
             *
             *  创建一个线程用户获取错误流,并且这个错误流是有返回结果的,所以用的是Callable来实现
             */
            //-u后面是用户名,-p是密码-p后面最好不要有空格,-test是数据库的名字
            Runtime runtime = Runtime.getRuntime();
            log.info("开始备份数据库中...  command: " + JSON.toJSONString(command));
            process = runtime.exec(command);
            log.info("数据库备份结束...");
            // 新增一个线程用于处理错误流,如果有错误就返回true,没有就返回false
            ErrorStreamUtil errThread = new ErrorStreamUtil(process);
            FutureTask<Boolean> result = new FutureTask<Boolean>(errThread);
            new Thread(result).start();
            if (result.get()) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



    /**
     * mysql的还原方法
     *
     * @param command  命令行
     * @param savePath 还原路径
     * @return
     */
    public static boolean recover(String[] command, String savePath) {
        log.info("文件所在位置:" + savePath);
        log.info("cmd命令:" + command[2]);
        Runtime runtime = Runtime.getRuntime();
        try {
            log.info("开始恢复数据库中...  command: " + JSON.toJSONString(command));
            Process process = runtime.exec(command);
            log.info("数据库恢复结束...");
            // 新增一个线程用于处理错误流,如果有错误就返回true,没有就返回false
            ErrorStreamUtil errThread = new ErrorStreamUtil(process);
            FutureTask<Boolean> result = new FutureTask<Boolean>(errThread);
            new Thread(result).start();
            /*// 执行sql文件
            InputStreamRestoreUtil restoreUtil = new InputStreamRestoreUtil(process, new File(savePath));
            Thread thread = new Thread(restoreUtil);
            thread.start();
            thread.join();*/
            if (result.get()) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Mysql恢复出错: {}", e);
            return false;
        }
    }

    /**
     * 删除文件
     *
     * @param file
     */
    public static void deleteFiles(File file) {
        // 判断文件是否存在
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else {
                File[] files = file.listFiles();
                for (File f : files) {
                    deleteFiles(f);
                }
            }
        } else {
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EntyIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值