视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Java调用bat执行的备份Oracle数据库类
2020-11-09 12:03:43 责编:小采
文档


package com.buckupDB;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.Fil

package com.buckupDB;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.filechooser.FileSystemView;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import com.pm360.mda.platform.db.DBConn;
import com.pm360.mda.platform.util.DBUtil;
import com.pm360.mda.platform.util.Logger;
import com.pm360.mda.platform.util.StringUtil;


/**
*
* 数据备份 impdp / expdp
* @author ZQD32
*
*/
public class BuckupdpDB {

/**
* //获取桌面路径
* @return
*/
private static String getDesktopPath()
{
FileSystemView fsv = FileSystemView.getFileSystemView();
File file= fsv.getHomeDirectory();
return file.getPath();
}


/**
* 获取时间字符
* @return
*/
private static String getDateTime()
{
Date date=new Date();
String str=date.toLocaleString();
str=str.replaceAll(":", "-");
str=str.replaceAll(" ", "-");
return str;
}

/**开启指定进程
* @param map
*/
private static void runingProcess(Map map)
{
String strPath = StringUtil.formatDbColumn(map.get("Paths"));
strPath = strPath.replace(" ", "\" \"");
try
{
StringBuilder sb=new StringBuilder("cmd /c start "+strPath+"\\temp.bat");
Process p = Runtime.getRuntime().exec(sb.toString());
}
catch (IOException e)
{
e.printStackTrace();
}
}

/**
* 执行备份文件
* @return
* @throws IOException
*/
public static String expingProcess(Map map)
{
if(productionExpBat(map).equalsIgnoreCase("good"))
{
runingProcess(map);
}
else
{
return "false";
}
return "true";
}

/**
* 执行导入文件
* @return
* @throws IOException
*/
public static String impingProcess(Map map)
{
if(productionImpBat(map).equalsIgnoreCase("good"))
{
runingProcess(map);
}
else
{
return "false";
}
return "true";
}

/**
* 获取 连接信息
* @param sid
* @param user
*/
private static Map getConnInfo()
{
Map map =new HashMap();

try {
BasicDataSource init = (BasicDataSource)new InitialContext().lookup("java:comp/env/jdbc/**");
String url =init.getUrl();
//获取Oracle数据库实例名
map.put("sid", url.substring(url.lastIndexOf(":")+1));
//获取oracle数据库登录名
map.put("user",init.getUsername());
//获取oracle数据库登密码
map.put("pwd", init.getPassword());
} catch (NamingException e) {
e.printStackTrace();
}
return map;
}


/**
*
* 生成 导出配置Bat文件
*/
private static String productionExpBat(Map map)
{
//得到数据库登录的信息
Map ConnInfoMap =getConnInfo();
//生成数据泵配置文件
productionExpdpPar(map);
//设置一个标志
String flag="";
//创建个缓存 数据集
List tmpBat=new ArrayList();
//获取模板 文件
File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\expdpDB.bat");
//生成空白的bat临时文件
File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\temp.bat");
//申明读取缓冲器
BufferedReader br=null;
try {
//设置读取缓冲器 文件指向
br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
//获取 rar 备份 文件的名字
String filename=StringUtil.formatDbColumn(map.get("FILENAME"));
if(filename==null||filename.equals("")){
//默认为 DBProjBuckup
filename="DBProjBuckup";
}
filename=filename+"_"+getDateTime();

try {
try {
while(br.ready())//判断是否还有可读信息
{
//读取一行数据
String str=br.readLine().toString();
//设置数据库的实例
if(str.startsWith("set sid")){
str="set sid="+ConnInfoMap.get("sid");
}
//设置数据登录名
else if(str.startsWith("set user")){
str="set user="+ConnInfoMap.get("user");
}
//设置数据登密码
else if(str.startsWith("set pwd")){
str="set pwd="+ConnInfoMap.get("pwd");;
}
//设置备份项目id过滤
else if(str.startsWith("set proj_id")){
str="set proj_id="+StringUtil.formatDbColumn(map.get("PROJ_ID"));
}
//设置备份文件名
else if(str.startsWith("set filename")){
str="set filename="+filename;
}
//桌面路径
else if(str.startsWith("set Desktop")){
str="set Desktop="+getDesktopPath();
}
//dmp文件存放路径**************该路径需要查取的
else if(str.startsWith("set savePth")){
str="set savePth="+getDumpPath();
}
tmpBat.add(str);
}
//关闭缓冲器
br.close();
} catch (IOException e) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}

//设定写数据缓冲器 文件指向
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);
for(String str : tmpBat)
{
//按行写入
pw.println(str);
}
//pw.println("del /q BuckupParfile.par");
//pw.println("del /q temp.bat");

//关闭缓冲器
pw.close();

flag="good";
} catch (FileNotFoundException e) {
flag="error";
e.printStackTrace();
}
return flag;
}

/**
* 获取数据泵数据存储路径
* @return
*/
private static String getDumpPath()
{
//select OS_PATH from sys.dir$ where OS_PATH like '%\dpdump\'
String dumpPath=null;
Connection conn=null;
try {
conn=DBConn.getConnection("***");//该方法自己写
dumpPath = (String)DBUtil.getResultFieldValue(conn, "select OS_PATH from sys.dir$ where OS_PATH like '%\\dpdump\\'");
} catch (SQLException e) {
Logger.error(e);
}finally{
try
{
conn.close();
} catch (SQLException e)
{
Logger.error(e);
}
}
return dumpPath;
}

