linux与win10中java操作mysql备份还原

总结:区别有两点,即在于路径和 mysql的执行命令:
1、win10 路径是这样:“E:/hbsqlBack/” ,linux是这样:"/usr/local/speedchina/hbsqlBack/"
在win10中,把数据库原始文件放在 “src/main/resources/sql/db.sql"下,后台代码打包成jar后是可以访问的,但是在linux上不能这么做,得这样指定具体的路径 :”/usr/local/speedchina/visualizationSystem/db.sql"
2、
在win10 下,.exec()方法里这样写 : .exec(“cmd /c”+sb.toString())

// win10 环境
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c"+sb.toString());

在linux 下,.exec()方法里这样写 : .exec({ “/bin/sh”, “-c”, sb.toString() })

// linux 环境
String[] command = { "/bin/sh", "-c", sb.toString() };
Process ps = Runtime.getRuntime().exec(command);

win10 环境 备份操作

 private final String DB_NAME = "test_db";
 @PostMapping("/backDB")
    public Map backDB() {
        return mysqlBackService.backDB();
    }
  public Map backDB() {
        HashMap<Object, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        //获取当前时间用作数据库悲愤sql文件的名称
        String backName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".sql";
        try {
            String s = BackDBUtiles.dbBackUp(backDBMapper, "127.0.0.1", "root", "123456", DB_NAME, "E:/hbsqlBack/", backName);
            resultMap.put("msg", s);
        } catch (Exception e) {
            resultMap.put("msg", "数据备份失败!");
            e.printStackTrace();
        }
        return resultMap;
    }



    public static String dbBackUp(BackDBMapper backDBMapperStatic, String host,String root,String pwd,String dbName,String backPath,String backName) throws Exception {

            String pathSql = backPath+backName;
            File fileSql = new File(pathSql);
            File fileDir = new File(backPath);

            //如果文件夹不存在
            if(!fileDir.exists()){
                //创建文件夹
                fileDir.mkdir();
            }
            //创建备份sql文件
            if (!fileSql.exists()){
                fileSql.createNewFile();
            }
            //mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
            StringBuffer sb = new StringBuffer();
            sb.append("mysqldump");
            sb.append(" -h" + host);
            sb.append(" -u"+ root);
            sb.append(" -p"+ pwd);
            sb.append(" "+dbName+" >");
            sb.append(pathSql);
            System.out.println("cmd命令为:"+sb.toString());
            // win10 环境
            Runtime runtime = Runtime.getRuntime();
            System.out.println("开始备份:"+dbName);
            Process process = runtime.exec("cmd /c"+sb.toString());

            //文件路径添加到数据库
            HashMap<Object, Object> map = new HashMap<>();
            map.put("backSqlPath",pathSql);
            if (backDBMapperStatic.insertBack(map)) {
                System.out.println("备份成功!");
                return "备份成功!";
            }
            return "备份失败";
        }

linux 环境 备份操作

 private final String DB_NAME = "test_db";
 @PostMapping("/backDB")
    public Map backDB() {
        return mysqlBackService.backDB();
    }

  public Map backDB() {
        HashMap<Object, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        //获取当前时间用作数据库备份sql文件的名称
        String backName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".sql";
        try {
            String s = BackDBUtiles.dbBackUp(backDBMapper, "192.168.12.13", "root", "root", DB_NAME, "/usr/local/test/sqlBack/", backName);
       
            resultMap.put("msg", s);
        } catch (Exception e) {
            resultMap.put("msg", "数据备份失败!");
            e.printStackTrace();
        }
        return resultMap;
    }


   public static String dbBackUp(BackDBMapper backDBMapperStatic, String host,String root,String pwd,String dbName,String backPath,String backName) throws Exception {
            String pathSql = backPath+backName;
            File fileSql = new File(pathSql);
            File fileDir = new File(backPath);
            //如果文件夹不存在
            if(!fileDir.exists()){
                //创建文件夹
                fileDir.mkdir();
            }
            //创建备份sql文件
            if (!fileSql.exists()){
                fileSql.createNewFile();
            }
            //mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
            StringBuffer sb = new StringBuffer();
            sb.append("mysqldump");
            sb.append(" -h" + host);
            sb.append(" -u"+ root);
            sb.append(" -p"+ pwd);
            sb.append(" "+dbName+" >");
            sb.append(pathSql);
            System.out.println("cmd命令为:"+sb.toString());
        
            // linux 环境
             System.out.println("开始备份:"+dbName);
            String[] command = { "/bin/sh", "-c", sb.toString() };
             Process ps = Runtime.getRuntime().exec(command);

            
            //文件路径添加到数据库
            HashMap<Object, Object> map = new HashMap<>();
            map.put("backSqlPath",pathSql);
            if (backDBMapperStatic.insertBack(map)) {
                System.out.println("备份成功!");
                return "备份成功!";
            }
            return "备份失败";
        }

