Problem when creating MySQL backup from Java

0

I have a problem which I can not think about anymore because I do not know what is happening, the following happens:

I have a database which is already in use so far it has 147 KB between the structure and the data. Well, if I use the console to do the Backup in this way:

<RUTA_DE_MYSQL>\mysqldump --opt --password=<CONTRASEÑA> --user=root <BASE_DE_DATOS> > <RUTA_DE_DESTINO>\Backup.sql

and it works perfect, that is a .sql with all the data of 145 KB, now, in Java I do this:

    private static void backup() {
    //Carpeta que contendra el Backup:
    File carpeta = new File(<RUTA_CONTENEDORA_BACKUP>);
    carpeta.mkdirs();
    //*
    //Creacion del .sql
    Runtime runtime = Runtime.getRuntime();
    File backup = new File(<RUTA_CONTENEDORA_BACKUP> + "\Backup.sql");
    try {
        InputStreamReader isr;
        BufferedReader br;
        FileWriter fileWriterGeneral = new FileWriter(backup);
        Process child = runtime.exec("<RUTA_DE_MYSQL>\mysqldump  --opt --user=root --password=<CONTRASEÑA>--databases <CONTRASEÑA> -R");
        isr = new InputStreamReader(child.getInputStream());
        br = new BufferedReader(isr);
        //*
        //Pasar todo al .sql
        String line;
        while ((line = br.readLine()) != null) {
            fileWriterGeneral.write(line + " \n");
        }
        isr.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Well at the time of compiling and executing the program it happens that the result is a .sql of 144 KB, after noticing that when I did the Restore in the database from that .sql I got 1 error. When verifying that .sql I saw that in the end, the file is cut, I explain to what I mean by "short":

When I make a backup the .sql that is generated has as last line this:

-- Dump completed on <FECHA_DEL_BACKUP> <HORA_DEL_BACKUP>

but in the .sql that I generate from my Java code that is not there but it is interrupted from a certain point like that without further.

What he tried was the following:

  

I thought the problem was here "(line = br.readLine ())", that at the moment that the "line" variable was passed what "br.readLine ()" had was not a layer to store all the line and that's why it was cut, then try this:

    //Pasar todo al .sql
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);  
        fileWriterGeneral.write(line + " \n");
    }

Try to see the content of "line" with the System.out.println () to see if the content was cut at some point, but in the console everything is perfect in the console I see ALL the backup line by line and it actually comes to an end.

It's a big problem because if it "cuts" in the middle of the dump of a table. no record is passed to the database, as is the case of a table in which it is now stopped by register 245 and therefore no record is passed to the table and remains empty PDT: first of all verify that record and it has nothing to do with everything, it is not even the problem because while trying to fix it the order of the tables changed in the .sql and it stopped in another side.

I hope you can help me because I really do not understand what happens ... thanks in advance ..

    
asked by ミケル アンヘル 17.03.2017 в 16:54
source

0 answers