/**
* 配置数据泵的参数
* @param map
* @return
*/
private static String productionExpdpPar(Map map)
{
String proj_id = StringUtil.formatDbColumn(map.get("PROJ_ID"));
//获取模板 文件
File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\BuckupParfile.par");
//生成空白的bat临时文件
File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\BuckupParfile.par");
try {
//申明读取缓冲器 //设置读取缓冲器 文件指向
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));

//设定写数据缓冲器 文件指向
PrintWriter pw= new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);

try {
while(br.ready())//判断是否还有可读信息
{
//读取一行数据
String str=br.readLine().toString();
str=str.replace("%proj_id%", proj_id);
pw.println(str);
}
br.close();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "";
}

/**
* 生成导入配置bat文件
* @param map
* @return
*/
private static String productionImpBat(Map map)
{
//获取 rar 备份 文件的名字
String FilePath=StringUtil.formatDbColumn(map.get("FilePath"));
String FileName = FilePath.substring(FilePath.lastIndexOf('\\')+1);

//生成 导入前清理 sql 文件
productionClearSql(map);

Map ConnInfoMap =getConnInfo();



//设置一个标志
String flag="";
//创建个缓存 数据集
List tmpBat=new ArrayList();
//获取模板 文件
File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\impdpDB.bat");
//生成空白的bat临时文件
File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\temp.bat");
//申明读取缓冲器
BufferedReader br=null;
try {
//设置读取缓冲器 文件指向
br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

try {
try {
while(br.ready())//判断是否还有可读信息
{
//读取一行数据
String str=br.readLine().toString();
//设置数据库的实例
if(str.startsWith("set sid")){
str="set sid="+ConnInfoMap.get("sid");
}
//设置数据登录名
else if(str.startsWith("set user")){
str="set user="+ConnInfoMap.get("user");
}
//设置数据登密码
else if(str.startsWith("set pwd")){
str="set pwd="+ConnInfoMap.get("pwd");
}
//设置备份文件 路径
else if(str.startsWith("set FilePath")){
str="set FilePath="+FilePath;
}
//dump路径
else if(str.startsWith("set Dumpfile")){
str="set Dumpfile="+getDumpPath();
}
//文件名
else if(str.startsWith("set FileName")){
str="set FileName="+FileName;
}
tmpBat.add(str);
}
//关闭缓冲器
br.close();
} catch (IOException e) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}

//设定写数据缓冲器 文件指向
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);
for(String str : tmpBat)
{
//按行写入
pw.println(str);
}
pw.println("ECHO watting …………………………");
pw.println("sqlplus %user%/%pwd%@%sid% @UpdateParentId.sql ");
pw.println("del /q UpdateParentId.sql");
pw.println("del /q impClearing.sql");
pw.println("del /q temp.bat");
//关闭缓冲器
pw.close();
flag="good";
} catch (FileNotFoundException e) {
flag="error";
e.printStackTrace();
}

return flag;
}

/**
* 生成导入前sql清理文件
* @param map
* @return
*/
private static String productionClearSql(Map map)
{
//获取项目id
//StringUtil.formatDbColumn(map.get("PROJ_ID")) 这个是即将导入 项目的 新的父id
String proj_id =getProjId(map);
//创建个缓存 数据集
List tmpBat=new ArrayList();
//获取模板 文件
File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\impClearing.sql");
//生成空白的bat临时文件
File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\impClearing.sql");
//申明读取缓冲器
BufferedReader br=null;
try {
//设置读取缓冲器 文件指向
br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
try {
while(br.ready())//判断是否还有可读信息
{
//读取一行数据
String str=br.readLine().toString();
//设置备份文件名 %proj_id%
str = str.replace("%proj_id%", proj_id);
tmpBat.add(str);
}
//关闭缓冲器
br.close();
} catch (IOException e) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}

//设定写数据缓冲器 文件指向
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);
for(String str : tmpBat)
{
//按行写入
pw.println(str);
}
pw.println("exit");
//关闭缓冲器
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

return "";
}


/**
* 生成导入 项目的id 和 更新语句
* @param map
* @return
*/
private static String getProjId(Map map)
{
String FilePath=StringUtil.formatDbColumn(map.get("FilePath"));
FilePath = FilePath.substring(FilePath.lastIndexOf("-")+1, FilePath.lastIndexOf('.'));

File UpParentId=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\UpdateParentId.sql");
PrintWriter pw;
try {
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(UpParentId)),true);
pw.println("update cm_proj t set t.parent_proj_id='"+StringUtil.formatDbColumn(map.get("PROJ_ID"))+"',t.parent_path=('"
+getProjPath(StringUtil.formatDbColumn(map.get("PROJ_ID")))+"'||';'||'"+FilePath+"') where t.proj_id='"+FilePath+"';");
pw.println("commit;");
pw.println("exit");
//关闭缓冲器
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return FilePath;
}

/**
* 获取父级路径
* @param map
* @return
*/
private static String getProjPath(String proj_id)
{
String projPath=null;
Connection conn=null;
try {
conn=DBConn.getConnection("***");//该方法自己写
projPath = (String)DBUtil.getResultFieldValue(conn, "select parent_path from cm_proj where proj_id ='"+proj_id+"'");
conn.close();
} catch (SQLException e) {
Logger.error(e);
}finally{
try
{
conn.close();
} catch (SQLException e)
{
Logger.error(e);
}
}
return projPath;
}

}

下载本文
显示全文
专题