數據庫備份

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  Date now = new Date();
  String fileName = sessionId + sdf.format(now) + ".sql";  //設置備份文件的名稱
  log.debug("fileName: " + fileName);
  //Util是我自己寫的一個類,用來讀取配置文件configure.properties

   批處理文件地址

  http://dl2.csdn.net/down4/20070911/11164635732.bat
  String backupFilePath = Util.getProperty("backupFilePath"); //從配置文件中讀取備份文件將要保存在哪個目錄
  String mysqlUserName = Util.getProperty("mysql_username");  //數據庫帳戶
  String mysqlPassword = Util.getProperty("mysql_password");  //數據庫密碼
  String host = Util.getProperty("mysql_host");               //數據庫IP地址
  String dateHandleDir = Util.getProperty("dateHandleDir");   //批處理文件路徑
  String backUpDir = dateHandleDir+"backup.bat";
  Runtime cmd = Runtime.getRuntime();                       
  boolean haveError = false;
  File f = new File(backupFilePath);                         
  if(!f.exists()){
   f.mkdirs();                                             //如果目錄不存在就創建一個
  }
  
  String commandstr =  "/""+backUpDir +"/""+ " " + mysqlUserName + " " + mysqlPassword + " "
  + host + " " + backupFilePath+fileName;
  log.info("commandstr!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + commandstr);
        Process p = cmd.exec(commandstr);                           //執行備份批處理文件
   BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));  //錯誤備份的輸出流
  String line="";
  while ((line = br.readLine()) != null) {
   //發生錯誤
   log.info("<<<<<<<" + line);
   haveError = true;    
  }
  
  if (haveError) {
   //備份數據庫時發生錯誤
   throw new CnidsException(Constants.FAIL_BACKUP_SQL);
  } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章