我正在尝试学习JSP / Servlets ......我设法编写了一个备份整个数据库的函数......
package com.functions;
import java.io.*;
import java.io.IOException;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import com.functions.utility.DBConnector;
public class addMarks3 extends HttpServlet {
private static final long serialVersionUID = 8097085789553030042L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("--------------------");
System.out.println(" On Backup.java ");
System.out.println("--------------------");
fetchDataFromDB(request,response);
}
private void fetchDataFromDB(HttpServletRequest request,HttpServletResponse response){
try {
Runtime runtime = Runtime.getRuntime();
File backupFile = new File("C:/Users/Rohan/Desktop/LMS/backup.sql");
FileWriter fw = new FileWriter(backupFile);
Process child = runtime.exec("C:/Program Files (x86)/MySQL/MySQL Server 5.5/bin/mysqldump --user=root --password=root --lock-all-tables --opt dummy");
InputStreamReader irs = new InputStreamReader(child.getInputStream());
BufferedReader br = new BufferedReader(irs);
String line;
while( (line=br.readLine()) != null ) {
fw.write(line + "\n");
}
fw.close();
irs.close();
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
我仍然无法弄清楚如何验证备份是否成功.. ?? 如何检查备份是否成功。
PS:每次运行文件时都会填充文件....
答案 0 :(得分:0)
好吧,如果每次都填充文件,这是否意味着备份成功了?
您是否考虑过将流程流传输到响应流?
答案 1 :(得分:0)
尝试这样。检查文件大小。
File backupFile = new File("C:/Users/Rohan/Desktop/LMS/backup.sql");
int len1=backupFile.length();
FileWriter fw = new FileWriter(backupFile);
Process child = runtime.exec("C:/Program Files (x86)/MySQL/MySQL Server 5.5/bin/mysqldump --user=root --password=root --lock-all-tables --opt dummy");
InputStreamReader irs = new InputStreamReader(child.getInputStream());
BufferedReader br = new BufferedReader(irs);
String line;
while( (line=br.readLine()) != null ) {
fw.write(line + "\n");
}
fw.close();
irs.close();
br.close();
int len2=backupFile.length();
int compare=len2-len1;
if(compare>0)
{
System.out.println("-- Backup done---");
}