backup postgresql from java

1

I have a code implemented for backup but when it runs it does not do anything to me and it does not give any errors. I have searched the Internet for other ways to do it but all the examples are similar to the code I use (besides I tried them and they do not work either ):

    Process p;
    ProcessBuilder pb;        
    pb = new ProcessBuilder(
        "C:\Program Files\PostgreSQL\9.3\bin\pg_dump.exe",
        "--host", "localhost",
        "--port", "5432",
        "--username", "postgres",
        "--no-password",
        "--format", "custom",
        "--blobs",
        "--verbose", "--file", "D:\bd.backup", "r4_citmatel_2016");
    p = pb.start();
    
asked by Omar Miranda 19.04.2017 в 22:20
source

2 answers

1

Here's my step by step of how respaldo and restauro my database Postgresql from Java .

import java.io.File;

public class BaseDatos extends Conexionbd{

    private Process proceso;
    private ProcessBuilder constructor;

    private final String host = "localhost";
    private final String puerto="5432";
    private final String usuario = "postgres";
    private final String clave = "root";
    private final String bd="siceo";
    private final String formato="custom";

    public boolean BD_backup(String path){
        boolean hecho=false;
        try{
            File pgdump= new File("C:/Program Files/PostgreSQL/9.4/bin\pg_dump.exe");
            if(pgdump.exists()){
                if(!path.equalsIgnoreCase("")) {
                    constructor = new ProcessBuilder("C:/Program Files/PostgreSQL/9.4/bin\pg_dump.exe", "--verbose", "--format", formato, "-f", path);
                } else {                             
                    constructor = new ProcessBuilder("C:/Program Files/PostgreSQL/9.4/bin\pg_dump.exe", "--verbose", "--inserts", "--column-inserts", "-f", path);
                    System.out.println("ERROR");
                }
                constructor.environment().put("PGHOST", host);
                constructor.environment().put("PGPORT", puerto);
                constructor.environment().put("PGUSER", usuario);
                constructor.environment().put("PGPASSWORD", clave);
                constructor.environment().put("PGDATABASE", bd);
                constructor.redirectErrorStream(true);
                proceso= constructor.start();
                escribirProcess(proceso);
                System.out.println("terminado backup " + path);
                hecho=true;
            }else{
                if(!path.equalsIgnoreCase("")) {
                    constructor = new ProcessBuilder("/opt/PostgreSQL/9.4/bin/pg_dump", "--verbose", "--format", formato, "-f", path);
                } else {                             
                    constructor = new ProcessBuilder("/opt/PostgreSQL/9.4/bin/pg_dump", "--verbose", "--inserts", "--column-inserts", "-f", path);
                    System.out.println("ERROR");
                }
                constructor.environment().put("PGHOST", host);
                constructor.environment().put("PGPORT", puerto);
                constructor.environment().put("PGUSER", usuario);
                constructor.environment().put("PGPASSWORD", clave);
                constructor.environment().put("PGDATABASE", bd);
                constructor.redirectErrorStream(true);
                proceso= constructor.start();
                escribirProcess(proceso);
                System.out.println("terminado backup " + path);
                hecho=true;
            }
        }catch(Exception ex){
            System.err.println(ex.getMessage()+ "Error de backup");
            hecho=false;
        }
        return hecho;
    }


    public boolean restaurarBackup(String path) {
        boolean hecho=false;
        try {
            File pgrestore = new File("C:/Program Files/PostgreSQL/9.4/bin\pg_restore.exe");
            if(pgrestore.exists()){
                constructor = new ProcessBuilder("C:/Program Files/PostgreSQL/9.4/bin\pg_restore.exe", "-i", "-h", host, "-p", puerto, "-U", usuario, "-d", bd, "-v", path);
                constructor.environment().put("PGPASSWORD", clave);
                constructor.redirectErrorStream(true);
                proceso=constructor.start();
                escribirProcess(proceso);
                hecho=true;
            }else{
                constructor = new ProcessBuilder("/opt/PostgreSQL/9.4/bin/pg_restore", "-i", "-h", host, "-p", puerto, "-U", usuario, "-d", bd, "-v", path);
                constructor.environment().put("PGPASSWORD", clave);
                constructor.redirectErrorStream(true);
                proceso=constructor.start();
                escribirProcess(proceso);
                hecho=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            hecho=false;
        }
        return hecho;
    }
}
    
answered by 27.04.2017 / 07:36
source
0

For psql you need an equal sign "=". For what it should be, for example --host = localhost. So you need to pass those arguments as a single string of arguments to ProcessBuilder:

pb = new ProcessBuilder(
        "C:\Program Files\PostgreSQL\9.3\bin\pg_dump.exe",
        "--host=localhost",
        "--port=5432",
        "--username=postgres",
        "--no-password",
        "--format=custom",
        "--blobs",
        "--verbose", "--file=D:\bd.backup", "r4_citmatel_2016");

Capture the error output of the ProcessBuilder using ProcessBuilder.getErrorStream () to see any psql error message.

    
answered by 26.04.2017 в 23:27