1. 程式人生 > >使用Java實現Mysql資料庫的備份與恢復

使用Java實現Mysql資料庫的備份與恢復

廢話不多說,直接上程式碼,如下示例:

public class DatabaseUtil {

    public static void main(String[] args) throws Exception {
        //測試備份
        String command1 = "mysqldump -hlocalhost -ualex -p123 demo";//引數依次是IP、賬號、密碼、資料庫名
        String savePath1 = "D:/demo.sql";
        boolean b1 = new DatabaseUtil().backup(command1, savePath1);
        if(b1){
            System.out.println("備份成功");
        }else {
            System.out.println("備份失敗");
        }

        /*//測試還原
        String command2 = "mysql.exe -hlocalhost -ualex -p123 --default-character-set=utf8 demo";
        String savePath2 = "D:/demo.sql";
        boolean b2 = new DatabaseUtil().recover(command2, savePath2);
        if(b2){
            System.out.println("還原成功");
        }else {
            System.out.println("還原失敗");
        }*/
    }

    /**
     * mysql的備份方法
     *
     * @param command  命令列
     * @param savePath 備份路徑
     * @return
     */
    public boolean backup(String command, String savePath) {
        boolean flag;
        // 獲得與當前應用程式關聯的Runtime物件
        Runtime r = Runtime.getRuntime();
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            // 在單獨的程序中執行指定的字串命令
            Process p = r.exec(command);
            // 獲得連線到程序正常輸出的輸入流,該輸入流從該Process物件表示的程序的標準輸出中獲取資料
            InputStream is = p.getInputStream();
            // InputStreamReader是從位元組流到字元流的橋樑:它讀取位元組,並使用指定的charset將其解碼為字元
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            //BufferedReader從字元輸入流讀取文字,緩衝字元,提供字元,陣列和行的高效讀取
            br = new BufferedReader(isr);
            String s;
            StringBuffer sb = new StringBuffer("");
            // 組裝字串
            while ((s = br.readLine()) != null) {
                sb.append(s + "\r\n");
            }
            s = sb.toString();
            // 建立檔案輸出流
            FileOutputStream fos = new FileOutputStream(savePath);
            // OutputStreamWriter是從字元流到位元組流的橋樑,它使用指定的charset將寫入的字元編碼為位元組
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
            // BufferedWriter將文字寫入字元輸出流,緩衝字元,以提供單個字元,陣列和字串的高效寫入
            bw = new BufferedWriter(osw);
            bw.write(s);
            bw.flush();
            flag = true;
        } catch (IOException e) {
            flag = false;
            e.printStackTrace();
        } finally {
            //由於輸入輸出流使用的是裝飾器模式,所以在關閉流時只需要呼叫外層裝飾類的close()方法即可,
            //它會自動呼叫內層流的close()方法
            try {
                if (null != bw) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (null != br) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    /**
     * mysql的還原方法
     *
     * @param command  命令列
     * @param savePath 還原路徑
     * @return
     */
    public boolean recover(String command, String savePath) {
        boolean flag;
        Runtime r = Runtime.getRuntime();
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            Process p = r.exec(command);
            OutputStream os = p.getOutputStream();
            FileInputStream fis = new FileInputStream(savePath);
            InputStreamReader isr = new InputStreamReader(fis, "utf-8");
            br = new BufferedReader(isr);
            String s;
            StringBuffer sb = new StringBuffer("");
            while ((s = br.readLine()) != null) {
                sb.append(s + "\r\n");
            }
            s = sb.toString();
            OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
            bw = new BufferedWriter(osw);
            bw.write(s);
            bw.flush();
            flag = true;
        } catch (IOException e) {
            flag = false;
            e.printStackTrace();
        } finally {
            try {
                if (null != bw) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (null != br) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
}