数据库实现window和Linux备份与恢复

这段Java代码实现了基于mysqldump的数据库备份和恢复功能,根据操作系统(Windows或Linux)执行不同的命令。提供了全库备份和指定表备份的方法,并在执行后输出错误信息。此外,还包括了测试用例进行操作验证。
摘要由CSDN通过智能技术生成

按数据库备份

  /**
     * 备份数据库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);
        //[1]创建备份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);

        Process process = null;
        System.out.println("cmd命令为:"+sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始备份:"+dbName);
        //[2]判断操作系统 windwos与linux使用的语句不一样
        if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
            process = Runtime.getRuntime().exec("cmd /c"+sb.toString());
        }else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
            process = Runtime.getRuntime().exec("/bin/sh -c"+sb.toString());
        }else{
            throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
        }
        System.out.println("备份成功!");

        //[3]输出返回的错误信息
        StringBuffer mes = new StringBuffer();
        String tmp = "";
        BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while((tmp = error.readLine()) != null){
            mes.append(tmp + "\n");
        }
        if(mes != null || !"".equals(mes) ){
            System.out.println("备份成功!==>"+mes.toString());
        }
        error.close();
    }

可选择某一个数据库中的多张表备份

/**
     * 备份数据库db
     * @param root
     * @param pwd
     * @param dbName
     * @param tableName
     * @param backPath
     * @param backName
     * mysqldump  test -u root -p -B --tables moni_yp > D:\db_script.sql 按某个数据库的,多张表,备份
     */
    public static void dbBackUp(String root,String pwd,String dbName,String tableName,String backPath,String backName) throws Exception {
        String pathSql = backPath+backName;
        File fileSql = new File(pathSql);
        //[1]创建备份sql文件
        if (!fileSql.exists()){
            fileSql.createNewFile();
        }
        //mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
        StringBuffer sb = new StringBuffer();
        sb.append("mysqldump");
        sb.append(" "+dbName);
        sb.append(" -h127.0.0.1");
        sb.append(" -u"+root);
        sb.append(" -p"+pwd);
        sb.append(" --tables");
        sb.append(" "+tableName+" >");
        sb.append(pathSql);

        Process process = null;
        System.out.println("cmd命令为:"+sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始备份:"+dbName);
        //[2]判断操作系统 windwos与linux使用的语句不一样
        if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
            process = Runtime.getRuntime().exec("cmd /c"+sb.toString());
        }else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
            process = Runtime.getRuntime().exec("/bin/sh -c"+sb.toString());
        }else{
            throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
        }
        System.out.println("备份成功!");

        //[3]输出返回的错误信息
        StringBuffer mes = new StringBuffer();
        String tmp = "";
        BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while((tmp = error.readLine()) != null){
            mes.append(tmp + "\n");
        }
        if(mes != null || !"".equals(mes) ){
            System.out.println("备份成功!==>"+mes.toString());
        }
        error.close();
    }

按数据库恢复

/**
     * 恢复数据库
     * @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());
            Process process = null;
            //判断操作系统 windwos与linux使用的语句不一样
            if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
                process = Runtime.getRuntime().exec("cmd /c"+sb.toString());
            }else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
                process = Runtime.getRuntime().exec("/bin/sh -c"+sb.toString());
            }else{
                throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
            }
            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();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("还原成功!");
    }

测试

  @Test
    void test01() throws Exception {
        NPtest01.dbBackUp("root","123","test","D:\\","test.sql");
    }
   @Test
    void test02() throws Exception {
       NPtest01.dbRestore("root","123","test","D:\\test.sql");
    }
  @Test
    void test03() throws Exception {
        NPtest01.dbBackUp("root","xk20010515","test","moni_yp","D:\\","test.sql");
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值