views:

84

answers:

1

Hello,

I've seen this problem many places, that is to say the problem with programmatically making backups from PostgreSQL. My solution is to use ExpectJ, but I'm having trouble getting my code to work.

I have the following code:

public class BackupUtility 
{
 public static void main(String[] args)
 {

  try
  {
   ExpectJ exp = new ExpectJ(20);
   Spawn shell = exp.spawn("\"C:\\Program Files\\PostgreSQL\\8.4\\bin\\pg_dump.exe\" " +
                           "-h localhost -U myUserName myDB");
   shell.expect("Password: ");
   shell.send("myPassword");

   System.out.println(shell.getCurrentStandardOutContents());

   shell.expectClose()
  }
  catch (Exception e) 
  {
   e.printStackTrace();
  }   
 }
}

However, on the shell.expect line, it's timing out.

When I run this command from the command prompt, it looks like the following:

C:\Documents and Settings\bobjonesthe3rd>"C:\Program Files\PostgreSQL\8.4\bin\pg_dump" -h localhost -U myUserName myDB
Password:

So, I know it prompts for a password, but ExpectJ isn't receiving the prompt for some reason.

Any help would be much appreciated.

Thanks, Matt

+1  A: 

I'm pretty sure an easier way to do that, without the need to use expect and send, is via a .pgpass file. Also referenced in the pg_dump docs.

rfusca
I don't have access to the actual PostgreSql installation, so using the .pgpass file is not a viable option. The "localhost" host in the post is just an example. PostgreSQL will normally be running on a different machine.
Mattimus Max
Then you complicated things alot. If you connect to a newer version of PG than your pg_dump is, your backup may not act like you expect.
rfusca
Here's our setup: We have a desktop application that uses PostgreSQL as a database server. As a requirement of the installer, the user must have PostgreSQL version 8.4 installed. They give us the server name and port number and we use that information to connect to their server. In our installation, we include a copy of the pg_dump utility and we use it in conjunction with their server information to make a backup. However, all this is really irrelevant. My question is not about how to make a backup, it's about how to property use ExpectJ.
Mattimus Max
True, in regards to you asking about ExpectJ, but .pgpass file does live on the client not the server, so its still a viable option. Best of luck.
rfusca