public void backupPGSQL(){
try{
Runtime r =Runtime.getRuntime();
//Path to the place we store our backup
String rutaCT = "C:\\BAKCUPS\\";
//PostgreSQL variables
String IP = "192.168.1.1";
String user = "postgres";
String dbase = "yourDataBase";
String password = "yourPassword";
Process p;
ProcessBuilder pb;
InputStreamReader reader;
BufferedReader buf_reader;
String line;
//We build a string with today's date (This will be the backup's filename)
java.util.TimeZone zonah = java.util.TimeZone.getTimeZone("GMT+1");
java.util.Calendar Calendario = java.util.GregorianCalendar.getInstance( zonah, new java.util.Locale("es"));
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
StringBuffer date = new StringBuffer();
date.append(df.format(Calendario.getTime()));
java.io.File file = new java.io.File(rutaCT);
// We test if the path to our programs exists
if (file.exists()) {
// We then test if the file we're going to generate exist too. If so we will delete it
StringBuffer fechafile = new StringBuffer();
fechafile.append(rutaCT);
fechafile.append(date.toString());
fechafile.append(".backup");
java.io.File ficherofile = new java.io.File(fechafile.toString());
if (ficherofile.exists()) {
ficherofile.delete();
}
r = Runtime.getRuntime();
pb = new ProcessBuilder(rutaCT + "pg_dump.exe", "-f", fechafile.toString(),
"-F", "c", "-Z", "9", "-v", "-o", "-h",IP, "-U", user, dbase);
pb.environment().put("PGPASSWORD", password);
pb.redirectErrorStream(true);
p = pb.start();
try {
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String ll;
while ((ll = br.readLine()) != null) {
System.out.println(ll);
}
} catch (IOException e) {
log("ERROR "+e.getMessage(), e);
}
}
} catch(IOException x) {
System.err.println("Could not invoke browser, command=");
System.err.println("Caught: " + x.getMessage());
}
}