Springboot 데이터베이스 백업 복원 방법
4378 단어 Springboot데이터 뱅 크백업환원
이번 에는 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 데이터베이스 백업 복원 에 관 한 더 많은 내용 은 예전 의 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.