win10环境 还原操作

 @PostMapping("/initialization")
    public Map initialization() {
        return mysqlBackService.initialization();
    }
    public Map initialization() {
        HashMap<Object, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        //恢复到初始得数据状态
        String s = BackDBUtiles.dbRestore("127.0.0.1", "root", "123456", DB_NAME, "src/main/resources/sql/db.sql");
        resultMap.put("msg", s);
        return resultMap;
    }
   public static String dbRestore(String host,String root,String pwd,String dbName,String filePath){
            //验证文件名是否是sql文件
            if (filePath.length() < 3) {
                return "请使用正确的.sql文件";
            }
            if (!"sql".equals(filePath.substring(filePath.length() - 3,filePath.length()))){
                return "请使用正确的.sql文件";
            }
            StringBuilder sb = new StringBuilder();
            sb.append("mysql");
            sb.append(" -h"+ host);
            sb.append(" -u"+root);
            sb.append(" -p"+pwd);
            sb.append(" "+dbName+" <");
            sb.append(filePath);
            System.out.println("cmd命令为:"+sb.toString());
            // win10 环境 ---------------------
            Runtime runtime = Runtime.getRuntime();
            System.out.println("开始还原数据");
            try {
                // win10 环境   ---------------------
                Process process = runtime.exec("cmd /c"+sb.toString());
              
                System.out.println("输出输出输出---    Runtime.getRuntime().exec(command)");
                InputStream is = process.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is,"utf8"));
                String line = null;
                while ((line=bf.readLine())!=null){
                    System.out.println(line);
                    System.out.println("输出输出输出---    line=bf.readLine())");
                }
                is.close();
                bf.close();
                System.out.println("输出输出输出---    bf.close();");
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("还原成功!");
            return "还原成功!";
        }

linux环境 还原操作

  @PostMapping("/initialization")
    public Map initialization() {
        return mysqlBackService.initialization();
    }
  public Map initialization() {
        HashMap<Object, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        //恢复到初始得数据状态
        String s = BackDBUtiles.dbRestore("192.168.12.13", "root", "root", DB_NAME, "/usr/local/test/sqlBack/db.sql");
     
        resultMap.put("msg", s);
        return resultMap;
    }
  public static String dbRestore(String host,String root,String pwd,String dbName,String filePath){
            //验证文件名是否是sql文件
            if (filePath.length() < 3) {
                return "请使用正确的.sql文件";
            }
            if (!"sql".equals(filePath.substring(filePath.length() - 3,filePath.length()))){
                return "请使用正确的.sql文件";
            }
            StringBuilder sb = new StringBuilder();
            sb.append("mysql");
            sb.append(" -h"+ host);
            sb.append(" -u"+root);
            sb.append(" -p"+pwd);
            sb.append(" "+dbName+" <");
            sb.append(filePath);
            System.out.println("cmd命令为:"+sb.toString());
   
            // linux 环境  ---------------------
            String[] command = { "/bin/sh", "-c", sb.toString() };
            System.out.println("开始还原数据");
            try {
                // linux 环境 ---------------------
                Process process = Runtime.getRuntime().exec(command);
                System.out.println("输出输出输出---    Runtime.getRuntime().exec(command)");
                InputStream is = process.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is,"utf8"));
                String line = null;
                while ((line=bf.readLine())!=null){
                    System.out.println(line);
                    System.out.println("输出输出输出---    line=bf.readLine())");
                }
                is.close();
                bf.close();
                System.out.println("输出输出输出---    bf.close();");
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("还原成功!");
            return "还原成功!";
        }

总结:区别有两点,即在于路径和 mysql的执行命令:
1、win10 路径是这样:“E:/hbsqlBack/”
linux是这样:"/usr/local/speedchina/hbsqlBack/"
在win10中,把数据库原始文件放在 “src/main/resources/sql/db.sql"下,后台代码打包成jar后是可以访问的,但是在linux上不能这么做,得这样指定具体的路径 :”/usr/local/speedchina/visualizationSystem/db.sql"
2、
在win10 下,.exec()方法里这样写 : .exec(“cmd /c”+sb.toString())

// win10 环境
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c"+sb.toString());

在linux 下,.exec()方法里这样写 : .exec({ “/bin/sh”, “-c”, sb.toString() })

// linux 环境
String[] command = { "/bin/sh", "-c", sb.toString() };
Process ps = Runtime.getRuntime().exec(command);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值