package com.battmonitor.sql;
|
|
import java.io.BufferedReader;
|
import java.io.BufferedWriter;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.OutputStreamWriter;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.Date;
|
|
/**
|
* ¶¨ÆÚ±¸·ÝÊý¾Ý¿âµÄÏß³Ì
|
* @author ¾ü
|
*
|
*/
|
public class MysqlDBBackUp_Thread extends Thread {
|
private MysqlConnPool m_conn_pool = null;
|
private String db_path = ""; //Êý¾Ý¿âµÄ°²×°Ä¿Â¼
|
private int back_up_day = 30; //ĬÈϱ¸·ÝÊý¾Ý¿âµÄ¼ä¸ô
|
private Date m_latest_backup_time = new Date(); //×îºó±¸·ÝÊý¾Ý¿âµÄʱ¼ä
|
private int mSqlConnPort = 5306; //Êý¾Ý¿âµÄ¶Ë¿ÚºÅ
|
|
public MysqlDBBackUp_Thread(MysqlConnPool con_pool) {
|
back_up_day = 30;
|
m_conn_pool = con_pool;
|
mSqlConnPort = m_conn_pool.getSqlConnPort();
|
|
Sql_Mysql sql = new Sql_Mysql(m_conn_pool.getConn());
|
try {
|
ResultSet res = sql.sqlMysqlQuery("select @@basedir as basePath from dual");
|
if(res.next()) {
|
db_path = res.getString(1);
|
db_path.substring(0, db_path.indexOf(":")+1);
|
}
|
|
res = sql.sqlMysqlQuery("SELECT SqlDB_BackUpTime FROM " + Sql_Mysql.AppSys_Table);
|
if(res.next()) {
|
m_latest_backup_time = res.getTimestamp(1);
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally {
|
sql.close_con();
|
}
|
}
|
|
@Override
|
public void run() {
|
/**********************************************************************************/
|
System.out.println(this.getName() + " - MysqlDBBackUp_Thread Started ...");
|
/**********************************************************************************/
|
while(true) {
|
try {
|
boolean db_backup_manual_en = false;
|
for(int n=0; n<back_up_day; n++) {
|
for(int h=0; h<24*3600; h++) {
|
try {
|
sleep(1000);
|
db_backup_manual_en = getSqlDB_BackUpManual_EN();
|
if(true == db_backup_manual_en) {
|
break;
|
}
|
} catch (InterruptedException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
|
if(true == db_backup_manual_en) {
|
break;
|
}
|
|
Date dt = new Date();
|
long dt_s = (dt.getTime() - m_latest_backup_time.getTime())/1000;
|
if((dt_s/(24*3600)) >= back_up_day) {
|
break;
|
}
|
}
|
|
System.out.println(this.getName() + " - MysqlDBBackUp_Thread backup all dbs at "
|
+ Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
doDbBackUp();
|
updateBackupInf();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
|
//»ñÈ¡Êý¾Ý¿â±¸·Ý±í¸ñÖÐµÄ SqlDB_BackUpManual_ENÖµ
|
private boolean getSqlDB_BackUpManual_EN() {
|
boolean en = false;
|
Sql_Mysql sql = new Sql_Mysql(m_conn_pool.getConn());
|
try {
|
ResultSet res = sql.sqlMysqlQuery("SELECT SqlDB_BackUpManual_EN FROM " + Sql_Mysql.AppSys_Table);
|
if(res.next()) {
|
en = res.getBoolean(1);
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally {
|
sql.close_con();
|
}
|
return en;
|
}
|
|
|
//¸üб¸·ÝÊý¾Ý¿â±í¸ñÖеÄÐÅÏ¢
|
private void updateBackupInf() {
|
Sql_Mysql sql = new Sql_Mysql(m_conn_pool.getConn());
|
try {
|
sql.sqlMysqlExecute("UPDATE " + Sql_Mysql.AppSys_Table
|
+ " SET "
|
+ " SqlDB_BackUpTime='" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "',"
|
+ " SqlDB_BackUpManual_EN=false");
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally {
|
sql.close_con();
|
}
|
}
|
|
//Ö´ÐÐÊý¾Ý¿â±¸·Ý
|
private void doDbBackUp() {
|
Process ps;
|
try {
|
String tmp_filename = "all_backup_" + Com.getDateTimeFormat(new Date(), Com.DTF_YMD_h_m_s);
|
String cmd_str = db_path + File.separator + "bin" + File.separator
|
+ "mysqldump -P " + mSqlConnPort + " -uroot -plmx8688139 --all-databases > "
|
+ db_path + File.separator + tmp_filename
|
+ "&echo popstar hello";
|
|
ps = Runtime.getRuntime().exec("cmd");
|
InputStreamReader isr = new InputStreamReader(ps.getErrorStream());
|
//Óûº³åÆ÷¶ÁÐÐ
|
BufferedReader br = new BufferedReader(isr);
|
|
// next command
|
OutputStreamWriter osw = new OutputStreamWriter (ps.getOutputStream ());
|
BufferedWriter bw = new BufferedWriter (osw);
|
bw.write(cmd_str);
|
bw.newLine();
|
bw.write("del " + db_path + File.separator + "*.bksql");
|
bw.newLine();
|
|
String target_filename = "all_backup_" + Com.getDateTimeFormat(new Date(), Com.DTF_YMD_h_m_s);
|
bw.write("rename " + db_path + File.separator + tmp_filename + " " + target_filename + ".bksql");
|
|
bw.newLine();
|
bw.flush();
|
bw.close();
|
osw.close();
|
|
String line = null;
|
//Ö±µ½¶ÁÍêΪֹ
|
while((line=br.readLine()) != null) {
|
System.out.println(line);
|
}
|
ps.destroy();
|
br.close();
|
isr.close();
|
} catch (IOException e2) {
|
// TODO Auto-generated catch block
|
e2.printStackTrace();
|
}
|
}
|
}
|