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);
}
}