日期:2014-05-16 浏览次数:20586 次
package com.jysd.ms.action; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Date; import java.util.Properties; import com.jysd.ms.exception.BaseException; import com.jysd.ms.util.ReadSystemConfig; import com.jysd.ms.util.TimeCountUtil; import com.opensymphony.xwork2.ActionSupport; public class IEEM060501Action extends ActionSupport { private static final long serialVersionUID = 1L; private Runtime runtime = Runtime.getRuntime(); private Process process = null ; //返回前台数据 private String message; //备份文件地址 private String backupUrl ; //备份文件名 private String dmpName; //备份方式 private int backupMode; //备份类型 private int backupType; private Properties properties = new Properties() ; /** * @方法名称: execute * @描述: 数据备份管理初始化方法 * 创建人: * @return String * @param @return */ public String execute()throws BaseException{ InputStream in = ReadSystemConfig.class.getClassLoader().getResourceAsStream("backupOracle.properties"); try { properties.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } backupUrl = properties.getProperty("backupurl"); return SUCCESS; } /** * @方法名称: backup * @描述: 备份数据库 * 创建人: * @return String * @param @return * @param @throws BaseException */ public String backup()throws BaseException{ //从配置文件中读取数据库信息,并备份数据库 boolean backupFlag = backupOracleDB(getExpString()); if(backupFlag){ message="备份成功!"; }else{ message="备份失败!"; } return "backJson"; } /** * @方法名称: backupOracleDB * @描述: 备份、还原 oracle 数据库的方法 * 创建人: * @return boolean * @param @param cmdStr 备份命令 ( 即导出 ) * @param @return */ public boolean backupOracleDB(String cmdStr) { boolean isSuccess = true; try { process = runtime .exec(cmdStr); String line = null; BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream())); //读取ErrorStream很关键,这个解决了挂起的问题。 while ((line = br.readLine()) != null){ System.out.println(line); } br = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = br.readLine()) != null){ System.out.println(line); } //process.waitFor(); if(process.waitFor()!=0){ isSuccess = false; } }catch (Exception e) { e.printStackTrace(); isSuccess = false; } return isSuccess ; } /** * @方法名称: getLeftMenu * @描述: 获取备份数据库的执行语句,备份文件名称,以当前时间命名 * @创建人: * @return String * @param @throws BaseException */ private String getExpString()throws BaseException{ StringBuffer sbu = new StringBuffer(); try { InputStream in = ReadSystemConfig.class.getClassLoader().getResourceAsStream("backupOracle.properties"); properties.load(in); sbu.append("exp "); sbu.append(properties.getProperty("username")).append("/"); sbu.append(properties.getProperty("password")).append("@"); //sbu.append(properties.getProperty("ipaddress")).append("/"); sbu.append(properties.getProperty("databasename")).append(" file="); sbu.append(properties.getProperty("backupurl")); Date date = new Date(); if(dmpName==null){ dmpName = TimeCountUtil.dateToString(date,"yyyy.MM.dd.HH.mm.ss"); }else{ dmpName += TimeCountUtil.dateToString(date,"yyyy.MM.dd.HH.mm.ss"); } dmpName = dmpName.replace(" ",""); dmpName = dmpName.replace(".dmp",""); sbu.append(dmpName).append(".dmp"); //等于2,仅备份监测数据(实时表、日结表数据) if(backupMode==2) sbu.append(" TABLES=(").append(properties.getProperty("realtable")).a