<html>
<head>
<title>数据备份</title>
<meta name="decorator" content="default"/>
</head>
<body>
<table>
<thead>
<tr>
<th style=" 20%;">表格描述</th>
<th style=" 20%;">操作</th>
</tr>
</thead>
<tbody>
<!-- tableList:后台获取的数据库的所有数据表 -->
<c:forEach items="${tableList}" var="row">
<tr>
<input id="id" name="id" type="hidden" value="${row.id}"/>
<input id="name" name="name" type="hidden" value="${row.name}"/>
<td>${row.description}</td>
<td><a href="#" onclick="startBackup(${row.id})">备份</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<script type="text/javascript">
function startBackup(id) {
try{
var downloadToken = +new Date(); //设置一个时间戳发送到后台
var url = "/sys/backupData/startBackup?id="+id+"&downloadToken="+downloadToken;
loading('正在备份...'); //提示框
var form = $("<form></form>");
form.attr("action", url);
form.attr("method", "post");
form.attr("enctype", "multipart/form-data");
$("body").append(form);
form.submit();
form.remove();
function checkToken() {
//前端实时监测时间戳和后台设置的cookie值是否相等,相等就说明文件下载成功,就可以关闭提示框
var token = getCookie("downloadToken");
if (token && token == downloadToken) {
clearTimeout(downloadTimer);
closeLoading(); //关闭提示框
}
}
var downloadTimer = setInterval(checkToken, 1000);
}catch(e){
alert(e.name + ": " + e.message);
}
}
//获取后台设置的cookie值
function getCookie(cookieName) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for(var i = 0; i < arrCookie.length; i++){
var arr = arrCookie[i].split("=");
if(cookieName == arr[0]){
return arr[1];
}
}
return "";
}
</script>
</body>
</html>
/**
* 备份和下载
*/
@RequestMapping(value = {"startBackup", ""})
public void startBackup(Table table,String downloadToken,HttpServletRequest request,HttpServletResponse response) throws Exception {
//将前端发送过来的时间戳设置到cookie中
Cookie cookie = new Cookie("downloadToken", downloadToken);
cookie.setPath("/");
cookie.setMaxAge(-1);
response.addCookie(cookie);
//获取数据库表的信息
table = inputTableService.get(table);
//配置项
String userName = "jdbc.username"; //进入数据库所需要的用户名
String password = "jdbc.password"; //进入数据库所需要的密码
String SID = "jdbc.url"; //用户所在的数据库名
String savePath = "C:\Project\oracleBackupData";//备份的文件所在的路径
SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
String backupDate=format.format(new Date()); //文件备份的时间
File saveFile = new File(savePath);
if (!saveFile.exists()) { // 如果目录不存在
saveFile.mkdirs(); // 创建文件夹
}
//exp导出命令:exp 用户名/"""密码"""数据库名 file=路径 tables=(表名)
//file= C:ProjectoracleBackupData表格描述+文件备份的时间.dmp
String command = "exp " + userName + "/"""" + password+ """"@" + SID + " file="+savePath+table.getDescription()+backupDate+".dmp tables=("+table.getName()+")";
Process process = Runtime.getRuntime().exec(command);
final InputStream is1 = process.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(is1));
String info;
try {
while ((info = br.readLine()) != null) {
System.out.println("info: " + info);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
// 保存输出结果
StringBuilder buf = new StringBuilder();
String line = null;
int i = 0;
try {
while ((line = br2.readLine()) != null) {
// 循环等待ffmpeg进程结束
System.out.println("info: " + line);
buf.append(line);
}
if (buf.toString().contains("ORA-")&& buf.toString().contains("EXP-")) {
process.destroy();
} else {
i = process.waitFor();
System.out.println("over status: " + i);
}
} catch (Exception e1) {
e1.printStackTrace();
}
//以上备份完成后将备份后的文件下载到浏览器端
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(table.getDescription()+backupDate+".dmp","UTF-8"));
//备份文件的路径
String realPath = savePath+table.getDescription()+backupDate+".dmp";
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(realPath);
int len = 0;
byte[] buffer = new byte[1024];
out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.flush();
in.close();
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}