備份的是Mysql資料庫。
代碼如下,自己運行下看看吧。廢話不說了。
package backup.back;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;/** * 資料庫的備份與還原作業 * * @Date 2017年3月7日 */public class DbOperate { /** * 資料庫表備份 * * @throws Exception */ public static void tableBackup(String dbUser,String dbPass,String dbHost,String dbPort,String dbName,String savePath, String tableName) throws Exception { Runtime runtime = Runtime.getRuntime(); // -u後面是使用者名稱,-p是密碼-p後面最好不要有空格,-family是資料庫的名字 Process process = runtime.exec("mysqldump -h " + dbHost + " -P " + dbPort + " -u " + dbUser + " -p" + dbPass + " " + dbName + " " + tableName); InputStream inputStream = process.getInputStream();// 得到輸入資料流,寫成.sql檔案 InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader br = new BufferedReader(reader); String s = null; StringBuffer sb = new StringBuffer(); while ((s = br.readLine()) != null) { sb.append(s + "\r\n"); } s = sb.toString(); File file = new File(savePath); file.getParentFile().mkdirs(); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(s.getBytes()); fileOutputStream.close(); br.close(); reader.close(); inputStream.close(); } /** * 備份資料庫 * * @param savePath * @throws Exception */ public static void dbBackup(String dbUser, String dbPass, String dbHost, String dbPort, String dbName, String savePath) throws Exception { Runtime runtime = Runtime.getRuntime(); // -u後面是使用者名稱,-p是密碼-p後面最好不要有空格,-family是資料庫的名字 Process process = runtime.exec("mysqldump -h " + dbHost + " -P " + dbPort + " -u " + dbUser + " -p" + dbPass + " " + dbName); InputStream inputStream = process.getInputStream();// 得到輸入資料流,寫成.sql檔案 InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader br = new BufferedReader(reader); String s = null; StringBuffer sb = new StringBuffer(); while ((s = br.readLine()) != null) { sb.append(s + "\r\n"); } s = sb.toString(); File file = new File(savePath); file.getParentFile().mkdirs(); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(s.getBytes()); fileOutputStream.close(); br.close(); reader.close(); inputStream.close(); } /** * 執行sql檔案 * * @param savePath * @throws Exception */ public static void dbRecover(String dbUser, String dbPass, String dbHost, String dbPort, String dbName, String savePath) throws Exception { // 擷取操作資料庫的相關屬性 Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("mysql -h" + dbHost + " -P " + dbPort + " -u " + dbUser + " -p" + dbPass + " --default-character-set=utf8 " + dbName); OutputStream outputStream = process.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(savePath))); String str = null; StringBuffer sb = new StringBuffer(); while ((str = br.readLine()) != null) { sb.append(str + "\r\n"); } str = sb.toString(); OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8"); writer.write(str); writer.flush(); outputStream.close(); br.close(); writer.close(); } public static void main(String[] args) { /*try { dbBackup("root", "ROOT", "localhost","3306", "EPDB", "D:/back.sql"); System.out.println("完成。"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ try { dbRecover("root", "ROOT", "localhost","3306", "EPDB", "D:/back.sql"); System.out.println("完成。"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}