Springboot 데이터베이스 백업 복원 방법

전에 컴퓨터 를 다시 설치 한 결과 IDEA 의 프로젝트 디 렉 터 리 가 백업 되 지 않 아서 평소에 참고 할 항목 을 잃 어 버 렸 습 니 다.특히 자신 이 쓴 Springboot 프로젝트 가 백업 되 지 않 았 습 니 다.이번 에는 완전히 인연 이 없어 서 다시 만 날 수 있 습 니 다.어떤 것 은 블 로그 원 에 두 는 것 이 좋 습 니 다.가끔 이런 문제 가 발생 하면 Ctrl+C&Ctrl+V 로 해결 할 수 있 습 니 다.
이번 에는 Springboot 가 데이터 베 이 스 를 백업 하고 백업 데 이 터 를 통 해 데이터 베 이 스 를 복원 하 는 것 을 기록 합 니 다.물론 Springboot 에 국한 되 지 않 고 데이터베이스 백업 복원 코드,자바 와 관련 된 것 은 모두 사용 할 수 있 습 니 다.
백업 데이터베이스
백업 은 명령 행 을 통 해 데이터 베 이 스 를 지정 한 디 렉 터 리 로 내 보 내 면 됩 니 다.저 는 Get 요청 입 니 다.페이지 는 백업 파일 의 이름,크기 와 백업 시간 을 보 여 줘 야 합 니 다.코드 에 사 용 된 log 는 Slf4j 이 고 최종 인터페이스 효 과 는 그림 과 같 습 니 다.

코드 가 원래 코드 에 변경 되 었 습 니 다.백업 파일 의 저장 디 렉 터 리 에 대해 서 는 application.properties 설정 파일 에 설정 되 어 있 습 니 다.하나의 설정 류 ProjectUrlConfig 를 통 해 가 져 옵 니 다.코드 에 있 는 procject UrlConfig.getBackPath()는 파일 디 렉 터 리 이 고 fileName 과 완전한 경 로 를 연결 합 니 다.

 /*       */
  @GetMapping("backupSQL")
  public ModelAndView backupSQL(Map<String, Object> map){
    String fileName = "backup_" + new Date().getTime() + ".sql";
    String cmd = "mysqldump -uroot -p123456 dbName > " + projectUrlConfig.getBackPath() + fileName; //-u  root mysql      ,-p   123456      ,       ;dbName              ,           
    try {
      Runtime.getRuntime().exec(cmd);
    }catch (Exception e){
      log.error("【     】  :{}", e.getMessage());
      map.put("msg", e.getMessage());
      return new ModelAndView("common/error", map);
    }
    log.info("【     】  ,SQL  :{}", fileName);
    map.put("msg","       ");return new ModelAndView("common/success", map);
  }
복구 데이터베이스
백업 은 cmd 명령 줄 에서"mysql-urot-p123456 dbName @GetMapping("rollback") public ModelAndView rollback(@RequestParam("filename") String fileName, Map<String, Object> map){ String path = projectUrlConfig.getBackPath() + fileName; try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("mysql -uroot -p123456 --default-character-set=utf8 dbName"); OutputStream outputStream = process.getOutputStream(); FileInputStream fis = new FileInputStream(path); InputStreamReader isr = new InputStreamReader(fis, "utf-8"); BufferedReader br = new BufferedReader(isr); String str = null; StringBuffer sb = new StringBuffer(); while ((str = br.readLine()) != null) { sb.append(str + "\r
"); } str = sb.toString(); OutputStreamWriter writer = new OutputStreamWriter(outputStream,"utf-8"); writer.write(str); writer.flush(); if(writer!=null){ writer.close(); } if(br!=null){ br.close(); } if(isr!=null){ isr.close(); } if(fis!=null){ fis.close(); } if(outputStream!=null){ outputStream.close(); } }catch (Exception e){ log.error("【 】 :{}", e.getMessage()); map.put("msg", e.getMessage()); return new ModelAndView("common/error", map); } log.info("【 】 , :{}", fileName); map.put("msg"," ");return new ModelAndView("common/success", map); }
이상 은 데이터 베 이 스 를 백업 하고 복원 할 수 있 지만 작은 데이터 베이스 에 만 적용 된다.
https://blog.csdn.net/duli3554197/article/details/89468758
총결산
Springboot 데이터베이스 백업 복원 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.Springboot 데이터베이스 백업 복원 에 관 한 더 많은 내용 은 예전 의 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기