java 實現mysql數據庫導出


package com.zbb.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class MySQLDatabaseBackup {

/**
* Java代碼實現MySQL數據庫導出
*
* @author
* @param hostIP MySQL數據庫所在服務器地址IP
* @param userName 進入數據庫所需要的用戶名
* @param password 進入數據庫所需要的密碼
* @param savePath 數據庫導出文件保存路徑
* @param fileName 數據庫導出文件文件名
* @param databaseName 要導出的數據庫名
* @return 返回true表示導出成功,否則返回false。
*/
public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目錄不存在
saveFile.mkdirs();// 創建文件夾
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("mysqldump").append(" --opt").append(" -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);
try {
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;
}

public static void recover(String hostIP, String userName, String password, String savePath, String fileName, String databaseName,String path) throws IOException{
Runtime runtime = Runtime.getRuntime();
//-u后面是用戶名,-p是密碼-p后面最好不要有空格,-family是數據庫的名字,--default-character-set=utf8,這句話一定的加
//我就是因為這句話沒加導致程序運行成功,但是數據庫里面的內容還是以前的內容,最好寫上完成的sql放到cmd中一運行才知道報錯了
//錯誤信息:
//mysql: Character set 'utf-8' is not a compiled character set and is not specified in the '
//C:\\Program Files\\MySQL\\MySQL Server 5.5\\share\\charsets\\Index.xml' file ERROR 2019 (HY000): Can't
// initialize character set utf-8 (path: C:\\Program Files\\MySQL\\MySQL Server 5.5\\share\\charsets\\),
//又是討人厭的編碼問題,在恢復的時候設置一下默認的編碼就可以了。
Process process = runtime.exec("mysql -u root -p48512 --default-character-set=utf8 ztest");
OutputStream outputStream = process.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
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();
}

public static void recoverDataBase(String userName, String pwd,
String fileUrl) {
StringBuffer sbRecover = new StringBuffer("imp ");
sbRecover.append(userName);
sbRecover.append("/");
sbRecover.append(pwd);
sbRecover.append(" file=");
sbRecover.append(fileUrl);
sbRecover.append(" ");
sbRecover.append("fromuser=");
sbRecover.append(userName);
sbRecover.append(" ");
sbRecover.append("touser=");
sbRecover.append(pwd);
sbRecover.append(" ");
sbRecover.append("ignore=y,destroy=y;");
runBatchFile(sbRecover);


}
public static void runBatchFile(StringBuffer sb) {
System.out.println("runBatchFile:" + sb.toString());
Process p;
try {
p = Runtime.getRuntime().exec("cmd.exe /c start " + sb.toString());
InputStreamReader isr = new InputStreamReader(p.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (line.indexOf("錯誤") != 1) {
break;
}
p.destroy();
p.waitFor();
}


} catch (IOException e) {


System.out.println(e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException, IOException {
try {
String fPath = "d:/mysql/2016-1-21.sql";
Runtime rt = Runtime.getRuntime();
// 調用 mysql 安裝目錄的命令
Process child = rt.exec("C://Program Files//MySQL//MySQL Server 5.5//bin//mysql -u root -p 48512 ztest");

OutputStream out = child.getOutputStream();// 控制台的輸入信息作為輸出流
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fPath), "utf-8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
System.out.println(outStr);
OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
System.out.println("7777777777777777777777777777777777777");
writer.write(outStr);
System.out.println("888888888888888888888888888888888888888");
writer.flush();
out.close();
br.close();
writer.close();
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

 

PropertiesUtil propertiesUtil = new PropertiesUtil("jdbc.properties");
String ip = propertiesUtil.readProperty("jdbc.ip");
String database = propertiesUtil.readProperty("jdbc.database");
String username = propertiesUtil.readProperty("jdbc.username");
String password = propertiesUtil.readProperty("jdbc.password");
String backupPath = propertiesUtil.readProperty("jdbc.backupPath");
String dbFileName = UUIDUtils.getUUID32()+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+".sql";
boolean dbTRUE = MySQLDatabaseBackup.exportDatabaseTool(ip, username, password, backupPath, dbFileName, database);
if(dbTRUE){
System.out.println("備份數據庫成功"+count++);
}

 

 

package com.zbb.util;

import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class PropertiesUtil {
private String fileName;

public PropertiesUtil(String fileName) {
this.fileName = fileName;
}

public String readProperty(String name) {
Resource res = new ClassPathResource(fileName);
Properties p = new Properties();
try {
p.load(res.getInputStream());
// System.out.println(p.getProperty(name));
} catch (Exception e) {
e.printStackTrace();
}

return p.getProperty(name);

}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM