package com.backup;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.UnsupportedEncodingException;
|
import java.sql.Connection;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.Date;
|
|
import com.base.Com;
|
import com.sql.MysqlConnPool;
|
|
public class MyRunTime {
|
private String dbpath = "";
|
private String serverip = "127.0.0.1";
|
private String sqlport = "3360";
|
private String uname = "root";
|
private String upass = "lmx8688139";
|
private MysqlConnPool pool;
|
|
private String regedit_code = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\BMS_FBSDEV\\Parameters"; //主程序在注册表中的路径
|
private final static String TYPE = "REG_SZ"; //注册表中的参数的类型
|
public MyRunTime(MysqlConnPool conn_pool){
|
this.pool = conn_pool;
|
initParam();
|
}
|
|
/**
|
* 获取数据库的安装目录
|
*/
|
private void initParam() {
|
//根据数据库连接获取mysql的安装目录
|
dbpath = getMysqlPath();
|
if(dbpath != null && dbpath.length() > 0){
|
//System.err.println(dbpath);
|
File file = new File(dbpath);
|
if(file.exists()){
|
System.out.println("数据库的安装目录:"+dbpath);
|
}else{
|
//根据主程序在注册表中的参数获取mysql的安装目录
|
dbpath = getMySQLPathFromReg();
|
System.out.println(dbpath);
|
if(dbpath != null && dbpath.length()>0){
|
file = new File(dbpath);
|
if(file.exists()){
|
System.out.println("数据库的安装目录:"+dbpath);
|
}else {
|
System.err.println("未获取到数据库的安装目录 at " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
}
|
}
|
}
|
}
|
}
|
|
/**
|
* 备份数据库表到指定的文件目录
|
* @param code
|
* @return
|
*/
|
public boolean exec(String dbname,String tbname,String backFilePath){
|
boolean flag = true;
|
Runtime runt = Runtime.getRuntime();
|
String code = dbpath+"/" + "mysqldump -h "+serverip
|
+ " -P" + sqlport + " -u" + uname + " -p" //根据数据库中的mysqldump备份数据库
|
+ upass + " --result-file=" + backFilePath+"/"+tbname+".sql"
|
+ " --opt -Q -R --skip-lock-tables --default-character-set=utf8 " + dbname +" "+tbname ;
|
try {
|
Process proc = runt.exec(code);
|
int tag = proc.waitFor(); // 等待备份完成
|
flag = flag && (tag == 0);
|
} catch (IOException e) {
|
e.printStackTrace();
|
flag = false;
|
} catch (InterruptedException e) {
|
flag = false;
|
e.printStackTrace();
|
}
|
return flag;
|
}
|
|
|
/**
|
* 根据数据库连接查询数据库的安装目录(对于安装目录中包含中问的情况不能成功)
|
* @param conn
|
* @return
|
*/
|
public String getMysqlPath(){
|
String sql="select @@basedir as basePath from dual";
|
PreparedStatement ps = null;
|
ResultSet rs = null;
|
String path="";
|
Connection conn = null;
|
try {
|
conn = this.pool.getConn();
|
ps = conn.prepareStatement(sql);
|
rs=ps.executeQuery();
|
if(rs.next()){
|
path=rs.getString("basePath");
|
}
|
if(path.endsWith("\\") || path.endsWith("/")){
|
path += "bin";
|
}else{
|
path += "/bin";
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally{
|
if(conn != null){
|
try {
|
conn.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return path;
|
}
|
|
|
|
/**
|
* 根据主程序在注册表中的目录获取mysql的安装目录
|
* @return
|
*/
|
public String getMySQLPathFromReg(){
|
String result = "";
|
String path = getRegeditParam(regedit_code,"AppDirectory");
|
if(path != null && path.length() > 0){
|
File f = new File(path);
|
File ParentFile = f.getParentFile();
|
File[] files = ParentFile.listFiles();
|
for(int i=0;i<files.length;i++){
|
if(files[i].isDirectory()){
|
String fileString = files[i].getName().toUpperCase();
|
if(fileString.indexOf("MYSQL") >= 0){
|
result = files[i].getAbsolutePath();
|
if(result.endsWith("\\") || result.endsWith("/")){
|
result += "bin";
|
}else{
|
result += "/bin";
|
}
|
return result;
|
}
|
}
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 根据在注册表中的指定的key值和注册表中的路径值获取指定的参数
|
* @param path
|
* @param key
|
* @return
|
*/
|
public static String getRegeditParam(String path,String key){
|
String commond = " reg query " + path;
|
String result = "";
|
InputStreamReader in = null;
|
try {
|
Process ps = null;
|
ps = Runtime.getRuntime().exec(commond);
|
ps.getOutputStream().close();
|
in = new InputStreamReader(ps.getInputStream(),"gbk");
|
String line;
|
BufferedReader ir = new BufferedReader(in);
|
int index = 0;
|
while ((line = ir.readLine()) != null) {
|
//System.out.println(line);
|
if(line.indexOf(key) >= 0 && (index = line.indexOf(TYPE)) >= 0){
|
result = line.substring(index + TYPE.length()).trim();
|
}
|
}
|
return result;
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally{
|
if(in != null){
|
try {
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return result;
|
}
|
|
|
public static void main(String[] args) throws UnsupportedEncodingException {
|
MysqlConnPool pool = new MysqlConnPool("127.0.0.1",3360,5);
|
|
System.out.println(new MyRunTime(pool));
|
}
|
}
|