如何通过 JDBC 从 Postgresql-DB 进行备份?
在我们的应用程序中,我们实现了从代码中触发的自动数据库迁移。现在我们要在进行任何迁移之前备份现有数据库。
谁能解释如何在 Java 代码中通过 JDBC 对 Postgresql-DB 进行完整备份?
更新:它不能通过 JDBC 工作。
这里有一些响应 弗兰克·海肯斯:
final List<String> baseCmds = new ArrayList<String>();
baseCmds.add("/usr/bin/pg_dump");
baseCmds.add("-h");
baseCmds.add("hostname");
baseCmds.add("-p");
baseCmds.add("5432");
baseCmds.add("-U");
baseCmds.add("username");
baseCmds.add("-b");
baseCmds.add("-v");
baseCmds.add("-f");
baseCmds.add("/path/to/backup.sql");
baseCmds.add("dbName");
final ProcessBuilder pb = new ProcessBuilder(baseCmds);
// Set the password
final Map<String, String> env = pb.environment();
env.put("PGPASSWORD", "password");
try {
final Process process = pb.start();
final BufferedReader r = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line = r.readLine();
while (line != null) {
System.err.println(line);
line = r.readLine();
}
r.close();
final int dcertExitCode = process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
In our application, we've implemented an automatic DB migration triggered from within our code. Now we want to backup the existing DB before doing any migration.
Can anyone explain how to do a full backup of a Postgresql-DB via JDBC from within Java code?
Update: it doesn't work via JDBC.
Here some working code to the response of Frank Heikens:
final List<String> baseCmds = new ArrayList<String>();
baseCmds.add("/usr/bin/pg_dump");
baseCmds.add("-h");
baseCmds.add("hostname");
baseCmds.add("-p");
baseCmds.add("5432");
baseCmds.add("-U");
baseCmds.add("username");
baseCmds.add("-b");
baseCmds.add("-v");
baseCmds.add("-f");
baseCmds.add("/path/to/backup.sql");
baseCmds.add("dbName");
final ProcessBuilder pb = new ProcessBuilder(baseCmds);
// Set the password
final Map<String, String> env = pb.environment();
env.put("PGPASSWORD", "password");
try {
final Process process = pb.start();
final BufferedReader r = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line = r.readLine();
while (line != null) {
System.err.println(line);
line = r.readLine();
}
r.close();
final int dcertExitCode = process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用 pg_dump ?
Why don't you use pg_dump?
Postgresql JDBC 库现在支持批量 COPY 操作。请参阅 http://jdbc.postgresql.org/ Documentation/publicapi/index.html?org/postgresql/copy/CopyManager.html
要备份数据库,您需要将
CopyOut
从数据库复制到流,然后反转使用CopyIn
恢复的过程。The Postgresql JDBC library now supports the bulk
COPY
operations. See http://jdbc.postgresql.org/documentation/publicapi/index.html?org/postgresql/copy/CopyManager.htmlTo back up a database, you'll want to
CopyOut
from the db to a stream, then reverse the process to restore usingCopyIn
.我使用 DbUnit 从我的 java 应用程序中备份数据库:
I use DbUnit for backup of a database from within my java application: