프로그래밍/java

[java] DB백업 파일 만들기

혀끄니 2023. 6. 12. 12:00
728x90

-DB백업 파일 만들기

String fullUrl = propertiesService.getString("mariaUrl"); //properties파일에 있는 디비 주소 가져오기
String host = fullUrl.split(":")[2].substring(2);  //아이피주소만 저장
String userName = propertiesService.getString("userName"); //디비 아이디 가져오기
String password = propertiesService.getString("password"); //디비 비밀번호 가져오기

// 파라미터 확인
String tableSchema = "";
String tableName = "";
String command = "";
List<EgovMap> gridRows = row.getSchGridRows();  //디비 정보 가져오기
	if (gridRows.size() > 0) {
		EgovMap r = (EgovMap)gridRows.get(0);
		tableSchema = (String) r.get("tableSchema");
		tableName = (String) r.get("tableName");

		if (tableName == "") {	// DB전체백업
			command = "mysqldump -h" + host + " -u" + userName + " -p" + password + " " + tableSchema;			
		} else {			// Table백업
			command = "mysqldump -h" + host + " -u" + userName + " -p" + password + " " + tableSchema + " " + tableName;
		}
	}
	try {
		String rootPath = propertiesService.getString("fileUpload");	// 파일업로드 경로
		//DB백업할 파일 생성 
        File rootFolder = new File(rootPath);
		if (!rootFolder.exists()) {
			try {
				rootFolder.mkdir();
			} catch (Exception e) {
				e.getStackTrace();
			}
		}
		LocalDate now = LocalDate.now();
		String dbPath = rootPath + "\\db";
		String filePath = dbPath + "\\"+ now;
		String fileName = filePath + "\\" + tableSchema + "-" + tableName + "(" + now + ").sql";
		File dbFolder = new File(dbPath);
		if (!dbFolder.exists()) {
			dbFolder.mkdir();
		}
		File fileFolder = new File(filePath);
		if (!fileFolder.exists()) {
			fileFolder.mkdir();
		} 
			
		File file = new File(fileName);
		FileWriter fw = new FileWriter(file);
		WinCmd cmd = new WinCmd(); //외부명령어 실행 게시글에서 동작 방법 확인가능
		String sql = cmd.execCommand(command);
		fw.write(sql);
		fw.close();

 

같이보면 좋은 자료

https://blog.naver.com/gurrms95/222698642439

 

[JAVA] 외부명령어 실행

-외부명령어 실행

blog.naver.com

 

728x90