检查备份已创建

发布于 2024-12-04 16:27:38 字数 1927 浏览 0 评论 0原文

我正在尝试学习 JSP/Servlet...我设法编写了一个备份整个数据库的函数...

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:每次我运行它时,该文件都会被填充......

I'm trying to learn JSP/Servlets... I managed to write a function that takes a backup of the whole database...

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();
        }

    }
}

I still cant figure out how to verify if the backup was successful..??
As in how do i check if the backup was succesfull or not.

PS: The file does get populated everytime i run it....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-12-11 16:27:38

尝试这样。检查文件大小。

 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---");
          }

Try like this.Check for the file sizes.

 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---");
          }
只有一腔孤勇 2024-12-11 16:27:38

那么,如果每次都填充该文件,是不是就意味着备份成功了?

您是否考虑过将进程流流式传输到响应流?

Well, if the file gets populated everytime, doesn't that mean that the backup was successful?

Have you considered either streaming the process stream to the response stream?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文