/**备份数据库**/
File workPath=fetchDBInstallPath();//获取数据库的pg_dump工具的目录
if(workPath==null){
return false;
}
String dbBackupPath=fetchDBBackupPath();
StringBuffer sb=new StringBuffer();
sb.append("cmd /c pg_dump -h 127.0.0.1 -p 5432 -U postgres -F c -b -v -f ").append(dbBackupPath+"\\"+fileName+".backup").append(" dbname");
log.info("**********************************************");
log.info("workPath="+workPath);
log.info("command:"+sb.toString());
log.info("**********************************************");
Process process=Runtime.getRuntime().exec(sb.toString(),null,workPath);
log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "Error"); //线程,用于输出外部程序的输出内容,防止阻塞
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "Output"); //线程,用于输出外部程序的输出内容,防止阻塞
log.info("+++++++++++++++++++++++++++");
errorGobbler.start();
outputGobbler.start();
process.waitFor();
if(process.exitValue()!=0){
return false;
}
从部署机器上的日志来看,运行完成了log.info("+++++++++++++++++++++++++++");
之后,线程没有正确运行,也就是说,应该是外部程序被阻塞了,然后我把命令行中的-v参数去掉,不让外部程序输出内容,可还是会阴塞,具体日志如下:
2012-10-12 10:23:43.551 INFO] **********************************************
2012-10-12 10:23:43.551 INFO] workPath=C:\Program Files\hikvision\CMS\database\bin
2012-10-12 10:23:43.551 INFO] command:cmd /c pg_dump -h 127.0.0.1 -p 5432 -U postgres -F c -b -v -f c:\backup\test.backup 5221cms
2012-10-12 10:23:43.551 INFO] **********************************************
2012-10-12 10:23:43.711 INFO] ~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-10-12 10:23:43.711 INFO] +++++++++++++++++++++++++++
1 个解决方案
#1
在命令前添加一行:
set PGPASSWORD=<password>
set PGPASSWORD=<password>
#1
在命令前添加一行:
set PGPASSWORD=<password>
set PGPASSWORD=<password>