KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > backup > PostgreSQLBinaryBackuper


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: c-jdbc@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): Dylan Hansen.
22  */

23
24 package org.objectweb.cjdbc.controller.backup;
25
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.BackupException;
31 import org.objectweb.cjdbc.common.log.Trace;
32 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
33
34 /**
35  * This class defines a Backuper for PostgreSQL databases. This backuper
36  * creates dumps in a binary formatu, using the "--format=c" switch on pg_dump.
37  * <p>
38  * Supported URLs are jdbc:postgresql://host:port/dbname?param1=foo,param2=bar
39  * <p>
40  * Currently the Backuper only takes 1 parameter, the encoding of the database
41  * that is created upon restore. More options can be easily added. This class
42  * makes calls to the pg_dump, createdb, dropdb and pg_restore commands.
43  *
44  * @author <a HREF="mailto:dhansen@h2st.com">Dylan Hansen</a>
45  * @version 1.1
46  */

47 public class PostgreSQLBinaryBackuper extends AbstractPostgreSQLBackuper
48 {
49   // Logger
50
static Trace logger = Trace.getLogger(PostgreSQLBinaryBackuper.class
51                                     .getName());
52
53   /**
54    * Creates a new <code>PostgreSQLBinaryBackuper</code> object
55    *
56    * @author Dylan Hansen
57    */

58   public PostgreSQLBinaryBackuper()
59   {
60   }
61
62   /**
63    * @see org.objectweb.cjdbc.controller.backup.Backuper#getDumpFormat()
64    * @author Dylan Hansen
65    */

66   public String JavaDoc getDumpFormat()
67   {
68     return "PostgreSQL Binary Dump";
69   }
70
71   /**
72    * @see org.objectweb.cjdbc.controller.backup.Backuper#backup(org.objectweb.cjdbc.controller.backend.DatabaseBackend,
73    * java.lang.String, java.lang.String, java.lang.String,
74    * java.lang.String, java.util.ArrayList)
75    * @author Dylan Hansen
76    */

77   public Date JavaDoc backup(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
78       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
79   {
80
81     // Parse the URL for the connection information
82
String JavaDoc url = backend.getURL();
83     String JavaDoc host = getHostFromURL(url);
84     String JavaDoc port = getPortFromURL(url);
85     String JavaDoc dbName = getDatabaseNameFromURL(url);
86
87     if (logger.isDebugEnabled())
88       logger.debug("Backing up database '" + dbName + "' on host '" + host
89           + ":" + port + "'");
90
91     try
92     {
93       // Create the path, if it does not already exist
94
File JavaDoc pathDir = new File JavaDoc(path);
95       if (!pathDir.exists())
96       {
97         pathDir.mkdirs();
98         pathDir.mkdir();
99       }
100
101       String JavaDoc fullPath = getDumpPhysicalPath(path, dumpName);
102
103       // Execute the pg_dump command
104
Runtime JavaDoc runtime = Runtime.getRuntime();
105       String JavaDoc[] cmd = {"pg_dump", "--format=c", "-f", fullPath, "-h", host, "-p", port, "-U",
106           login, dbName};
107
108       Process JavaDoc process = runtime.exec(cmd);
109       process.waitFor();
110       if (process.exitValue() != 0)
111         throw new BackupException(
112             "pg_dump execution did not complete successfully!");
113     }
114     catch (Exception JavaDoc e)
115     {
116       String JavaDoc msg = "Error while performing backup";
117       logger.error(msg, e);
118       throw new BackupException(msg, e);
119     }
120
121     return new Date JavaDoc(System.currentTimeMillis());
122   }
123
124   /**
125    * @see org.objectweb.cjdbc.controller.backup.Backuper#restore(org.objectweb.cjdbc.controller.backend.DatabaseBackend,
126    * java.lang.String, java.lang.String, java.lang.String,
127    * java.lang.String, java.util.ArrayList)
128    * @author Dylan Hansen
129    */

130   public void restore(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
131       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
132   {
133     // Parse the URL for the connection information
134
String JavaDoc url = backend.getURL();
135     String JavaDoc host = getHostFromURL(url);
136     String JavaDoc port = getPortFromURL(url);
137     String JavaDoc dbName = getDatabaseNameFromURL(url);
138
139     if (logger.isDebugEnabled())
140       logger.debug("Restoring database '" + dbName + "' on host '" + host + ":"
141           + port + "'");
142
143     // Check to see if the given path + dumpName exists
144
String JavaDoc fullPath = getDumpPhysicalPath(path, dumpName);
145     File JavaDoc dump = new File JavaDoc(fullPath);
146     if (!dump.exists())
147       throw new BackupException("Backup '" + fullPath + "' does not exist!");
148
149     try
150     {
151       Runtime JavaDoc runtime = Runtime.getRuntime();
152       Process JavaDoc process = null;
153
154       if (logger.isDebugEnabled())
155         logger.debug("Dropping database '" + dbName + "'...");
156
157       // Drop the database if it already exists
158
String JavaDoc[] dropCmd = {"dropdb", "-h", host, "-p", port, "-U", login, dbName};
159       process = runtime.exec(dropCmd);
160       process.waitFor();
161       if (process.exitValue() != 0)
162         throw new BackupException(
163             "dropdb execution did not complete successfully!");
164
165       // Re-create the database, use the specified encoding if provided
166
if (optionsMap.containsKey("encoding"))
167       {
168         String JavaDoc encoding = (String JavaDoc) optionsMap.get("encoding");
169
170         if (logger.isDebugEnabled())
171           logger.debug("Creating databse '" + dbName + "' with encoding '"
172               + encoding + "'");
173
174         String JavaDoc[] createCmd = {"createdb", "-h", host, "-p", port, "-U", login,
175             "--encoding=" + encoding, dbName};
176         process = runtime.exec(createCmd);
177         process.waitFor();
178         if (process.exitValue() != 0)
179           throw new BackupException(
180               "createdb execution did not complete successfully!");
181       }
182       else
183       {
184         if (logger.isDebugEnabled())
185           logger.debug("Creating database '" + dbName + "'");
186
187         String JavaDoc[] createCmd = {"createdb", "-h", host, "-p", port, "-U", login,
188             dbName};
189         process = runtime.exec(createCmd);
190         process.waitFor();
191         if (process.exitValue() != 0)
192           throw new BackupException(
193               "createdb execution did not complete successfully!");
194       }
195
196       if (logger.isDebugEnabled())
197         logger
198             .debug("Rebuilding '" + dbName + "' from dump '" + dumpName + "'");
199
200       // Use the pg_restore command to rebuild the database
201
String JavaDoc[] replayCmd = {"pg_restore", "-h", host, "-p", port, "-U", login, "--format=c", "-d", dbName, fullPath};
202       process = runtime.exec(replayCmd);
203       process.waitFor();
204       if (process.exitValue() != 0)
205         throw new BackupException(
206             "psql execution did not complete successfully!");
207     }
208     catch (Exception JavaDoc e)
209     {
210       String JavaDoc msg = "Error while performing backup";
211       logger.error(msg, e);
212       throw new BackupException(msg, e);
213     }
214   }
215 }
216
Popular Tags