zoukankan      html  css  js  c++  java
  • java程序中备份Mysql数据库

    1.得到系统的类型.不同的系统有不同的文件分割符号.

    public static boolean isLinux(){
         String OS = System.getProperty("os.name").toLowerCase();
            if (OS.indexOf("windows") > -1) {
             return false;
            }
            else{
             return true;
            }
        } 

    2.根据不同的系统生成不同的命令:

    //表名

     String dbs = "ACTION DDINFO DEVICE DEVICEALARM DEVICEINFO DEVICELOG ";

    List command=new ArrayList();

     if (isLinux) {
                    command.add(exec_path+ "mysqldump");
                    command.add("--opt");
                    command.add("--user=root");
                    command.add("--lock-all-tables=true");
                    command.add("--result-file=" + dumpedFilepath+File.separator+SERVER_BACKUP_SQL_FILE);
                    command.add("orca");
                    String[] str=dbs.split(" ");
                    for(int i=0;i<str.length;i++)
                    command.add(str[i]);  
                }
                else {
                    command.add("\""+exec_path+ "mysqldump\"");// is this work?
                    command.add("--opt");
                    command.add("--user=root");
                    command.add("--lock-all-tables=true");
                    command.add("--result-file=" + dumpedFilepath+File.separator+SERVER_BACKUP_SQL_FILE);
                    command.add("orca");
                    String[] str=dbs.split(" ");
                    for(int i=0;i<str.length;i++)
                    command.add(str[i]);

    3.运行命令

    得到Runtime

    private Runtime cmd = Runtime.getRuntime();

    Process p = cmd.exec((String[])command.toArray(new String[0]),null);
             String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(p.getErrorStream()));
                while ( (line = br.readLine()) != null)
                    log.error("<<<<<<<" + line);

    Process 是为了显示错误信息

  • 相关阅读:
    hibernate篇章六--demo(Hibernate之第1解之-hibernate_demo_1)
    hibernate篇章六--demo(0.准备工作)
    博客装扮3-博客园界面装扮优化教程
    java学习笔记--1_常见输入输出语句熟悉篇章
    矩阵相乘问题
    Algorithm
    hibernate篇章五--Hibernage工作原理
    hibernate篇章四-- Hibernate配置文件中hiberante.hbm2ddl.auto四个参数的配置
    hibernate篇章三-- hibernate配置文件hibernate.cfg.xml的详细解释
    hibernate篇章二--成就搭建hibernate框架
  • 原文地址:https://www.cnblogs.com/huqingyu/p/1381321.html
Copyright © 2011-2022 走看看