Java實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)的自動(dòng)備份和自動(dòng)還原
在介紹前我要說(shuō)一下,網(wǎng)上的例子不少,但是坑很多,有些代碼看起來(lái)是對(duì)的,但是小問(wèn)題不少,原因在于樓主沒(méi)有測(cè)試過(guò),或者沒(méi)有交代使用時(shí)的特殊環(huán)境。以下代碼都是親身測(cè)試過(guò)的。
1.項(xiàng)目背景:使用java實(shí)現(xiàn)遠(yuǎn)程備份mysql數(shù)據(jù)庫(kù)到本地電腦,并可以將本地?cái)?shù)據(jù)庫(kù)的備份還原到遠(yuǎn)程mysql數(shù)據(jù)庫(kù)。本機(jī)環(huán)境windows。
2.注意:本機(jī)必須已經(jīng)裝了mysql數(shù)據(jù)庫(kù),并且將mysql 的bin加在了系統(tǒng)環(huán)境變量中。我的mysql在c盤,以下代碼也是以C盤的mysql的bin下運(yùn)行的。具體要以你實(shí)際安裝MYSQL的位置而定,不然就會(huì)自己坑自己了。
3.代碼邏輯:很重要!?。?/p>
4.我們首先(在遠(yuǎn)程數(shù)據(jù)庫(kù))創(chuàng)建一個(gè)有測(cè)試數(shù)據(jù)的庫(kù):robot,一個(gè)空的數(shù)據(jù)庫(kù)(沒(méi)表,沒(méi)數(shù)據(jù))robot-test。
5.我們第一步將robot數(shù)據(jù)庫(kù)備份到本地電腦d:\test,名字命名:robot-test.sql。
6.查看本地電腦的d:\test\robot-test.sql是否存在,并確定是否備份成功。

7.第二步,將robot-test.sql還原到之前創(chuàng)建的空數(shù)據(jù)庫(kù):robot-test。
8.查看robot-test數(shù)據(jù)庫(kù)的表和數(shù)據(jù)是否還原成功。
備份和還原方法寫到一個(gè)類中:
package com.company;
import java.io.File;
import java.io.IOException;
public class DataBaseDumpOrBackrout {
/**
* @param hostIP ip地址,可以是本機(jī)也可以是遠(yuǎn)程
* @param userName 數(shù)據(jù)庫(kù)的用戶名
* @param password 數(shù)據(jù)庫(kù)的密碼
* @param savePath 備份的路徑
* @param fileName 備份的文件名
* @param databaseName 需要備份的數(shù)據(jù)庫(kù)的名稱
* @return
*/
public static boolean backup(String hostIP, String userName, String password, String savePath, String fileName,
String databaseName) {
fileName +=".sql";
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目錄不存在
saveFile.mkdirs();// 創(chuàng)建文件夾
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}
//拼接命令行的命令
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump --opt -h ").append(hostIP);
stringBuilder.append(" --user=").append(userName).append(" --password=").append(password)
.append(" --lock-all-tables=true");
stringBuilder.append(" --result-file=").append(savePath + fileName).append(" --default-character-set=utf8 ")
.append(databaseName);
System.out.println(stringBuilder.toString());
try {
//調(diào)用外部執(zhí)行exe文件的javaAPI
Process process = Runtime.getRuntime().exec(stringBuilder.toString());
if (process.waitFor() == 0) {// 0 表示線程正常終止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
/**
* @param filepath 數(shù)據(jù)庫(kù)備份的腳本路徑
* @param ip IP地址
* @param database 數(shù)據(jù)庫(kù)名稱
* @param userName 用戶名
* @param password 密碼
* @return
*/
public static boolean recover(String filepath,String ip,String database, String userName,String password) {
String stmt1 = "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqladmin -h "+ip+" -u"+userName+" -p"+password+" create DATABASE if not EXISTS "+database;
String stmt2 = "mysql -h "+ip+" -u"+userName+" -p"+password+" "+database+" < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
System.out.println(stmt1);
Runtime.getRuntime().exec(stmt1);
System.out.println(cmd);
Runtime.getRuntime().exec(cmd);
System.out.println("數(shù)據(jù)已從 " + filepath + " 導(dǎo)入到數(shù)據(jù)庫(kù)中");
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}
備份的main方法

package com.company;
/**
*
* @author zndume
* 數(shù)據(jù)庫(kù)備份主方法測(cè)試
*
*/
public class BuckupMain {
public static void main(String[] args) {
String hostip = "172.17.7.60";
String username = "root";
String password = "zndume";
String savepath = "D:\\test";
String filename = "robot-test";
String databaseName = "robot";
boolean backup = DataBaseDumpOrBackrout.backup(hostip, username, password, savepath, filename, databaseName);
System.out.println(backup);
}
}
還原的main方法

package com.company;
public class RecoverMain {
public static void main(String[] args) {
String ip = "172.17.7.60";
String userName = "root";
String password = "zndume";
String savepath = "D:\\test\\robot-test.sql";
String databaseName = "robot-test";
boolean recover = DataBaseDumpOrBackrout.recover(savepath, ip, databaseName, userName, password);
System.out.println(recover);
}
}
以上就是Java實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)的自動(dòng)備份和自動(dòng)還原的詳細(xì)內(nèi)容,更多關(guān)于Java mysql自動(dòng)備份和自動(dòng)還原的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java lambda循環(huán)_使用Java 8 Lambda簡(jiǎn)化嵌套循環(huán)操作
這篇文章主要介紹了java lambda循環(huán)_使用Java 8 Lambda簡(jiǎn)化嵌套循環(huán)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
mybatis攔截器無(wú)法注入spring bean的問(wèn)題解決
本文主要介紹了mybatis攔截器無(wú)法注入spring bean的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
解析SpringBoot?搭建基于?MinIO?的高性能存儲(chǔ)服務(wù)的問(wèn)題
Minio是Apache?License?v2.0下發(fā)布的對(duì)象存儲(chǔ)服務(wù)器,使用MinIO構(gòu)建用于機(jī)器學(xué)習(xí),分析和應(yīng)用程序數(shù)據(jù)工作負(fù)載的高性能基礎(chǔ)架構(gòu)。這篇文章主要介紹了SpringBoot?搭建基于?MinIO?的高性能存儲(chǔ)服務(wù),需要的朋友可以參考下2022-03-03

