java - 尝试从 LAN 网络上的不同电脑获取数据库备份会生成空的 sql 文件

标签 java mysql database-backups

当我尝试从另一台 PC(称为 pac 1)获取数据库备份时,该 PC 使用 mysqldump 连接到与访问计算机(称为 pac 2)服务器相同的本地网络到本地计算机

数据库位于PC 1上,我想通过执行java程序在PC上生成数据库备份。目前我实现了这一点,但是出现错误,因此创建了一个空的 SQL 文件。

  • userPathForDbBackup 是一个字符串,它通过文件选择器获取用户的路径

  • mySqlPath 是一个变量,它是 pc2 上的文件路径

String mySqlPath="C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump.exe";
String name="name";
String pathFinal=userPathForDbBackup + "\\Backup_" + name + ".sql";
String[] command = {mySqlPath,"mysqldump", "-h"+dbh, "-u "+dbUser, "-p" + dbPass, dbName,">"+pathFinal};
try{
  connection = create_connection.jdbc.getCon();
  String executeCmd = "";
  if(connection!=null){
    executeCmd = "mysqldump -h"+dbh+"-u"+dbUser+" -p"+dbPass+" "+dbName+"file.sql" ;
    System.out.println();
    ProcessBuilder pb = new ProcessBuilder(command);

    Process runtimeProcess =pb.start();
    // Process runtimeProcess =Runtime.getRuntime().exec(command);
    int processComplete = runtimeProcess.waitFor();
    if(processComplete == 0){
      System.out.println("Backup taken successfully");
    } else {
      System.out.println("Could not take mysql backup");
    }
  } else{
    System.out.println("connection not sucess");
  }
}catch (Exception e) {
  e.printStackTrace();
}

这不会引发任何异常,但会在用户选择的目录中创建一个空的 SQL 文件。

最佳答案

 String dbName = "";
            String dbUser = "";
            String dbPass = "";
            String dbh = "";
            String dbport = "";
            Connection connection;


//userPathForDbBackup is user selected path from file chooser


            String pathFinal=userPathForDbBackup + "\\Backup_" + name + ".sql";
            System.out.println(userPathForDbBackup+"before");
            userPathForDbBackup= userPathForDbBackup+"\\Backup_name.sql";
            System.out.println(userPathForDbBackup+"after");

            String folderPath = userPathForDbBackup.substring(0, userPathForDbBackup.lastIndexOf("\\")) ;


            File path =new File(userPathForDbBackup);

            String[] executeCmd = new String[]{"C:/Program Files/MySQL/MySQL Server 5.6/bin/mysqldump.exe", "--user=" + dbUser, "--password=" + dbPass,""+ dbName,"-r"+path};

            try{

                connection = create_connection.jdbc.getCon();

                if(connection!=null){
                    Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
                    int processComplete = runtimeProcess.waitFor();
                    if (processComplete != 0) { //4
                        // something went wrong
                        InputStream errorStream = runtimeProcess.getErrorStream();
                        byte[] buffer = new byte[errorStream.available()];
                        errorStream.read(buffer);

                        System.out.println(new String(buffer));
                    }
                } else{
                    System.out.println("connection not sucess");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }

终于让这段代码工作了

关于java - 尝试从 LAN 网络上的不同电脑获取数据库备份会生成空的 sql 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53171343/

相关文章:

java - 如何强制我的 EAR 文件仅部署在特定服务器上?

java - 使用 nohup standalone full 启动 WildFly

MySQL LEFT JOIN 与 GROUP BY 和 WHERE IN(子查询)

postgresql - 在 postgres 中导出域

mysql - 在 Symfony 应用程序中通过 php 备份 MySQL

java - 如何通过Java代码为mongoDB实现嵌套查询

java - 如何在 Spring Boot 测试中获取正在运行的服务器端口?

python - 我如何将 xml 格式的数据从 flex 传递到 python

mysql - SQL - 无法添加或更新子行 : a foreign key constraint fails - Pivot table

MongoDb 热备份 - 使用 fsyncLock 复制数据/数据库 VS 副本集