JAVA 데이터베이스 백업 실현 (원 격 데이터베이스 지원)

package com.gaxf.paas.task;

import com.gaxf.paas.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class DatabaseTask {
    private static final Logger log = LoggerFactory.getLogger(DatabaseTask.class);
    private static final String PORT = ""; //      
    private static final String HOST_IP = ""; //         ip                 mysqldump   
    private static final String USERNAME = ""; //       
    private static final String PASSWORD = ""; //        
    private static final String SAVE_PATH_WINDOWS = "D:\\database\\"; //              windosw    
    private static final String SAVE_PATH_LINUX = "/data/database/"; //              linux    


    // ========================           ============================================
    private static final String JIMI_HYDRANT = "jimi-hydrant";
    private static final String PAAS = "paas";


    /**
     *      
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void dbBackUpPaas() {
        try {
            dbBackUp(PAAS);
            dbBackUp(JIMI_HYDRANT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param databaseName            
     * @return
     */
    public static void dbBackUp(String databaseName) throws Exception {
        String fileName = databaseName + "_" +      + ".sql";
        File saveFile = new File(savePath());
        if (!saveFile.exists()) {//        
            saveFile.mkdirs();//      
        }

        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath() + fileName), "utf8"));
        Process process = Runtime.getRuntime().exec(getCommand(fileName, databaseName));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf8"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            printWriter.println(line);
        }
        printWriter.flush();
        if (process.waitFor() != 0) {
            log.info("       !");
        }
    }

    public static String getCommand(String fileName, String databaseName) {
        StringBuilder arg = new StringBuilder();
        arg.append("mysqldump ");
        arg.append(" -h" + HOST_IP);
        arg.append(" -P" + PORT);
        arg.append(" -u" + USERNAME);
        arg.append(" -p" + PASSWORD);
        arg.append(" --skip-opt ");
        arg.append("--add-drop-database ");
        arg.append("--routines ");
        arg.append("--triggers ");
        arg.append("--compress ");
        arg.append("-r");
        arg.append(savePath());
        arg.append(fileName);
        arg.append(" --databases " + databaseName);
        log.info(arg.toString());
        return arg.toString();
    }

    /**
     *     sql     
     *
     * @return
     */
    public static String savePath() {
        String savePath;
        if (isWindows()) {
            savePath = SAVE_PATH_WINDOWS;
        } else {
            savePath = SAVE_PATH_LINUX;
        }
        return savePath;
    }

    /**
     *                
     *
     * @return
     */
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }
}

좋은 웹페이지 즐겨찾기