----------------------------------------------------->>>>>>>>
原理:MySQL 数据库备份原理: Navicat等数据库界面软件通用的数据库备份原理就是直
接调用MYSQL本身的系统命令。
MySQL本身的系统命令:
--opt –h localhost --user=root --password=admin --lock-all-tables=true --result-file=E://oes//2221.sql --default-character-set=utf8 oes
解析:主机–h,用户名--user,密码—password,锁定所有表--lock-all-tables=true,
目标文件--result-file,编码--default-character-set=utf8,数据源oes
Java中执行系统命令方法:
Runtime cmd = Runtime.getRuntime();
Process p = cmd.exec(“”); //执行CMD指令(String)
由于无法确定主机是否配置了MySQL环境变量,所以需要最保险的确定MySQL中mysqldump的位置,它存在于MySQL安装文件夹得Bin目录下,问题就是如何获取MySQL的安装目录?
----------------------------------------------------->>>>>>>>
针对于获取MySQL的安装目录,我用的是比较笨的方法:解析注册表。
找到注册表中MySQL的软件信息,里面包含有软件的安装地址,卸载地址,版本号等等基本信息,直接取用它的安装信息就行。
软件信息在注册表中的位置:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
软件关联注册表中软件信息:
----------------------------------------------------->>>>>>>>
类CheckSoftware,解析MySQL软件安装地址
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CheckSoftware {
/*
* 遍历注册表,查询MySQL的注册表关联
*/
public static String check() throws Exception {
Runtime runtime = Runtime.getRuntime();
Process process = null;
process = runtime
.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
BufferedReader in = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String string = null;
while ((string = in.readLine()) != null) {
process = runtime.exec("cmd /c reg query " + string
+ " /v DisplayName");
BufferedReader name = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String message = queryValue(string, "DisplayName");
if (message != null && message.contains("MySQL")) {
String message2 = queryValue(string, "InstallLocation");
return message2;
}
}
in.close();
process.destroy();
return null;
}
/*
* 查询出需要的MySQL服务的安装路径
*/
private static String queryValue(String string, String method)
throws IOException {
String pathString = "";
Runtime runtime = Runtime.getRuntime();
Process process = null;
BufferedReader br = null;
process = runtime.exec("cmd /c reg query " + string + " /v " + method);
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
br.readLine();
br.readLine();// 去掉前两行无用信息
if ((pathString = br.readLine()) != null) {
pathString = pathString.replaceAll(method + " REG_SZ ", ""); // 去掉无用信息
return pathString;
}
return pathString;
}
}
----------------------------------------------------->>>>>>>>
类JavaMysql备份还原数据库
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class JavaMysql {
/*
* 备份数据库 1、读取配置文件 2、启动智能查询Mysql安装目录 3、备份数据库为sql文件
*/
public static void backup(String sql) {
Properties pros = getPprVue("prop.properties");
String username = pros.getProperty("username");
String password = pros.getProperty("password");
CheckSoftware c = null;
try {
System.out.println("MySQL服务安装地址 :"+c.check().toString());
} catch (Exception e2) {
e2.printStackTrace();
}
String mysqlpaths;
try {
mysqlpaths = c.check().toString() + "bin" + "\\";
String databaseName = pros.getProperty("databaseName");
String address = pros.getProperty("address");
String sqlpath = pros.getProperty("sql");
File backupath = new File(sqlpath);
if (!backupath.exists()) {
backupath.mkdir();
}
StringBuffer sb = new StringBuffer();
sb.append(mysqlpaths);
sb.append("mysqldump ");
sb.append("--opt ");
sb.append("-h ");
sb.append(address);
sb.append(" ");
sb.append("--user=");
sb.append(username);
sb.append(" ");
sb.append("--password=");
sb.append(password);
sb.append(" ");
sb.append("--lock-all-tables=true ");
sb.append("--result-file=");
sb.append(sqlpath);
sb.append(sql);
sb.append(" ");
sb.append("--default-character-set=utf8 ");
sb.append(databaseName);
System.out.println("cmd指令 :"+sb.toString());
Runtime cmd = Runtime.getRuntime();
try {
Process p = cmd.exec(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
/*
* 读取属性文件
*/
public static Properties getPprVue(String properName) {
InputStream inputStream = JavaMysql.class.getClassLoader()
.getResourceAsStream(properName);
Properties p = new Properties();
try {
p.load(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
/*
* 根据备份文件恢复数据库
*/
public static void load(String filename) {
Properties pros = getPprVue("prop.properties");
String root = pros.getProperty("jdbc.username");
String pass = pros.getProperty("jdbc.password");
String mysqlpaths = c.check().toString() + "bin" + "\\";
String sqlpath = pros.getProperty("sql");
String filepath = mysqlpaths + sqlpath + filename; // 备份的路径地址
String stmt1 = mysqlpaths + "mysqladmin -u " + root + " -p" + pass
+ " create finacing"; // -p后面加的是你的密码
String stmt2 = mysqlpaths + "mysql -u " + root + " -p" + pass
+ " finacing < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("数据已从 " + filepath + " 导入到数据库中");
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Test测试
*/
public static void main(String[] args) throws IOException {
backup("2221.sql");
}
}
----------------------------------------------------->>>>>>>>
属性文件: prop.properties,动态配置用户名及密码等基本属性
username = root
password = admin
sql = E://oes//
address=localhost
databaseName=oes
http://cobaya.cn/