package atest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import web.db.DBConnector;
/**
* @author 朱言信
* @desc 备份oracle
*/
public class DadaBackup4Oracle {
public void doWork(){
//执行该数据备份线程
innerBackupClass ibc = new innerBackupClass("这里输入oracle的用户名", "密码", "192.168.0.66:1521/orcl", "输出的地址", "输出的文件名", "要备份的表名");
Thread td = new Thread(ibc);
td.start();
}
class innerBackupClass implements Runnable {
private String user; //用户名
private String password; //密码
private String url; //链接地址
private String outFilePath; //输出的文件路径
//private String outZipFilePath; //输出的zip文件路径
private String filename; //文件名
private String tableNames; //表名
//Sys_databackup databackup = null;
/**
* 构造方法
*/
public innerBackupClass(String user, String password, String url, String outFilePath,
//String outZipFilePath,
String filename,
String tableNames
//Sys_databackup databackup
) {
this.user = user;
this.password = password;
this.tableNames = tableNames;
this.url = url;
this.outFilePath = outFilePath;
//this.outZipFilePath = outZipFilePath;
this.filename = filename;
//this.databackup = databackup;
}
/**
* 线程执行入口
*/
public void run() {
Connection con = DBConnector.getconecttion(); // 取得一个数据库连接
Runtime rt = Runtime.getRuntime();
Process processexp = null;
checkCreatDir(outFilePath);
//这里拼装的是exp导出 当然还可以使用其它的 你懂得
String exp = "exp " + user + "/" + password + "@" + url + " file="
+ outFilePath + "/" + filename + ".dmp";
if(!"".equals(tableNames)){
exp += " tables=(" + tableNames + ")";
}
try {
processexp = rt.exec(exp);
new Thread(new StreamDrainer(processexp.getInputStream())).start();
new Thread(new StreamDrainer(processexp.getErrorStream())).start();
processexp.getOutputStream().close();
int exitValue = processexp.waitFor();
//databackup.setBackupstate("1"); //1为成功 0为失败
if(exitValue != 0){
//databackup.setBackupstate("0");
}
//将备份DMP文件压缩为zip
//删除dmp文件及所在的文件夹
//将备份的结果持久化
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
DBConnector.freecon(con); // 释放数据库连接
}
}
public void checkCreatDir(String dirPath) { // 目录是否存在
File file = new File(dirPath);
if (!file.exists()) {
file.mkdirs();
}
}
}
/**
*输出控制台信息
*/
class StreamDrainer implements Runnable {
private InputStream ins;
public StreamDrainer(InputStream ins) {
this.ins = ins;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}