故障恢复关键步骤与灾备方案设计 7月10日 星期四 15:00 云祺视频号准时直播
云祺直播二维码
扫码预约直播,观看精彩内容! 扫码预约直播,观看精彩内容!
关闭按钮
云祺Logo 云祺Logo
关于我们

技术分享

技术分享 J2EE下的oracle数据库备份方法

J2EE下的oracle数据库备份方法

2021-06-01

 
1./**


 * @Description: 实现数据库的备份


* @sinceJDK1.5


 * @date (开发日期):
 * @注意事项:数据库备份文件放在服务器上,路径在Constant里设置,如希望正确使用,需要导入相关jar


 
    public class BackUpDataBaseAction extends Action {


 

    static Logger logger = Logger.getLogger(BackUpDataBaseAction.class);


 

    public ActionForward execute(ActionMapping actionMapping,


           ActionForm actionForm, HttpServletRequest httpServletRequest,


           HttpServletResponse httpServletResponse) {


 

       Calendar cDay = Calendar.getInstance();


       SimpleDateFormat myf = new SimpleDateFormat("yyyyMMddHHmmss");


       String day = myf.format(cDay.getTime());


       // 获得Constant中定义的数据库备份文件存放路径,格式如:


        // public static final String DataBaseBackUpPath = "D:\\sOADB\\";


String outputFile = Constant.DataBaseBackUpPath;


       // 如果服务器端硬盘上不存在outputFile目录,则创建这个目录


       File mydir = new File(outputFile);


       if (!(mydir.exists())) {


           mydir.mkdir();


       }


 

       // 备份文件名


       String fileName = "soa" + day + ".dmp";


       // 将备份好的数据库文件放在服务器端的outputFile目录下


      


       // 得到数据库的连接信息


 

        // 得到物理路径,取得applicationContext.xml中数据库定义,


//url,usrname,passwordO/R映射文件等内容


       ServletContext m_application = this.getServlet().getServletContext();


       String path = m_application.getRealPath("/");


       String filename = path + "WEB-INF\\applicationContext.xml";


       WriteXMLWithDom wxf = new WriteXMLWithDom();


       String[] str = wxf.GetOralAddrWithDom(filename);


       if (str[0].equalsIgnoreCase("")) {


           return actionMapping.findForward("failure");


       }


       // str[1] 用户名  ,[2] 用户密码 ,


       // 服务器地址,[0]


       String command = "exp  "+str[1]+"/"+str[2]+"@"+str[0].substring(str[0].lastIndexOf(':')+1)+" file=" + outputFile+ fileName;


       Runtime rt = Runtime.getRuntime();


       // Process process=null;


       try {


 

           // process=rt.exec(command);


           rt.exec(command);


           // process.waitFor();


           // 假等待一下,目的是让其能在文件列表中列出,可以process.destroy()得到


           Thread.sleep(5000);


       } catch (Exception e) {


             //捕捉异常


           String errorSORT = Constant.errorBeifen;


           httpServletRequest.setAttribute("ERRORSORT", errorSORT);


          


           String error = "数据库备份操作失败,请稍候再试! ";


           httpServletRequest.setAttribute("PUBLICINFERROR", error);


           return actionMapping.findForward("warning");


       }


       // p.destroy();


       logger.warn(StringUtil.getLogString(((UserInfo) httpServletRequest


              .getSession().getAttribute(Constant.USER)).getUserAccount(),


              "执行了数据库备份操作"));


 

       return actionMapping.findForward("success");


    }


 

}


 2.获取application中关于数据库定义的信息,包括用户名,密码,url,.hbm.xml文件等。

package com.gao.sys;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class WriteXMLWithDom {
 public WriteXMLWithDom() {
 }

 public int CallWriteXMLWithDom(String fname, String tablename) {
  int returnValue = 0;
  try {

   File file = new File(fname);
   BufferedInputStream bufferedInputStream = new BufferedInputStream(
     new FileInputStream(file));
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder parser = factory.newDocumentBuilder();

   InputStream instr = new FileInputStream(fname);

   Document doc = parser.parse(instr);
   // 根节点
   NodeList nodeList = doc.getElementsByTagName("beans");
   for (int b = 0; b < nodeList.getLength(); b++) {
    Node book = nodeList.item(b);
    for (Node bean = book.getFirstChild(); bean != null; bean = bean
      .getNextSibling()) {
     // bean节点
     if (bean.getNodeName().equals("bean")) {

      for (int i = 0; i < bean.getChildNodes().getLength(); i++) {
       // property节点
       Node pro = bean.getChildNodes().item(i);

       if (pro.getAttributes() != null
         && pro.getAttributes().getNamedItem("name") != null) {
        if (pro.getAttributes().getNamedItem("name")
          .getNodeValue().equals(
            "mappingResources")) {
         // list节点
         Node listn = pro.getFirstChild()
           .getNextSibling();

         Element elistn = (Element) listn;
         Element newvalue = doc
           .createElement("value");
         Text newtest = doc
           .createTextNode("com/gao/data/bean/"
             + tablename + ".hbm.xml");
         newvalue.appendChild(newtest);
         elistn.appendChild(newvalue);

        }
  

   // }
   FileOutputStream outStream = new FileOutputStream(fname);
   OutputStreamWriter outWriter = new OutputStreamWriter(outStream,
     "UTF-8");

   try {
    // Prepare the DOM document for writing
    Source source = new DOMSource(doc);

    // Prepare the output file
    Result result = new StreamResult(outWriter);

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance()
      .newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    // xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"dtd/spring-beans.dtd");
    // xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"-//SPRING//DTD
    // BEAN//EN");
    xformer.transform(source, result);

   } catch (TransformerConfigurationException e) {
    e.printStackTrace();
   } catch (TransformerException e) {
    e.printStackTrace();
   }

   try {
    outWriter.close();
    outStream.close();
    returnValue = 1;
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   } finally {
    return returnValue;
   }
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   return returnValue;
  }
 }

 public int DeleteNodeFromXMLWithDom(String fname, String tablename) {
  int returnValue = 0;
  try {

   File file = new File(fname);
   BufferedInputStream bufferedInputStream = new BufferedInputStream(
     new FileInputStream(file));
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder parser = factory.newDocumentBuilder();

   InputStream instr = new FileInputStream(fname);

   Document doc = parser.parse(instr);
   // 根节点
   NodeList nodeList = doc.getElementsByTagName("beans");
   for (int b = 0; b < nodeList.getLength(); b++) {
    Node book = nodeList.item(b);
    for (Node bean = book.getFirstChild(); bean != null; bean = bean
      .getNextSibling()) {
     // bean节点
     if (bean.getNodeName().equals("bean")) {

      for (int i = 0; i < bean.getChildNodes().getLength(); i++) {
       // property节点
       Node pro = bean.getChildNodes().item(i);

       if (pro.getAttributes() != null
         && pro.getAttributes().getNamedItem("name") != null) {
        if (pro.getAttributes().getNamedItem("name")
          .getNodeValue().equals(
            "mappingResources")) {
         // list节点
         Node listn = pro.getFirstChild()
           .getNextSibling();

         Element elistn = (Element) listn;
         

         Node valuen = elistn.getFirstChild();
         
         int k = valuen.getChildNodes().getLength();
         for(int j=0;j<elistn.getChildNodes().getLength()-1;j++){
          valuen=valuen.getNextSibling();
          if(valuen.getTextContent().contains(
            tablename)){
           listn.removeChild(valuen);
           break;
          }
  

   // }
   FileOutputStream outStream = new FileOutputStream(fname);
   OutputStreamWriter outWriter = new OutputStreamWriter(outStream,
     "UTF-8");

   try {
    // Prepare the DOM document for writing
    Source source = new DOMSource(doc);

    // Prepare the output file
    Result result = new StreamResult(outWriter);

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance()
      .newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    xformer.transform(source, result);

   } catch (TransformerConfigurationException e) {
    e.printStackTrace();
   } catch (TransformerException e) {
    e.printStackTrace();
   }

   try {
    outWriter.close();
    outStream.close();
    returnValue = 1;
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   } finally {
    return returnValue;
   }
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   return returnValue;
  }
 }

 public String[] GetOralAddrWithDom(String fname) {
  String[] str = { "", "", "" };
  try {

   File file = new File(fname);
   BufferedInputStream bufferedInputStream = new BufferedInputStream(
     new FileInputStream(file));
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder parser = factory.newDocumentBuilder();

   InputStream instr = new FileInputStream(fname);

   Document doc = parser.parse(instr);
   // 根节点
   NodeList nodeList = doc.getElementsByTagName("beans");
   for (int b = 0; b < nodeList.getLength(); b++) {
    Node book = nodeList.item(b);
    for (Node bean = book.getFirstChild(); bean != null; bean = bean
      .getNextSibling()) {
     // bean节点
     if (bean.getNodeName().equals("bean")
       && bean.getAttributes().getNamedItem("id")
         .getNodeValue().equals("DataSource")) {

      for (int i = 0; i < bean.getChildNodes().getLength(); i++) {
       // property节点
       Node pro = bean.getChildNodes().item(i);

       if (pro.getAttributes() != null
         && pro.getAttributes().getNamedItem("name") != null) {
        if (pro.getAttributes().getNamedItem("name")
          .getNodeValue().equals("url")) {
         str[0] = pro.getAttributes().getNamedItem(
           "value").getNodeValue();

        }
        if (pro.getAttributes().getNamedItem("name")
          .getNodeValue().equals("username")) {
         str[1] = pro.getAttributes().getNamedItem(
           "value").getNodeValue();
        }
        if (pro.getAttributes().getNamedItem("name")
          .getNodeValue().equals("password")) {
         str[2] = pro.getAttributes().getNamedItem(
           "value").getNodeValue();

  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   return str;


 
云祺备份软件,云祺容灾备份系统,虚拟机备份,数据库备份,文件备份,实时备份,勒索软件,美国,图书馆
  • 标签:
  • 容灾备份

您可能感兴趣的新闻 换一批

现在下载,可享30天免费试用

立即下载

请添加好友为您提供支持
jia7jia_7

微信售后服务二维码

请拨打电话
为您提供支持

400-9955-698