KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > backupTool > dbDump > PostgresqlDbDumper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.outerj.daisy.backupTool.dbDump;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 public class PostgresqlDbDumper extends AbstractDbDumper {
22     private static String JavaDoc dumpExecutableName = "pg_dump";
23
24     private static String JavaDoc restoreExecutableName = "psql";
25
26     public PostgresqlDbDumper(String JavaDoc dbName, String JavaDoc host, Integer JavaDoc port, String JavaDoc password, String JavaDoc username) {
27         super(dbName, host, port, password, username);
28     }
29
30     public void dump(File JavaDoc dumpFile) throws Exception JavaDoc {
31         try {
32             System.out.println("Dumping database : " + dbName);
33             String JavaDoc command = dumpExecutableName + " -U " + this.username + " --host=" + this.host + " --file=" + dumpFile.getPath()
34                     + (port.intValue() > 0 ? " --port=" + port : "") + " --create --oids --no-owner " + this.dbName;
35
36             Process JavaDoc dumpProcess = Runtime.getRuntime().exec(command);
37             handleRuntimeProcess(dumpProcess, null, System.out);
38         } catch (IOException JavaDoc e) {
39             throw new Exception JavaDoc("The " + dumpExecutableName + " command was not found. Try putting this executable in your environments path variable.");
40         }
41     }
42
43     public void restore(File JavaDoc dumpFile) throws Exception JavaDoc {
44         try {
45             System.out.println("Restoring database : " + dbName + "\nYou may be prompted for the password of the following database user twice : " + username);
46             String JavaDoc dropCommand = "dropdb -U " + username + " --host " + host + (port.intValue() > 0 ? " --port=" + port : "") + " --quiet " + dbName;
47             Process JavaDoc dropProcess = Runtime.getRuntime().exec(dropCommand);
48             handleRuntimeProcess(dropProcess, null, System.out);
49
50             String JavaDoc command = restoreExecutableName + " --username " + this.username + " --host " + this.host + " --file " + dumpFile.getPath()
51                     + (port.intValue() > 0 ? " --port " + port : "") + " --quiet template1";
52             Process JavaDoc restoreProcess = Runtime.getRuntime().exec(command);
53             handleRuntimeProcess(restoreProcess, null, System.out);
54         } catch (IOException JavaDoc e) {
55             throw new Exception JavaDoc("The " + restoreExecutableName + " command was not found. Try putting this executable in your environments path variable.");
56         }
57     }
58 }
Popular Tags