簡體   English   中英

從java獲取Mysql數據庫備份的問題

[英]Issue in Taking Mysql database backup from java

好的,我知道有很多與它相關的問題和文章,在關注它們並與它們一起玩之后,我仍然無法成功。這是我的代碼

 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;   
 import java.security.CodeSource;
 import javax.swing.JOptionPane;

public class BackupData 
{

public static void main(String[] args) {
    try 
    {

        /*NOTE: Getting path to the Jar file being executed*/
        /*NOTE: YourImplementingClass-> replace with the class executing the code*/
        CodeSource codeSource = BackupData.class.getProtectionDomain().getCodeSource();
        File jarFile = new File(codeSource.getLocation().toURI().getPath());
        String jarDir = jarFile.getParentFile().getPath();
        System.out.println("jarDir"+ jarDir);


        /*NOTE: Creating Database Constraints*/
        String dbName = "xyz";
        String dbUser = "root";
        String dbPass = "root";

        /*NOTE: Creating Path Constraints for folder saving*/
        /*NOTE: Here the backup folder is created for saving inside it*/
        String folderPath = jarDir + "\\backup";

        /*NOTE: Creating Folder if it does not exist*/
        File f1 = new File(folderPath);
        System.out.println("f1" + f1);
        f1.mkdir();

        /*NOTE: Creating Path Constraints for backup saving*/
        /*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/

         String savePath = "\"" + jarDir + "\\backup\\" + "1.sql\"";
         System.out.println("savepath" + savePath);

        /*NOTE: Used to create a cmd command*/
        String executeCmd = "C:\\Program Files\\MySQL\\MySQL Workbench 6.3 CE\\mysqldump -u " + dbUser + " -p " + dbPass + " --database " + dbName + " -r " + savePath;

        /*NOTE: Executing the command here*/
        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();

        /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/


              if (processComplete == 0) 
          {
            System.out.println("Backup Complete");
          } 

             else 
          {
            System.out.println("Backup Failure");
            System.out.println(processComplete);
          }




    } 
    catch (URISyntaxException | IOException | InterruptedException ex) 
    {
        JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());
    }
      }
     }

此代碼給出的輸出 - 備份失敗,2(進程完成值)

我只是不明白我做錯了什么?我錯過了什么嗎? 我只是無法弄清楚問題是什么,任何幫助將不勝感激,謝謝。

你為什么要做這一切? 為此,有一個名為mysqldump的命令行實用程序。

mysqldump 客戶端實用程序執行邏輯備份,生成一組 SQL 語句,可以執行這些語句來重現原始數據庫對象定義和表數據。 它轉儲一個或多個 MySQL 數據庫以進行備份或傳輸到另一台 SQL 服務器。 mysqldump 命令還可以生成 CSV、其他分隔文本或 XML 格式的輸出。 您還可以從 mysql 手冊中找到以下有用的鏈接。

  1. https://dev.mysql.com/doc/refman/5.7/en/backup-methods.html

  2. https://dev.mysql.com/doc/refman/5.7/en/copying-databases.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM