1. 程式人生 > >資料庫的備份 與還原

資料庫的備份 與還原

進行之前,你需要確定,你真的配置好了資料庫的環境變數,我這裡用的是mysql

path:

%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%CATALINA_HOME%\bin;%CATALINA_HOME%\lib;D:\java\mysql-5.7\bin;%MAVEN_HOME%\bin;%MYSQL_HOME%

 public static void backupOne() {  
               try {
                    Runtime rt = Runtime.getRuntime();
        
                    // 呼叫 呼叫mysql的安裝目錄的命令
                    Process child = rt
                            .exec("mysqldump -h  127.0.0.1 -uroot -p123456 ssss");
                    // 設定匯出編碼為utf-8。這裡必須是utf-8
                    // 把程序執行中的控制檯輸出資訊寫入.sql檔案,即生成了備份檔案。注:如果不對控制檯資訊進行讀出,則會導致程序堵塞無法執行
                    InputStream in = child.getInputStream();// 控制檯的輸出資訊作為輸入流
        
                    InputStreamReader xx = new InputStreamReader(in, "UTF-8");
                    // 設定輸出流編碼為utf-8。這裡必須是utf-8,否則從流中讀入的是亂碼
        
                    String inStr;
                    StringBuffer sb = new StringBuffer("");
                    String outStr;
                    // 組合控制檯輸出資訊字串
                    BufferedReader br = new BufferedReader(xx);
                    while ((inStr = br.readLine()) != null) {
                        sb.append(inStr + "\r\n");
                    }
                    outStr = sb.toString();
        
                    // 要用來做匯入用的sql目標檔案:
                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                      String data = df.format( new  Date());
                    FileOutputStream fout = new FileOutputStream("d:\\dataBase.sql");
                    OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
                    writer.write(outStr);
                    writer.flush();
                    in.close();
                    xx.close();
                    br.close();
                    writer.close();
                    fout.close();
        
                    System.out.println("結束");
        
                } catch (Exception e) {
                    e.printStackTrace();
                }
        
    }


資料庫還原方法

public static void restore() {
        //這裡改成自己的
        String databaseName ="ssss";
        
        
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime
                    .exec("D:\\java\\mysql\\bin\\mysql.exe -h localhost -uroot -p123456 --default-character-set=utf8 "
                            + databaseName);
            OutputStream outputStream = process.getOutputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream("d:\\test.sql"), "utf-8"));
            String str = null;
            StringBuffer sb = new StringBuffer();
            while ((str = br.readLine()) != null) {
                sb.append(str + "\r\n");
            }
            str = sb.toString();
            // System.out.println(str);
            OutputStreamWriter writer = new OutputStreamWriter(outputStream,
                    "utf-8");
            writer.write(str);
            writer.flush();
            outputStream.close();
            br.close();
            writer.close();
            System.out.println("已經備份完畢");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

執行就行。

參考:https://blog.csdn.net/enweitech/article/details/51612858 

還有很多沒看懂。。。。。。。