JAVA实现Mysql备份与恢复

主要使用JAVA中Runtime.getRuntime().exec()的方法调用dos命令 使用mysqldump工具进行Mysql的备份
直接上代码:
mysql备份命令

 mysqldump -h 127.0.0.1 -uroot -proot mysql user >D:/info/server/var/backupdata/backups.sql
/**
     * 备份mysql数据库
     * @param username 账号
     * @param pwd 密码
     * @param url 地址
     * @param path 路径
     * @param tableName 数据库名 表名
     * @throws Exception
     */
    public static void dbBackUpMysql(String username,String pwd,String url,String path,String tableName) throws Exception {
        //mysqldump -h 127.0.0.1 -uroot -proot mysql user >D:/info/server/var/backupdata/backups.sql
        String dbName = "mysql";
        dbName += " "+tableName;
        String pathSql = path+tableName+".sql";
        File fileSql = new File(pathSql);
        File filePath = new File(path);
        //创建备份sql文件
        if (!filePath.exists()){
            filePath.mkdirs();
        }
        if (!fileSql.exists()){
            fileSql.createNewFile();
        }
        //mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
        StringBuffer sb = new StringBuffer();
        sb.append("mysqldump");
        sb.append(" -h"+url);
        sb.append(" -u"+username);
        sb.append(" -p"+pwd);
        sb.append(" "+dbName+" >");
        sb.append(pathSql);
        System.out.println("cmd命令为:"+sb.toString());
        System.out.println("开始备份:"+dbName);
        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("暂不支持该操作系统,进行数据库备份或还原!");
        }
        //设置超时一分钟
        process.waitFor(60000, TimeUnit.MILLISECONDS);
        //输出返回的错误信息
        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();
    }

mysql执行sql命令

mysql -h127.0.0.1 -uroot -proot mysql < /home/back.sql
/**
 * 数据库还原
 * @param username 账号
 * @param pwd 密码
 * @param url 地址
 * @param path 文件存放路径
 * @param tableName 数据库名 表名
 * @throws Exception
 */
public static void dbRestoreMysql(String username,String pwd,String url,String path,String tableName) throws Exception{
    //mysql -hlocalhost -uroot -proot db < /home/back.sql
    String dbName = "mysql";
    StringBuilder sb = new StringBuilder();
    sb.append("mysql");
    sb.append(" -h"+url);
    sb.append(" -u"+username);
    sb.append(" -p"+pwd);
    sb.append(" "+dbName+" <");
    sb.append(path+tableName+".sql");
    System.out.println("cmd命令为:"+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("暂不支持该操作系统,进行数据库备份或还原!");
    }
    System.out.println("开始还原数据");
    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();
    System.out.println("还原成功!");
}
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值