Java web程序中备份oracle数据库
1.生成备份文件:
public ActionForward createDmp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String fileName = "data_oracle" + sdf.format(c.getTime());
String dmpFolder = request.getSession().getServletContext()
.getRealPath("")
+ "\\datas\\dmp\\";
String logFolder = request.getSession().getServletContext()
.getRealPath("")
+ "\\datas\\log\\";
String cmd = "exp username/password@orcl file='" + dmpFolder + fileName
+ ".dmp' log='" + logFolder + fileName + ".log' owner=username";
Runtime.getRuntime().exec(cmd);
Thread.sleep(100 * 1000);
request.setAttribute("msg", "备份成功,文件生成会有延迟,请稍等");
return mapping.findForward("dmpCreated");
}
2.查询备份文件:
public ActionForward findDmp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String dmpFolder = request.getSession().getServletContext()
.getRealPath("")
+ "\\datas\\dmp\\";
File folder = new File(dmpFolder);
File[] files = folder.listFiles();
List<String[]> fileList = new ArrayList<String[]>();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().indexOf(".dmp") > 0) {
String[] one = new String[2];
one[0] = files[i].getName();
one[1] = files[i].length() / 1000 + "K";
fileList.add(one);
}
}
request.setAttribute("fileList", fileList);
return mapping.findForward("dmpList");
}
3.删除备份文件:
public ActionForward deleteDmp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String dmpFolder = request.getSession().getServletContext()
.getRealPath("")
+ "\\datas\\dmp\\";
File folder = new File(dmpFolder);
File[] files = folder.listFiles();
String fileName = request.getParameter("dmpName");
for (int i = 0; i < files.length; i++) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
request.setAttribute("msg", "删除成功");
break;
}
}
return mapping.findForward("dmpList");
}
|