package com.backup;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.sql.Connection;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
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 String backFilePath = "D:/temp/a.sql";
|
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){
|
File file = new File(dbpath);
|
if(file.exists()){
|
System.out.println(file.getAbsolutePath());
|
}else{
|
//´Ó×¢²á±íÖлñÈ¡Êý¾Ý¿âµÄ°²×°Ä¿Â¼
|
dbpath = getMySQLPathFromReg();
|
if(dbpath != null && dbpath.length()>0){
|
file = new File(dbpath);
|
if(file.exists()){
|
System.out.println(file.getAbsolutePath());
|
}
|
}
|
}
|
}
|
}
|
|
/**
|
* Ö´ÐÐÖ¸¶¨µÄCode
|
* @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" //µÚ¶þ¸ö-pºóÃæ²»ÄÜÓпոñ£¬·ñÔò½«±»ÈÏΪÊÇÊý¾Ý¿âµÄÃû³Æ
|
+ 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";
|
}
|
System.out.println(path);
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally{
|
if(conn != null){
|
try {
|
conn.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return path;
|
}
|
|
|
|
/**
|
* ´Ó×¢²á±íÖлñȡƴ½ÓÊý¾Ý¿âµÄ°²×°Ä¿Â¼
|
* @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;
|
}
|
|
/**
|
* »ñȡָ¶¨×¢²á±íϵIJÎÊýÖµ
|
* @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());
|
String line;
|
BufferedReader ir = new BufferedReader(in);
|
int index = 0;
|
while ((line = ir.readLine()) != null) {
|
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) {
|
//MysqlConnPool pool = new MysqlConnPool("127.0.0.1",3360,5);
|
//MyRunTime my = new MyRunTime(pool);
|
//my.exec("db_alarm", "tb_devalarm_data");
|
|
//System.out.println(new MyRunTime(null).getMySQLPathFromReg());
|
|
}
|
}
|